Post subject: Re: Simple C question *shape* (Diamond with an empty box inside)
Posted: Thu Nov 10, 2011 10:38 pm
Ex-Staff
Joined: Jan 2008 Posts: 2761 Location: /wave
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.
Post subject: Re: Simple C question *shape* (Diamond with an empty box inside)
Posted: Fri Nov 11, 2011 12:18 am
Forum God
Joined: Aug 2006 Posts: 8834 Location: Age of Wushu
first analyse: - n = half size of the box - center index = [2;2] <=> [(n+1)/2;(n+1)/2] - this box is symmetric, you can create small task then just do the "mirror" - it's hard if you print directly the "*"
algorithm (not in C): - create an array of arrays size n*2-1 x n*2-1 (n=5 => size = 9) - fill all the arrays with "*" - fill the top-left corner a blank triangle
for (i=0; i<n-1; i++) { --rows for (i=0; j<n-i-1; i++) --column fill blank }
- repeat for all corner - fill the middle with blank where index range = 0 -> n-1 start position = [(n+1)/2 ; (n+1)/2 end position = [(n-1) - (n+1)/2 ; (n-1) - (n+1)/2]
Here you go, it's a shell for what you should be thinking of. Obviously it's more verbose than you need so you can change the var names to i/j/z/whatever but it should be pretty clear, if it's not you can im me or something, just shoot me a pm. I've obviously commented out the part you should be trying to do, but just follow my first code block example. You'll have to draw the bottom of the diamond as well.
Post subject: Re: Simple C question *shape* (Diamond with an empty box inside)
Posted: Fri Nov 11, 2011 1:04 pm
Ex-Staff
Joined: Jan 2008 Posts: 2761 Location: /wave
Oh, if you can use if statements use nuke's code, my code is downright retarded because I had to do a workaround for no ifs. fabs is just absolute value, just say you googled it.
Users browsing this forum: Google [Bot], Google Adsense [Bot] and 7 guests
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot post attachments in this forum