Page 1 of 1

Re: Simple C question *shape* (Diamond with an empty box inside)

Posted: Thu Nov 10, 2011 10:38 pm
by SM-Count
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.

Re: Simple C question *shape* (Diamond with an empty box inside)

Posted: Fri Nov 11, 2011 12:18 am
by NuclearSilo
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]

Re: Simple C question *shape* (Diamond with an empty box inside)

Posted: Fri Nov 11, 2011 2:02 am
by NuclearSilo
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

Re: Simple C question *shape* (Diamond with an empty box inside)

Posted: Fri Nov 11, 2011 3:27 am
by SM-Count
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)

Posted: Fri Nov 11, 2011 5:13 am
by SM-Count
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.

Re: Simple C question *shape* (Diamond with an empty box inside)

Posted: Fri Nov 11, 2011 11:57 am
by NuclearSilo
http://codepad.org/vUSSC2TY

Spoiler!


Replace fabs for optimization, but it's less understandable than the first
http://codepad.org/N4I72K7C

Re: Simple C question *shape* (Diamond with an empty box inside)

Posted: Fri Nov 11, 2011 12:48 pm
by SM-Count
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)

Posted: Fri Nov 11, 2011 1:04 pm
by SM-Count
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.