Computer Science Canada

Turing wont calculate the new total if added again+

Author:  Stickman [ Mon Mar 07, 2022 10:10 am ]
Post subject:  Turing wont calculate the new total if added again+

Im having issue with the program and it keeps showing the same error every time and it not adding the new total

Expected result when:
Every time the command is run again by the loop it should add a new number to it.

the cause:
total := investment * intrate
the program keeps showing the same results.

i tried to fix it but it not working, this is for a assignment I have to hand in cause I need to catch up on the work.

Turing script:

%folder 0: note

%folder 1: var
var year: int
var value: real
var money: real
var investment: real
var total: real
var intrate: real
money := 0
intrate := 0
investment := 0
year := 0
%folder 2: script
put "whats your investment"
get investment
put "what your intrest rate"
get intrate
put "how much money do you want to have"
get money

loop
total := investment * intrate
year := year + 1

put "year:", year
put "total:", total
exit when total = money
end loop

Author:  scholarlytutor [ Thu Mar 10, 2022 5:10 pm ]
Post subject:  Re: Turing wont calculate the new total if added again+

Hi Stickman,

The code is mostly good, but there are a few of problems

The first problem is that your number stays the same each year. That's because your 'investment' variable never changes. Delete the total variable because you don't need it. Keep adding the value to 'investment' instead.

The second problem is with your maths. So let's say we invest $200 at 5% interest. So after a year that is $215. To get that, you need to multiply $200 by 0.05:

investment += investment * (intrate/100)

Finally, to exit the loop, you exit when investment is >= money, not equal to, because most of the time the investment will not equal exactly the amount of money a person wants to have.


Here's the final code:

loop
investment += investment * (intrate / 100)
year := year + 1

put "year:", year
put "total:", investment : 0 : 2
exit when investment >= money
delay (1000)
end loop

The : 0 : 2 beside investment will round it to two decimal places so that it looks better. Also, the delay (1000) will wait 1 second so that you can see the results in slow motion.

Hope that helps!


: