Posted: Fri Dec 12, 2008 8:39 pm Post subject: making a diamond with *
hi , i am making a diamond with * (stars) and i did it after like 2 days of trying . but my code seems long and stupid, (programmer since 1 month, turing that is , i know html and basic) so here is the code, i was wondering if anyone could shorten it for me , if shortening it IS possible.
code:
var nlines : int
var spaces : int
var stars : int
put "enter number of lines!"
get nlines
cls
put " "
so the point of the program is asking how many lines the use would like, and then making a diamond based on that number of lines (twice that number of lines)
Sponsor Sponsor
ecookman
Posted: Fri Dec 12, 2008 8:50 pm Post subject: Re: making a diamond with *
i don't think you can make the program shorter... turing is a horrible language to use
you need long complex programs to do simple things
as an example run this
Turing:
var playerhp, monsterhp :int var playerattk, monsterattk :int var playerdef: int var onetwo :int var monsterchoice: int
put"press 1 to attack or press 2 to defend" get onetwo
if onetwo =1then %player attacks put"you hit a ",playerattk
monsterhp := monsterhp-playerattk
%monster decides what to do - attack or defend if monsterchoice =1then put"the monster attked and hit a ",monsterattk
playerhp := playerhp-monsterattk
endif endif
if onetwo =2then %player defends put"your defence is ",playerdef
playerhp:=playerhp+playerdef
%monster decides what to do - attack or defend if monsterchoice= 1then put"the monster attked and hit a ",monsterattk
playerhp := playerhp-monsterattk
my program is not the final product. but it is the base if it didn't need to be shortened can't be shortened. Only 1 way to really to do something in turing
dc116
Posted: Fri Dec 12, 2008 8:55 pm Post subject: Re: making a diamond with *
You can definitely make it shorter. Here's my version, using counters.
code:
var nlines,counter,spaces : int
put "enter number of lines!"
get nlines
cls
put ""
counter:= 0
spaces:=nlines
loop
counter:= counter + 1
spaces:= spaces - 1
put repeat(" ",spaces)..
put repeat("*",2*counter-1)
exit when counter=nlines
end loop
loop
counter:= counter - 1
spaces:= spaces + 1
put repeat(" ",spaces)..
put repeat("*",2*counter-1)
exit when counter=0
end loop
ecookman
Posted: Fri Dec 12, 2008 9:05 pm Post subject: RE:making a diamond with *
well didn't really make it shorter
just wrote another program that is probably beyond his knowledge if he is just starting...
here at compsci we like to help people get the right answers not give it to them.
if you examine his program it uses effective but simple for loops, which is in his area knowledge and understanding of he program
next time try to point them to a tutorial that shows them how to use something or break down your code and show them how it works not *slap* done here you go. The point that i am getting at is teach someone how to do something not do it for them.
-------------------------------------------------------
you are probably thinking that what i said was wrong. if he was looking for a alternative way to write his program i was wrong, but what he asked for is exactly what i gave. if what HE wrote can be shortened
dc116
Posted: Fri Dec 12, 2008 9:10 pm Post subject: RE:making a diamond with *
Thanks for the advice, I'll keep that in mind next time.
ecookman
Posted: Fri Dec 12, 2008 9:12 pm Post subject: RE:making a diamond with *
no problem i got sooo many warning due to that. i think like 3...just saving you from bannage
kousha41564
Posted: Sat Dec 13, 2008 11:00 pm Post subject: RE:making a diamond with *
dc16, what you wrote seems so genius to me but i have no idea whats going on in your program, i would love if you could break it down for me telling me how it works. (i know counters, and such, i just dont understand the part where you used repeat . )
and if i actualy wrote my program effeciently then i am proud of myself thank you! lol
and ecooman thats a nice game man , if you can you should try incorperating some graphics
TheGuardian001
Posted: Sun Dec 14, 2008 12:29 am Post subject: Re: making a diamond with *
the key thing you would need to use is the locate statement
code:
locate(row,column)
locate moves the text output to the specified row and column.
for example, a vertical line 5 columns from the right side would be
Turing:
locate(10,5)%10 rows down, 5 columns over put"*" locate(11,5)%11 rows down, 5 columns over put"*" locate(12,5)%12 rows down, 5 columns over put"*" locate(13,5)%13 rows down, 5 columns over put"*" locate(14,5)%14 rows down, 5 columns over put"*"
Sponsor Sponsor
Tony
Posted: Sun Dec 14, 2008 2:19 am Post subject: Re: making a diamond with *
ecookman @ Fri Dec 12, 2008 8:50 pm wrote:
i don't think you can make the program shorter... turing is a horrible language to use
Since Turing is such a horrible language to use, I challenge you come up with a shorter solution in any other language. Your baseline? 6 lines
Turing:
var lines :int put"enter number of lines!" get lines
for i: 1.. lines *2 - 1 putrepeat(" ",ceil((1 + 2*(lines-1) / 2)) - ceil((1 + 2*(i + 2*(i - lines)*round((lines - i + 0.00001) / abs(lines - i + 0.00001) / 2 - 0.5) - 1))/2)) + repeat("*",1 + 2*(i + 2*(i - lines)*round((lines - i + 0.00001) / abs(lines - i + 0.00001) / 2 - 0.5) - 1)) endfor
Posted: Sun Dec 14, 2008 11:06 am Post subject: RE:making a diamond with *
i don't know enough about other languages to do that sorry...
i just know i hate turing and it is a pain to code anything
DanielG
Posted: Sun Dec 14, 2008 11:17 am Post subject: RE:making a diamond with *
Turing has many flaws, but amount of code needed to write an avergae program is not one of them.
kousha41564
Posted: Sun Dec 14, 2008 12:13 pm Post subject: RE:making a diamond with *
tony, holy crap ! 6 lines, make me feel like a retard lol
Tony
Posted: Sun Dec 14, 2008 4:45 pm Post subject: RE:making a diamond with *
@ecookman -- other languages will hardly make drawing ASCII patterns any easier for you. I'm not sure how you've decided that Turing is so much worse and so much more "painful" than other languages, if you don't know enough about other languages to use them.
@kousha41564 -- don't be. I'd take your solution over my own, as it's much easier to follow, understand, and maintain.
Posted: Sun Dec 14, 2008 6:21 pm Post subject: RE:making a diamond with *
yay lol
dc116
Posted: Sun Dec 14, 2008 6:28 pm Post subject: Re: RE:making a diamond with *
kousha41564 @ Sun Dec 14, 2008 12:13 pm wrote:
tony, holy crap ! 6 lines, make me feel like a retard lol
Ahh same here
Here's the breakdown of my version, as requested.
code:
% Declares variables
var nlines,counter,spaces : int
put "enter number of lines!"
get nlines
cls
put ""
% counter is basically the number of row (eg. row one)
counter:= 0
% spaces basically "aligns" the diamond (or making the asterisks into the shape of the diamond)
spaces:=nlines
%Draws the upper part of the diamond.
loop
% the number of rows increases
counter:= counter + 1
% As it increase, there are more asterisks, which results in less spaces
spaces:= spaces - 1
% here the number of spaces is controlled using repeat.
put repeat(" ",spaces)..
% here the number of asterisks is controlled using repeat, using the formula (sequence number * 2 - 1)
put repeat("*",2*counter-1)
exit when counter=nlines
end loop
%Draws the lower part.
%Basically my way of reflecting the upper part on a horizontal line.
loop
counter:= counter - 1
spaces:= spaces + 1
put repeat(" ",spaces)..
put repeat("*",2*counter-1)
exit when counter=0
end loop