Computer Science Canada

THe Program works but getting WAY TOO LONG!!!! Is there any way to shorten it.?

Author:  naqibaba [ Thu Dec 03, 2009 10:21 pm ]
Post subject:  THe Program works but getting WAY TOO LONG!!!! Is there any way to shorten it.?

In this program I am having to quiz a user about the capital city of each province in Canada. The Program should allow the user only 3 guesses only.

The program is getting way too long if I do it my way. THIS IS JUST FOR CAPITAL OF ONTARIO. WATCH.



var guess1, guess2, guess3 : string

loop
put " What is the capital city of Ontario. "
get guess1
if guess1 = "Toronto" or guess1 = "toronto" then
put " Correct answer. "
exit
else
put " Wrong."
end if
put " "
put " What is the capital city of Ontario. "
get guess2
if guess2 = "Toronto" or guess2 = "toronto" then
put " Correct answer. "
exit
else
put " Wrong."
end if
put " "
put " What is the capital city of Ontario. "
get guess3
if guess3 = "Toronto" or guess3 = "toronto" then
put " Correct answer."
exit
else
put " Wrong. No more Try Outs. Bye Bye."
exit
end if
end loop



Is there any way I can shorten it so I can do the other 10 provinces in a short amount of time. I would appreciate any kind of help.

Author:  Tony [ Thu Dec 03, 2009 10:33 pm ]
Post subject:  RE:THe Program works but getting WAY TOO LONG!!!! Is there any way to shorten it.?

code:

var guess1, guess2, guess3 : string

see arrays. Though you don't really need to keep all 3 answers anyway, so one variable should be enough.

code:

if guess1 = "Toronto" or guess1 = "toronto" then

Normalize the capitalization.
code:

if to_upper_case(guess) = "TORONTO" then

You are repeating too much code
code:

put " What is the capital city of Ontario. "
...
put " What is the capital city of Ontario. "

Use loops.


If you think of copy-pasting the code 10 times, for all the provinces, then you are repeating code. Think about a way of using generic code that can ask about any province, and put _that_ inside a loop that runs 10 times.

(loading data from arrays or files is a typical choice).

Author:  naqibaba [ Thu Dec 03, 2009 10:44 pm ]
Post subject:  Re: THe Program works but getting WAY TOO LONG!!!! Is there any way to shorten it.?

I just saw it and I dont think that its of my level. Can there be an example just to make it easy?

Author:  Tony [ Thu Dec 03, 2009 10:51 pm ]
Post subject:  RE:THe Program works but getting WAY TOO LONG!!!! Is there any way to shorten it.?

You can raise your level with The Turing Walkthrough

Author:  Kharybdis [ Thu Dec 03, 2009 11:30 pm ]
Post subject:  RE:THe Program works but getting WAY TOO LONG!!!! Is there any way to shorten it.?

Try starting with how to use for loops and loops before you get into arrays and records though.

Author:  Murphman [ Fri Dec 04, 2009 1:54 pm ]
Post subject:  Re: THe Program works but getting WAY TOO LONG!!!! Is there any way to shorten it.?

You can shorten your code by alot. You only need to ask the question once in the loop.
Make a Variable called count and declare it as a int,

var count : int

Now make the count equal 0 out side of the loop

count := 0

now i am gonna let you figure the rest out on your own. this is all i will tell you. You need an exit when, and you also need to make count a counter somewhere in the if.

exit when count = 3

count := count + 1 <--- this will add 1 to count everytime the loop is ran[/list]

Author:  mirhagk [ Sat Dec 05, 2009 8:59 pm ]
Post subject:  RE:THe Program works but getting WAY TOO LONG!!!! Is there any way to shorten it.?

also count+=1 does the same thing as count:=count +1.

and you should really look into arrays becuase you could just make 10 string variables and cycle through them all to ask all 10 questions or w/e

Author:  naqibaba [ Mon Dec 07, 2009 8:15 pm ]
Post subject:  Re: THe Program works but getting WAY TOO LONG!!!! Is there any way to shorten it.?

why cant anybody just tell me how to do it. I am newbe in turing.

Author:  Tony [ Mon Dec 07, 2009 8:35 pm ]
Post subject:  RE:THe Program works but getting WAY TOO LONG!!!! Is there any way to shorten it.?

Tell you how to do what?

Author:  Superskull85 [ Mon Dec 07, 2009 10:56 pm ]
Post subject:  RE:THe Program works but getting WAY TOO LONG!!!! Is there any way to shorten it.?

Because you need to be able to do the work on your own. You don't learn much if people just give you the solution without studying it.


: