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

Anything else. Post a funny site or tell us about yourself. Discuss current events or whatever else you want. Post off topic threads here.
Post Reply
User avatar
SM-Count
Ex-Staff
Posts: 2761
Joined: Sat Jan 12, 2008 7:02 pm
Quick Reply: Yes
Location: /wave

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

Post 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.

User avatar
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)

Post 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]
Playing Age of Wushu, dota IMBA

User avatar
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)

Post 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
Playing Age of Wushu, dota IMBA

User avatar
SM-Count
Ex-Staff
Posts: 2761
Joined: Sat Jan 12, 2008 7:02 pm
Quick Reply: Yes
Location: /wave

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

Post 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.

User avatar
SM-Count
Ex-Staff
Posts: 2761
Joined: Sat Jan 12, 2008 7:02 pm
Quick Reply: Yes
Location: /wave

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

Post 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.

User avatar
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)

Post by NuclearSilo »

http://codepad.org/vUSSC2TY

Spoiler!


Replace fabs for optimization, but it's less understandable than the first
http://codepad.org/N4I72K7C
Playing Age of Wushu, dota IMBA

User avatar
SM-Count
Ex-Staff
Posts: 2761
Joined: Sat Jan 12, 2008 7:02 pm
Quick Reply: Yes
Location: /wave

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

Post by SM-Count »

He can't use if statements nuke :/ That's why my code looks retarded lol.

User avatar
SM-Count
Ex-Staff
Posts: 2761
Joined: Sat Jan 12, 2008 7:02 pm
Quick Reply: Yes
Location: /wave

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

Post 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.

Post Reply

Return to “Off Topic Lounge”