Go Back   Computer Forums > General Computing > Hardware
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 08-12-2009, 02:43 PM   #1
Fully Optimized
 
Join Date: Nov 2006
Posts: 3,629
Send a message via AIM to ߧ∩H♠H䎀
Default 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.

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?
__________________

ߧ∩H♠H䎀 is offline   Reply With Quote
Old 08-12-2009, 03:08 PM   #2
Site Team
 
berry120's Avatar
 
Join Date: Jul 2009
Location: England, UK
Posts: 3,425
Default Re: I need some help with some Psuedocode?

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?
__________________

berry120 is offline   Reply With Quote
Old 08-12-2009, 03:30 PM   #3
Fully Optimized
 
Join Date: Nov 2006
Posts: 3,629
Send a message via AIM to ߧ∩H♠H䎀
Default Re: I need some help with some Psuedocode?

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.
ߧ∩H♠H䎀 is offline   Reply With Quote
Old 08-12-2009, 05:09 PM   #4
Site Team
 
berry120's Avatar
 
Join Date: Jul 2009
Location: England, UK
Posts: 3,425
Default Re: I need some help with some Psuedocode?

Glad to help

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

?
berry120 is offline   Reply With Quote
Old 08-12-2009, 06:05 PM   #5
Fully Optimized
 
Join Date: Nov 2006
Posts: 3,629
Send a message via AIM to ߧ∩H♠H䎀
Default Re: I need some help with some Psuedocode?

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?
ߧ∩H♠H䎀 is offline   Reply With Quote
Old 08-12-2009, 06:29 PM   #6
Site Team
 
berry120's Avatar
 
Join Date: Jul 2009
Location: England, UK
Posts: 3,425
Default Re: I need some help with some Psuedocode?

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:

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!
berry120 is offline   Reply With Quote
Old 08-12-2009, 10:06 PM   #7
Fully Optimized
 
Join Date: Nov 2006
Posts: 3,629
Send a message via AIM to ߧ∩H♠H䎀
Default Re: I need some help with some Psuedocode?

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()
perform headings()
numberOfYears = 5
...while numberOfYears <= 25
.......perform calcSavings()
...numberOfYears = numberOfYears + 5
endwhile
read clientFiles
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
ߧ∩H♠H䎀 is offline   Reply With Quote
Old 08-13-2009, 05:31 AM   #8
Site Team
 
berry120's Avatar
 
Join Date: Jul 2009
Location: England, UK
Posts: 3,425
Default Re: I need some help with some Psuedocode?

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!
__________________

berry120 is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 10:16 PM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0