Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 loop and variable help needed please
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
4sak3nX




PostPosted: Wed Feb 24, 2010 2:45 pm   Post subject: loop and variable help needed please

So i am trying to do a little thing for my gr.10 programming course. The instructions tell me to do the following... (copied and pasted so its exact wording)
**Draw the input-processing-output table and write the program to solve the following problem.
Calculate the amount in a bank account which is earning 5% interest, after a number of years. First ask for the amount initially deposited in the bank account, then ask for the number of years the money is to be left in the bank. Calculate the amount in the bank at the end using the equation
initial amount x 1.05 raised to the power of the number of years

Display a sentence giving the final amount in the bank.**

So the only way i can think to do the exponent is to do a loop, but I am unsure of how to do it since it is to a variable?
Does anybody know a better way to do this or can help me out with actually getting it to loop?

(i checked with my teacher and i wasnt sure what he was saying...)

The code is below...


var number1 : real
var number2 : real
var answer : real

put "How much money is in the bank to start? " ..
get number1

put "How long is the money going to sit? " ..
get number2

answer := number1 * 1.05

put "After"
put number2
put "years, there will be"
put answer
put "in the bank."
Sponsor
Sponsor
Sponsor
sponsor
TerranceN




PostPosted: Wed Feb 24, 2010 3:38 pm   Post subject: RE:loop and variable help needed please

Turing has this built in as the operator **. For example:

Turing:
put 5 ** 3 % prints 5 to the power of 3 which is 5 * 5 * 5 which is 125


Hope that helps.
USEC_OFFICER




PostPosted: Wed Feb 24, 2010 5:02 pm   Post subject: RE:loop and variable help needed please

loops can be done with

loop

end loop

but that is probably not what you are looking for. Perhaps look through the Turing help files?
ProgrammingFun




PostPosted: Wed Feb 24, 2010 7:33 pm   Post subject: Re: loop and variable help needed please

You can do everything in the loop except for declaring the variables:

Turing:

var number1 : real
var number2 : real
var answer : real

loop  %start loop here

put "How much money is in the bank to start? " ..
get number1

put "How long is the money going to sit? " ..
get number2

answer := number1 * 1.05

put "After"
put number2
put "years, there will be"
put answer
put "in the bank."
cls % to clear the screen so the user can do this all over again

end loop  % end loop here


Hope this helps.
TerranceN




PostPosted: Wed Feb 24, 2010 8:19 pm   Post subject: RE:loop and variable help needed please

Are we reading the same question?

"So the only way i can think to do the exponent is to do a loop, but I am unsure of how to do it since it is to a variable?
Does anybody know a better way to do this or can help me out with actually getting it to loop?"

AFAICT, thats asking how to do exponents in turing, not asking how to get the program to run more than once.

@ProgrammingFun:
FYI, the variables can be declared inside the loop, because their scope is only to the end loop, but they will be reallocated each time the program goes around the loop.

Also, the user needs a way to see the end result before the next loop, so an Input.Pause (or something similar) would be needed before the cls.
ProgrammingFun




PostPosted: Wed Feb 24, 2010 9:36 pm   Post subject: RE:loop and variable help needed please

My bad.
You could also use delay before cls...I was just giving a general idea.
And for exponents, what TerranceN said Very Happy
4sak3nX




PostPosted: Thu Feb 25, 2010 10:25 am   Post subject: RE:loop and variable help needed please

Thanks guys it worked! Smile

Umm the exponent thing that TerranceN suggested at the beginning was just what i needed!

Im new to turing so there will most likely be more posts by me needing help Sad

Anyway I guess this thread can be closed!

Thanks again for the help!
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: