Please help

Nov 8, 2014 at 11:17pm
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.
Nov 8, 2014 at 11:42pm
while (x < size&&x<y)
{
cout << "#";
x++;
}cout << size<<endl;
x = 0;


why not try changing this to
while (x < sqsize)
{
if(x==y)
{cout << sqsize;}
else
{cout << "#";}
x++;
}
y++;
x = 0;
cout <<endl;
}

hope it helps. :)
--EDITED..sorry, forgot about that endl...-_-..(btw, i changed size to sqsize)
Last edited on Nov 9, 2014 at 3:11am
Nov 9, 2014 at 12:20am
thanks, but it doesn't work the way it suppose to be. still need to find other way.
Nov 9, 2014 at 12:58am
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.

Nov 10, 2014 at 11:42am
why not use
gotoxy function
?

if the size is 10, then it will mess up your squares. :)
Nov 10, 2014 at 2:01pm
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. :)
Last edited on Nov 10, 2014 at 2:02pm
Nov 11, 2014 at 5:23am
Yeah. Goto function is good at some point. :)

But I suggest using "\t" function as an alternate. :)
Topic archived. No new replies allowed.