Tuesday, July 14, 2009

C# nested loops with hollow rectangles?

OK, so for my CS class, I need to make a program using nested loops and a SINGLE if...else statement, which asks the user for the length and height of the square, and then produces the following output using asteriks: (this would be a 5*5 square, but it needs to work with whatever they type in):





*****


* *


* *


* *


*****





i got the lesson before this, where we were asked to output this: (3*3 example):





***


***


***





and this is my loop:


for (int i = 0; i %26lt; height; i++)


{


for (int j = 0; j %26lt; length; j++)


{


Console.Write("*");


}


Console.WriteLine();


}


I cant seem to figure out how to incorporate the spaces...I can get spaces to show up, but I cant figure out how to make the number of spaces relate to the length the user inputted...C# keeps giving me an error that you cant use multiplication with operatents of string and int type. I was trying to tell it to insert spaces using something like ((length-2) * " ")


HELP! thanks =)

C# nested loops with hollow rectangles?
try this:





Console.Write("* ");





PUt the space after the star.





______


added later


-----------





Ohhh, HOLLOW squares...





my bad.
Reply:Use an If in the inner loop.





something liks this


if (*some code*){


Console.Write(" ");


}


else


{


Console.Write("*");


}





*some code* =


(i %26lt; 0 and i %26gt; height - 1) and (j %26gt;0 and J %26lt; length-1)
Reply:The only time you need to 'draw' a line would be at the top, bottom and side edges.. you know this would be true when i=0 or i= height-1 or when j=0 or j=length-1.


All other times you can just throw down a space.


(this would probably be your one 'if' condition)


hope this helps ;)

covent garden

No comments:

Post a Comment