Computer Forums I need some help with some Psuedocode?

 08-12-2009, 01:43 PM #1 Fully Optimized   Join Date: Nov 2006 Posts: 3,629 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? __________________ __________________
 08-12-2009, 02:08 PM #2 Site Team     Join Date: Jul 2009 Location: England, UK Posts: 3,428 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? __________________ __________________
 08-12-2009, 02:30 PM #3 Fully Optimized   Join Date: Nov 2006 Posts: 3,629 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. __________________
08-12-2009, 04:09 PM   #4
Site Team

Join Date: Jul 2009
Location: England, UK
Posts: 3,428
Re: I need some help with some Psuedocode?

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

?
__________________

 08-12-2009, 05:05 PM #5 Fully Optimized   Join Date: Nov 2006 Posts: 3,629 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? __________________
 08-12-2009, 05:29 PM #6 Site Team     Join Date: Jul 2009 Location: England, UK Posts: 3,428 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
 08-12-2009, 09:06 PM #7 Fully Optimized   Join Date: Nov 2006 Posts: 3,629 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 __________________
 08-13-2009, 04:31 AM #8 Site Team     Join Date: Jul 2009 Location: England, UK Posts: 3,428 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! __________________ __________________