I need some help with some Psuedocode?

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.


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?


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? :)
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

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.


Fully Optimized
Glad to help :)

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

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?


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

In which case, what about this:

float INTEREST_RATE = 0.1;

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

int valueAt(int years)
...int sum = 0;
...for(i=0 ; i<years ; i++)
......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! :)
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.

perform headings()
numberOfYears = 5
...while numberOfYears <= 25
.......perform calcSavings()
...numberOfYears = numberOfYears + 5
read clientFiles
years = 1
yearlyInvestment = 5000
while years <= numberOfYears
...balanceYear = (yearlyInvestment * INTREST_RATE) + yearlyInvestment
years = years + 1

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



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!