Need help with my for loop

Hello I was on here last night wish an issue with my code and I got a for loop that helped me fix my problem. now I need to change that loop to display on the screen:

4###
44##
444#
4444

This for loop completes the first line but I am having trouble getting it to display the consecutive 4 for the last 3 lines.
The code I have is



1
2
3
4
5
6
7
8
9
10
  for (int r = 0; r < size; r++)
			{
				cout << endl;
				for (int c = 0; c < size; c++)
				if (r == c)
					cout << size;
				else
					cout << '#';
			}



Size is an int variable and this code displays on the screen

4###
#4##
##4#
###4

and i need it to display what is up above.
Last edited on
Conside the following lines:
1
2
  if (r == c)
    cout << size;


How many times do you think that is going to output the 4?
Hint: once

What you appear to want is to output the 4 until the column (c) is equal to the row number (r). How would you express that until in a conditional?

BTW, you need to close your code with a
[/code]
tag.

Please try and format your code into source code so that way it is easier to read. When you start a new topic, the code and /code in brackets should already be there, just get your code between them, don't delete it, and it will format it correctly.
Last edited on
Im not sure how I would express that. should I switch line 6 and line 8 of the if statement ?
Switching lines 6 and 8 is only going to switch where the 4 and the # appear. That's not what you want.

I gave you a big hint with the bolded until.

Try this:
 
  if (c <= r)   // if col <= row, then print the number 


Last edited on
Topic archived. No new replies allowed.