Computer Science Canada

For Loops Help

Author:  jolly50 [ Sat Oct 27, 2007 6:04 pm ]
Post subject:  For Loops Help

Hi,

I'm really new with turing and I'm trying to get some of my homework done, but I'm having problems with the one question...

Here is the question:
"Write a program to compute the bank balance at the end of each year for 10 years resulting from an initial deposit of $1000 and an annual interest rate of 6%. Output for each year end the number of the year, the initial balance, the interest for the year, and the balance at the end of the year."

What I have so far:

code:

var total, interest, endOfyear: real
var initialdeposit : int

total:= 0


put "What is your initial deposit? "..
get initialdeposit


endOfyear:= initialdeposit*1.06
interest:=initialdeposit*0.06

for i: 1..10
    put i,". ", endOfyear
    total:=total+endOfyear+interest

   
end for


I have only worked on the first part with the for loops...my problem is that I don't know how to make each line add the 6% of the initial deposit....

I'm not asking you to write my homework for me.....I'm asking you for help to fix my loop....just to be clear

Author:  Nick [ Sat Oct 27, 2007 8:05 pm ]
Post subject:  RE:For Loops Help

Turing:
for i: 1..10
    put i,". ", endOfyear
    total:=total+endOfyear+interest

   
end for


theres your problem...
you are outputting the endOfyear instead of your total

also you are declaring total after you output anything

heres how it should look

Turing:
for i: 1..10
    total:=total+endOfyear+interest
    put i,". ", total
end for

Author:  jolly50 [ Sun Oct 28, 2007 10:53 am ]
Post subject:  Re: For Loops Help

ok...i'm close to finishing it, but there is still one more problem.

I fixed the problem i had and i moved around the variables but my output is still wrong.

code:

var total, interest, endOfyear: real
var initialdeposit : int

total:= 0


put "What is your initial deposit? "..
get initialdeposit


endOfyear:= initialdeposit*1.06
interest:=initialdeposit*0.06

for i: 1..10
    total:=total+initialdeposit+interest
    put i,". ", total
end for



but my output should be something like

1. 1060
2. 1120
3. 1180
..etc


instead i'm getting

1. 1060
2. 2120
3. 3180
4. 4240
...etc

I think the problem is with my "total:=total+...

Thanks for your help

Author:  rdrake [ Sun Oct 28, 2007 11:29 am ]
Post subject:  RE:For Loops Help

You're adding the initial deposit to the result each time. Hence why your output is an extra $1000 each time.

Author:  Nick [ Sun Oct 28, 2007 12:11 pm ]
Post subject:  RE:For Loops Help

rdrake is right so instead of adding the total in the loop add it to the total before hand so...

Turing:
var total, interest, endOfyear: real
var initialdeposit : int

total:= 0


put "What is your initial deposit? "..
get initialdeposit


endOfyear:= initialdeposit*1.06
interest:=initialdeposit*0.06

total:=intialdeposit

for i: 1..10
    total:=total+interest
    put i,". ", total
end for

Author:  jolly50 [ Mon Oct 29, 2007 7:44 pm ]
Post subject:  RE:For Loops Help

Thanks...it worked perfectly


: