is there something wrong with my formatting?

Im trying to write a program that will print the numbers from 1-100 in this format:
1 3 5 7 9
12 14 16 18 20
21 23 25 27 29
32 34 36 38 40
41 43 45 47 49
52 54 56 58 60
61 63 65 67 69
72 74 76 78 80
81 83 85 87 89
92 94 96 98 100

i have to use loops to write the program. I thought that this wold be reltively easy, but when i initially ran the program this is all that was output

1
2
3
11111111111111111111111113333333333333333333333333555555555555555555555555577777777777777777777777779999999999999999999999999
0
0


here is what i have coded so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using std::cout;
using std::endl;

int main()
{
int a,b,c;


 for(int a=1; a<=10; a+=2)
 for (int b=12; b<=20; b+=2)
 for (int c=21; c<=29; c+=2)
     cout<<a<<"";
     cout<<endl;
     cout<<b<<"";
     cout<<endl;
     cout<<c<<"";
     cout<<endl;

return 0;
}


I am really new at using loops, but if someone could point me in the direction of what I am doing wrong i would be extremely appreciative.
You are trying to print too many things in your loops, which are nested (acting at the same time) instead of sequential.
1
2
3
4
5
6
7
8
9
10
for (int a=1; ...)
{
  for (int b=12; ...)
  {
    for (int c=21; ...)
    {
      ...
    }
  }
}
You have, BTW, changed your code since you got that output. Be careful about that, because people who know programming don't like you when you say "this is what I did" and "this is what I got" when the two don't match, and you are more likely to be ignored.

You need to think about how to create that output pattern yourself, using a pen and paper (as if someone gave you instructions to do it by hand) before you can make the computer do it. This is the rule of programming that few people will ever tell you.

As a hint, you will need two loops, nested. The outside loop will work from 1 to 100, inclusive. The inside loop will only count five items, but you will not print the inner loop's variable.
1
2
3
4
5
6
7
8
9
for (int a = 1; a <= 100; XXX)
{
  for (int b = 0; b < 5; b++)
  {
    cout << a << " ";
  }
  YYY;
  cout << endl;
}
At XXX and YYY you will do something to a to make it have the numbers you want. What is it?

Hope this helps.
arghhh Duoas, I just spent 15 minutes writing the code with detailed explanations of every line and its purpose. and was ready to post it but saw you beat me with a response! ughhh, now I have to go back to my much harder and frustrating project! Curses Batman!!! ;)
I just re-complied and ran my posted code and got the same exact output that I posted, so im not sure how it was different.

XXX should be a+=2?

is the b variable there to make the the the output in 5 columns?
is this a homework assignment?
here is a working code mixed with pseudo code. I tested it with real values, and the console output appears the way you are requiring. if you can study it and figure out what the variables should be and where they should be placed, it should work great. Be aware of bracketing requirements and what you want each loop to achieve, as you can see by my pseudocode, I only used two for loops to achieve the results., anyway, here it is:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

#include <iostream>
using namespace std;

int main()
{
	int a,b,console value;
	a=b=starting console value=loop starting value; 

	for (a=loop starting value; a<=loop boundary; a++) // This is the outer for loop

	{	//bracketing is opening bracket for the outer for loop
	
	b=inner loop start value;// b initializes inner loop starting value for next cycle,


	for (b=loop starting value; b<=loop boundary; b++)//This is the inner for loop/this will run through
					//its cycle before the next iteration of the 
					//of the outside loop is initiated

		{ 	//this is the opening bracket for the inner loop, remember, the arguements
			//contained between the inner loop brackets will initiate each time the
			//outerloop is iterated.

			cout << console value output << " "; value +=2;	//inner loop statements execute

		}	//closing bracket for the inner loop

	if(console value % 2) console value c+=1;	//conditional statement this is acted upon each
		else console value-=1; cout << '\n';	//time the outer loop is incremented (initialization
						//of proper variable value before newline
									
	}// closing bracket for outer loop
     

	return 0;
}

my output:


1 3 5 7 9
12 14 16 18 20
21 23 25 27 29
32 34 36 38 40
41 43 45 47 49
52 54 56 58 60
61 63 65 67 69
72 74 76 78 80
81 83 85 87 89
92 94 96 98 100


Last edited on
oh wow thank you! I just have one more question... what do you mean by console value?

it is a lab assignment, im in an introductory c++ class. im having a lot trouble and am trying to find a tutor at my school. but in the mean time I still need to complete my assignments.
I used the term 'console value' to describe the actual variable that is used, added to, conditioned then output to the console, I left a hint on line 29 whereas I denoted 'c' as the console value in my code. Niether for loops output to the screen, replace all 'console value' pseudo code with 'c' and work backwards from there, paying attention to the comments and hopefully it will come together for you. Here is line 29 & 30 as i have it in my code:
1
2
if(c % 2) c+=1;	
    else c-=1; cout << '\n';


also, I realized line 14 became unnecessary as the code developed, so just remove it
Last edited on
...and got the same exact output that I posted
Sorry, I missed that your loop only contains one statement. Lines 14-18 are not in the loop.
ha duoas, I cant keep up with you guys your all in go fast boats and I am in a canoe paddling with my hands ;)
hmm... try this one.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>

int main(){

	int i, num = 1;
	while(num != 100)
	{
		for(i = 0; i < 5; i++){
			std::cout<<num<<" ";
			if(i != 4)
				num += 2;
			else
				break;
		}
		if(num == 100)
			break;
		std::cout<<"\n";
		if(num%2 == 0)
			num += 1;
		else
			num += 3;

	}
	return 0;
}
different but basically the same:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

#include <iostream>
using namespace std;

int main()
{
	int a,b,c;
	a=b=c=1; 
	for (a=1; a<=10; a++)  
	{		
		for (b=1; b<=5; b++)  
		{ 	
			cout << c << " "; c +=2;	
		}		
	if(c % 2)  c+=1;	
		else c-=1; cout << '\n';	
	}     
	return 0;
}
Thanks you all for you help!
I hope this would be easiest one. try it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  int x = 0;
  for(int i = 1; i <=100; i++)
  {
    if((i%2) && !x )
    {
      printf("%d ", i);
    }
    if(!(i%2)&& x )
    {
      printf("%d ", i);
    }
    if(!(i%10))
    {
      x ^= 1;
      printf("\n");
    }
  }
@harini1111
comment: C-style programming. use cout instead of printf. this is a C++ forum you know?
Topic archived. No new replies allowed.