Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I'm still very new in coding world and I'm having a hard time solving this question. So, I need to create a program that will ask the user to input 3 integers. The inputted numbers will be the starting point, distance between, and stopping point sequentially. Example:
Enter three integers: 10 2 20
Solution: 10 12 14 16 18 20

What I have tried:

I've tried doing it manually like an arithmetic problems which didn't work because you don't have the exact number of the stopping point. Also tried for and if, but I still didn't know what to put inside the conditions. It will be very helpful if anyone can help me, thank you
Posted
Updated 11-Aug-20 11:06am
Comments
Patrice T 10-Aug-20 20:08pm    
Show your code.
Richard MacCutchan 11-Aug-20 9:04am    
You are give three numbers A, B, and C. A is the starting point, C is the limit, and B is the step. So writing a simple for loop should be easy, even for a beginner.

Your statement is incorrect : you DO have the exact number of the stopping point because it was entered by the user. This is asking for an implementation as a for loop because those are the three parameters it uses. A for loop looks like this :
C++
for( Initialize; Condition; Increment )
{
   // code executed on every loop iteration
}
Following is a little sample code that implements a for loop and each of the three parameters is a call to a function. In most loops they are simple statements and the first line of each of the three functions is what the statement usually looks like. Anyway, this is meant as a mini-tutorial. I recommend that you read up further on for loops because they are directly applicable to your problem.
C++
// for loop example

int Initialize( int init )
{
   printf( "Initialize() : returning value of %d\n", init );
   return init;
}

bool Condition( int & value, int limit )
{
   bool result = ( value < limit );
   const char * rstr = result ? "true" : "false";
   printf( "Condition() : value is %d - returning %s\n", value, rstr );
   return result;
}

void Increment( int & value, int incr )
{
   value += incr;
   printf( "Increment() : value is now %d\n", value );
}

void main()
{
   const int init = -1;    // initial value
   const int limit = 3;    // limit of loop
   const int incr = 1;     // increment step

   printf( "beginning for loop : init is %d, limit is %d, incr is %d\n",
           init, limit, incr );

   int value;
   for( value = Initialize( init ); Condition( value, limit ); Increment( value, incr ) )
   {
       printf( "for loop body : value is %d\n", value );
   }

   // the equivalent for loop is :
   // for( value = init; value < limit; value += incr )

   printf( "exited for loop - value is %d\n", value );
}
I recommend that you compile and run this program to see what happens. I made the statements functions so you can see the exact order of execution of each statement. You can also use a debugger and step through the program to see it in detail.

Another very useful thing to know is how a while loop differs from a for loop. The most important difference is a while has no incrementing statement which means a continue statement will act differently. In a for loop continue will cause execution to jump to the incrementer. In a while loop there isn't one so it goes back to the conditional statement. To see this in action, change the for loop to this and observe the behaviour :
C++
for( value = Initialize( init ); Condition( value, limit ); Increment( value, incr ) )
{
    if( value == 1 )
        continue;
    printf( "for loop body : value is %d\n", value );
}
With this in mind, a for loop can be thought of as a while loop. Here is the equivalent while loop :
C++
// for loop equivalent while loop

   printf( "beginning while loop : init is %d, limit is %d, incr is %d\n",
           init, limit, incr );

   int value = Initialize( init );
   while( Condition( value, limit ) )
   {
       printf( "while loop body : value is %d\n", value );

       Increment( value, incr );
   }

   printf( "exited while loop - value is %d\n", value );
Here is the while loop with the continue statement implement as a goto so it behaves like the for loop :
C++
// for loop equivalent while loop with pseude-continue

   printf( "beginning while loop : init is %d, limit is %d, incr is %d\n",
           init, limit, incr );

   int value = Initialize( init );
   while( Condition( value, limit ) )
   {
       if( value == 1 )
           goto ContinueTarget;

       printf( "while loop body : value is %d\n", value );

ContinueTarget :

       Increment( value, incr );
   }

   printf( "exited while loop - value is %d\n", value );
Warning : if you change the goto statement to a continue you will wind up in an endless loop because the incrementer would be continually skipped and Condition would always return true.

Here's one last thing. Since you have seen how a for statement is equivalent to a while statement, here is how the for statement is equivalent to an if with a goto. Here is the initial for loop written with only if and goto :
C++
    init = Initialize( init );
LoopCondition:
    if( ! Condition( value, limit ) )
        goto LoopEnd;

    printf( "for loop body : value is %d\n", value );

    Increment( value, incr );
    goto LoopCondition;
LoopEnd:
I hope you can see why people prefer to use for and while loops.

As a test for yourself, add code to last snippet of the for loop so it behaves like the previous one with the continue statement.
 
Share this answer
 
v8
Comments
BillWoodruff 10-Aug-20 13:03pm    
I suggest you check this code to see if it compiles.
Richard MacCutchan 11-Aug-20 9:02am    
Promoting goto statements???
Rick York 11-Aug-20 11:33am    
That is what "I hope you can see why people prefer to use for and while loops." says to you?

All right then.

Richard MacCutchan 11-Aug-20 12:22pm    
I agree it is an excellent explanation but ...
... the sort of people who look at these answers tend to be more interested in copying the codes 'as is', rather than reading the instructions and caveats.
Rick York 11-Aug-20 13:41pm    
Yes, you are right, and that was actually a good question. I remembered when I was writing my compiler I figured out how looping is built in terms of primitive instructions for code generation purposes and thought that might be useful for someone just learning. In retrospect, it probably isn't useful unless they are thinking in those terms and most beginners aren't. I was because I came from the hardware side and learned software later. Really though - who learns assembler languages before C? No one does today.
Hey! I am not too sure of what exactly you tried as you have not shared your attempt of code for it.


Rephrasing the problem statement for you if that helps: You need to start a loop from a that goes till c with step b. a, b, c being the three input parameters you shared.

Following is what you need to learn and try: Look for ways to loop in C/C++. You will learn that how to start a loop in steps and how they end.

Few keywords in case you are totally unaware: while, do-while & for


BTW, asking for entire code without sharing your effort or specific queries is not encouraged here!
 
Share this answer
 
v3
Comments
[no name] 10-Aug-20 12:29pm    
I don't see a reason for a uncommented 2 for this. My 5
Sandeep Mewara 10-Aug-20 12:32pm    
Thanks!
BillWoodruff 10-Aug-20 13:06pm    
my vote of #1: a useless, vague, reply to an OP who made no effort.
Sandeep Mewara 10-Aug-20 13:11pm    
Thanks! :)
BillWoodruff 12-Aug-20 14:24pm    
"rahi gulzar to phool khilenge" Kabir
It looks like you're struggling with basic syntax concepts of the C/C++ language. Check this website: Statements and flow control - C++ Tutorials[^] , specifically the section on for loops.

More generally, if you've just started to learn, this tutorial could be very helpful: you should work through it from the start.
 
Share this answer
 
Here is a tutorial site to learn the basics of programming: stanford.edu: Learn to Program[^]

Advice: find a tutorial about loops in C.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900