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.
Simple C question *shape* (Diamond with an empty box inside)
- NuclearSilo
- Forum God
- Posts: 8834
- Joined: Mon Aug 21, 2006 12:00 pm
- Quick Reply: Yes
- Location: Age of Wushu
Re: Simple C question *shape* (Diamond with an empty box inside)
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]
- 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]
Playing Age of Wushu, dota IMBA
- NuclearSilo
- Forum God
- Posts: 8834
- Joined: Mon Aug 21, 2006 12:00 pm
- Quick Reply: Yes
- Location: Age of Wushu
Re: Simple C question *shape* (Diamond with an empty box inside)
nevermind about what I said.
check if the current point is inside the zone [(n+1)/2 ; 2(n-1) - (n+1)/2], if yes then do not print it
check if the current point is inside the zone [(n+1)/2 ; 2(n-1) - (n+1)/2], if yes then do not print it
Playing Age of Wushu, dota IMBA
Re: Simple C question *shape* (Diamond with an empty box inside)
I'm at school, I'll edit this post when I get home in 1.5 hrs with some concrete code, hopefully that'll help.
Re: Simple C question *shape* (Diamond with an empty box inside)
http://codepad.org/e0L2crAN
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.
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.
- NuclearSilo
- Forum God
- Posts: 8834
- Joined: Mon Aug 21, 2006 12:00 pm
- Quick Reply: Yes
- Location: Age of Wushu
Re: Simple C question *shape* (Diamond with an empty box inside)
http://codepad.org/vUSSC2TY
Replace fabs for optimization, but it's less understandable than the first
http://codepad.org/N4I72K7C
Spoiler!
Replace fabs for optimization, but it's less understandable than the first
http://codepad.org/N4I72K7C
Playing Age of Wushu, dota IMBA
Re: Simple C question *shape* (Diamond with an empty box inside)
He can't use if statements nuke :/ That's why my code looks retarded lol.
Re: Simple C question *shape* (Diamond with an empty box inside)
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.