### C++ using repetition structure

Pages: 12
I cleaned it up some by myself and I know what am I trying to get to but I don't know how to do it. It is not letting me post my screen shot of when I actually ran it :/ but I need it to say for example...
Set 1:0-9
Set 2:9-18
and so on.

Any suggestions? I changed those first two in the if statement to try and see if it would work but no luck on that.

//declare variables
int numbers =0;
int sets =0;

//display first set of numbers
cout << "Numbers:" << endl;

while (numbers >= 0 && numbers <= 117)
{
cout << numbers << endl;
numbers = numbers +9;

} //end while

if (numbers >= 0 && numbers <= 9)
cout << "Set 1:" << endl;
else if (numbers >= 9 && numbers <= 18)
sets = 2;
else if (numbers >= 18 && numbers <= 27)
sets = 3;
else if (numbers >= 27 && numbers <= 36)
sets = 4;
else if (numbers >= 36 && numbers <= 45)
sets = 5;
else if (numbers >= 45 && numbers <= 54)
sets = 6;
else if (numbers >= 54 && numbers <= 63)
sets = 7;
else if (numbers >= 63 && numbers <= 72)
sets = 8;
else if (numbers >= 72 && numbers <= 81)
sets = 9;
else if (numbers >= 81 && numbers <= 90)
sets = 10;
else if (numbers >= 90 && numbers <= 99)
sets = 11;
else if (numbers >= 99 && numbers <= 108)
sets = 12;
else if (numbers >= 108 && numbers <= 117)
sets = 13;

//end if
You have given no rationale for having "sets". Your professor has asked you to print our 2 lists of numbers.

 ``12345678910111213141516171819202122`` ``````#include using namespace std; int main() { //using for loop for (unsigned i = 0; i <= 117; i += 9) { cout << i << endl; } //using while loop unsigned i = 0; while (i <= 117) { cout << i << endl; i += 9; } return 0; }``````

Will do exactly what he has asked for.
Im doing what your saying but it is still running as

Numbers:
0
9
18
27
and so on?
Yes, that is what he asked for. If you need them to appear as
Set1: 0, 9, 18 etc.
Then you need to put the cout statements around the loops. Walk through the code identifying what it does, then identify where you need to change it to achieve your final goal. You really only need to add 4 lines and modify 2 other lines to finish this (from the example code I gave you).

Your output should look like this:
 ```0 9 18 27 36 45 54 63 72 81 90 99 108 117 0 9 18 27 36 45 54 63 72 81 90 99 108 117```
Ok Zaita so do I need to add four cout statements in the while statement?

0
0
9
9
18
18
and so on?
o_O
http://www.cplusplus.com/forum/general/98158/#msg527963

That was it. That was the whole program.
Oh ok awesome! Thank you for your help! Now I'm having trouble with the for statement though :/ Do you know anything that could help or maybe that I looked over?

#include <iostream>

using namespace std;

int main ()
{
//declare variables
int numbers =0;
int sets =0;

//display first set of numbers
cout << "Numbers:" << endl;

//using for loop
for (numbers = 0; numbers <= 117; numbers += 9);
{
cout << numbers << endl;

} //end for

if (numbers >= 0 && numbers <= 9)
sets = 1;
else if (numbers >= 9 && numbers <= 18)
sets = 2;
else if (numbers >= 18 && numbers <= 27)
sets = 3;
else if (numbers >= 27 && numbers <= 36)
sets = 4;
else if (numbers >= 36 && numbers <= 45)
sets = 5;
else if (numbers >= 45 && numbers <= 54)
sets = 6;
else if (numbers >= 54 && numbers <= 63)
sets = 7;
else if (numbers >= 63 && numbers <= 72)
sets = 8;
else if (numbers >= 72 && numbers <= 81)
sets = 9;
else if (numbers >= 81 && numbers <= 90)
sets = 10;
else if (numbers >= 90 && numbers <= 99)
sets = 11;
else if (numbers >= 99 && numbers <= 108)
sets = 12;
else if (numbers >= 108 && numbers <= 117)
sets = 13;

//end if

system("pause");
return 0;
} //end of main function
Another question...do I need the if statement?
I don't think you understand what we have given you. Spend some time with the code tweaking it and seeing how the results change. You need to READ what we say and then play with it. You have not listened.
Topic archived. No new replies allowed.
Pages: 12