My project required four pattern
i. a square with a left to right diagonal in which the diagonal is the size of the square
ii. a square with a right to left diagonal in which the diagonal is the size of the square
iii. a square that fills from left to right, using the size of the square as the ‘fill’ character
iv. a square that fills from right to left, using the size of the square as the ‘fill’ character
I was trying to figure out one of these
for example, the first one supposed to be when you enter 5
5####
#5###
##5##
###5#
####5
however, what I can get right now is
while (y < size)
{
while (x < size&&x<y)
{
cout << "#";
x++;
}
cout << size<<endl;
x = 0;
y++;
}
and it shows
5
#5
##5
###5
####5
Could anyone help me how to fill the rest part to #.
I tried setfill but it doesnt work that way.
Thanks a lot.
Learn to use code tags when posting in the forum. See <> to the right when you are writing your message. It will preserve your indentation.
If you use consistent indentation, your problem may become clearer. But it is not guaranteed. :-)
I played around with your exercise. I chose to use row and col instead of y and x, respectively. It made it clearer to me what I was trying to do.
You have used two while loops. This, of course, can work. I think this code would be clearer using for loops. Have you studied for loops yet?
Your problem is where are you trying to write your end of line. When do you want to execute cout << endl;? (Answer in words and not in code.)
Have you studied writing functions yet? In my version, I chose to write two functions: void displaySquare(int size) and void displayRow(int row, int size). Each was thus small and compact. It helped me to decide where to output the end of line.
Do you mean goto function? That's(goto) actually discourage in programming applications because(mainly) it makes the code harder to debug and analyze. The other option would be to use recursive or for loops. But he'll get into those as he advances though. :)