# I need some help with some Psuedocode?

#### Äß§ý∩†H♠HäŽ€

##### Fully Optimized
Okay I have a problem that I'm trying to figure out and its probably really simple and I'm just misunderstanding it. I've got a scenario that I've got to write some code for and I'm kind of stuck.

Scenario

A analyst wants a report to tally retirement benefits. It should show 5 values for each client. The report assumes the client has invested \$5,000 a year for 5,10,15,20 and 25 years and earn a rate of 10% annually.

I've got the main module setup which has a houseKeeping(), mainLoop() and finishUP() modules. I've also completed the houseKeeping() calling the files and reading the records.

The mainLoop() module is what I'm having problems with. The mainLoop() calls the headings() then needs to be looped 5 times until the last year (25) has been exceeded with an increment of 5.

So I know I need to assign a value of (5) to the counter and exceed it. How exactly should I write the code for this? Within this loop it also wants me to call the calcSavings() which will do the calculations for the invest totals through the 5 different years listed above. I need it to initialize a counter and an accumulator before entering a loop, then loop the module 5 times. The loop needs to calculate the return on investment while adding the investment per year and increment the counter. It then prints the years and totals.

Anyone decent with code that could help me out here?

#### berry120

##### Fully Optimized
Well in pseudo-code you can really do things however you like, as long as it's clear what it means to a human reader! I'd probably do it something like this:

int years := 5;
while (years<=25) {
int savings := calcSavings(years); //calculate the savings, giving it the number of years to calculate
print savings;
years += 5;
}

Would that be something like it or have I misunderstood?

#### Äß§ý∩†H♠HäŽ€

##### Fully Optimized
Okay I think I understand how your writing this out. For some reason I was trying to put the calculations in a different way. So I think I'm getting it now though.

For example with the declared variables I had selected.

int numberOfYears = 5
while numberOfYears <= 25
....int balanceYear = (yearlyInvestment * INTREST_RATE) + yearlyInvestment
print balanceYear
....numberOfYears + 5
endwhile

I still think I'm missing something here. numberOfYears is the counter and balanceYear is the accumulator correct? I need the balanceYear to accumulate 5 times in the code and add to the counter.

Sorry if I'm confusing but its kind of weird to try and explain. I think your example was pretty helpful though.

#### berry120

##### Fully Optimized

int balanceYear = (yearlyInvestment * INTREST_RATE) + yearlyInvestment
Don't you want something like:
int balanceYear = ((yearlyInvestment * INTREST_RATE) + yearlyInvestment)*numberOfYears

?

#### Äß§ý∩†H♠HäŽ€

##### Fully Optimized
I don't think it would work like that would it?

The way you have it this would be the output.

yearlyInvestment (5,000) * INTREST_RATE (.10) = 500 for the first year + yearlyInvestment (5,000) = 5,500 * numberOfYears (5) = 27,500.

It needs to increase the count and loop the balanceYear because the interest is accumulated annually. That would be wrong for the end output wouldn't it?

#### berry120

##### Fully Optimized
Ah yes, it seems I misunderstood the problem - you're talking about compound interest with a 5000 sum added every year yes?

int YEARLY_INVESTMENT = 5000;
float INTEREST_RATE = 0.1;

void main()
...int years = 5;
...while(years<=25)
......print valueAt(years);
......years += 5;

int valueAt(int years)
...int sum = 0;
...for(i=0 ; i<years ; i++)
......sum += YEARLY_INVESTMENT;
......sum *= 1+INTEREST_RATE;

Untested and I'm tired so may be making silly mistakes, but I think that should be the gist of it. Feel free to disagree if you think I've done something wrong!

#### Äß§ý∩†H♠HäŽ€

##### Fully Optimized
Nah it looks like your example should work out good. I think I figured out how to do it though. I ended up doing it this way. Thanks again for your input.

mainLoop()
numberOfYears = 5
...while numberOfYears <= 25
.......perform calcSavings()
...numberOfYears = numberOfYears + 5
endwhile
return
calcSavings()
years = 1
yearlyInvestment = 5000
while years <= numberOfYears
...balanceYear = (yearlyInvestment * INTREST_RATE) + yearlyInvestment
years = years + 1
endwhile

balance5Years = years + 5 balanceYear
balance10Years = years + 10 balanceYear
balance15Years = years + 15 balanceYear
balance20Years = years + 20 blanceYear
balance25Years = years + 25 blanceYear

print balance5Years, balance10Years, balance15Years, balance20Years, balance25Years

return

#### berry120

##### Fully Optimized
Hmm, I don't think your example is quite right, what's the while loop in calcSavings meant to do? At the moment it just seems to be setting the same variable (balanceYear) to the same value 25 times!