Computer Science Canada

christmas tree problem

Author:  junkpro11 [ Tue May 04, 2004 4:17 pm ]
Post subject:  christmas tree problem

lol does anyone know how to make a christmas tree like this?
----*
---***
--*****
-*******

ignore the dashes....

thanks

Author:  Cervantes [ Tue May 04, 2004 4:21 pm ]
Post subject: 

ya, read up on the "put" command.

here's what the code will look like:
code:

put "   *"
put "  ***"
put " *****"
put "*******"

Author:  junkpro11 [ Tue May 04, 2004 4:25 pm ]
Post subject: 

sorri i wasnt clear
i dont want to just use "put"

i want to get a number, eg like 5 and make a christmas tree like this:
--*
-***
*****

again ignore the dashes

Author:  Cervantes [ Tue May 04, 2004 4:39 pm ]
Post subject: 

oh, okay thats a pretty good question actually. simplest way is to use repeat and a for loop and play around with it until you get it Smile

heres what i came up with

code:

var base : int
put "Enter base width : " ..
get base

for t : 1 .. base

    put repeat (" ", base - t) ..
    put repeat ("*", t * 2 - 1)

end for

Author:  junkpro11 [ Tue May 04, 2004 4:48 pm ]
Post subject: 

thanks for ur help!!!!!!

i need to make a circle now with the asterisks...i think the closest thing i can make is an octagon though......so can i use repeat for making a "circle"?

Author:  Cervantes [ Tue May 04, 2004 5:24 pm ]
Post subject: 

interesting problem.
heres what i came up with:
code:

var radius : int
put "enter radius : " ..
get radius
var x, y : real
x := -radius - 1

for c : 1 .. radius * 2 + 1

    x += 1
    y := sqrt (radius ** 2 - x ** 2)
    locate (round (x) + maxrow div 2, round (-y) + maxcol div 2)
    put "*"
    locate (round (x) + maxrow div 2, round (y) + maxcol div 2)
    put "*"


end for


unfortunately in text the width of a character is less than its height, so it looks very, very off. also it looks off because locate requires rounding.

Author:  Paul [ Tue May 04, 2004 5:51 pm ]
Post subject: 

Hey, if u enter 5, it looks like a rupee in Zelda!

Author:  junkpro11 [ Tue May 04, 2004 7:32 pm ]
Post subject: 

um....sorri if this sounds dumb.....but can u explain the code u did cuz i am new at this and i duno wat that code meant.... Embarassed

Author:  Cervantes [ Tue May 04, 2004 7:40 pm ]
Post subject: 

i don't really know what it means either Confused I basically played around altering and fixing stuff until i came up with that.
but basically, i used the equation of a circle. thats x^2 + y^2 = r^2. rearange that so that we get y^2 = r^2 - x^2, then sqrt it all so we get y = sqrt (r^2 - x^2). so now we've got x and y values (got x because we are just increasing x every time). so now locate at (x,y) and draw a *. I've got 2 locates and 2 puts. look what happens when you take one of them out. and as for the for loop being 1 .. radius * 2 + 1, look what happens when you take off the + 1. look what happens when you take off the *2.

this is how i program Smile try something, fix it, try more, fix more, until i get it to work. Then i know how to do it, and if i have the energy i rewrite the code nice and organized and add any features i want. Very Happy

Author:  junkpro11 [ Tue May 04, 2004 8:11 pm ]
Post subject: 

wow.....well im a slow processor so i'll think about it for a while....thanks alot for the help!


: