Re: Simple C question *shape* (Diamond with an empty box inside)
Posted: Thu Nov 10, 2011 10:38 pm
Since you're not allowed to have jump conditions, the easiest way to do it with the code you already have is to break it into 4 different for loops rather than the 2 that you have.
First thing you'd have to do is have a variable that tells you how many holes you need Max(N-2,0) or just n-2 if you can assume N has min 3.
Then you can do a bit of trickery to know when you need holes. Implement a counter variable that starts at zero and increments every time you print a *. Wrap your first loop in a while statement that runs until the counter variable is equal to N (i.e. the number of starts you printed is equal to N which starts the empty box).
The second/third part of the code is simple, it's basically what you have but with empty boxes, since you have a variable with the box height/width (n-2) modify your code so that you draw n-2 spaces after stars starting with 2 stars until you get to one above the center. Then draw the bottom part of the with the n-2 spaces in the middle.
Last part of the code is like the first part, wrap it in a while loop with a counter, exit the while loop when your counter = 1.
First thing you'd have to do is have a variable that tells you how many holes you need Max(N-2,0) or just n-2 if you can assume N has min 3.
Then you can do a bit of trickery to know when you need holes. Implement a counter variable that starts at zero and increments every time you print a *. Wrap your first loop in a while statement that runs until the counter variable is equal to N (i.e. the number of starts you printed is equal to N which starts the empty box).
The second/third part of the code is simple, it's basically what you have but with empty boxes, since you have a variable with the box height/width (n-2) modify your code so that you draw n-2 spaces after stars starting with 2 stars until you get to one above the center. Then draw the bottom part of the with the n-2 spaces in the middle.
Last part of the code is like the first part, wrap it in a while loop with a counter, exit the while loop when your counter = 1.