d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Interest Rate Table > Need Some Help
Add Reply New Topic New Poll
Member
Posts: 4,780
Joined: Jun 24 2012
Gold: 3,189.00
Feb 11 2020 08:27pm
I’m trying to create a program to calculate how much money is accumulated depending on interest rates and I’m having difficulty figuring out how to make it work. Basically I’m creating a 2d array where each column represents interest rate from 0-10%. I ask for an initial amount and each row I add annual investment until it reaches a certain age. The problem is, I have no clue how I’m supposed to carry over previous year’s amount to the current row.

My for loop looks as follows(it’s a rough draft that I planned to clean up after figuring out the calculations so bear with me):

for (int i=0; i < table.length; i++)
{
for (int j=0; j < table.length; j++)
{
if (i==0)
{
table[j] = initialAmt;
}
else
{
value= (int) (initialAmt * (1*interestRate)) + annualInvest;
interestRate += 0.01;
table[j] = value;
}
}
interestRate = 0.0;
initialAmt += annualInvest;
}

The value that populates the array is different than the expected value and I’m not sure what I’m doing wrong. Can someone shed me some light?
Member
Posts: 1,039
Joined: Jul 8 2008
Gold: 1,939.50
Feb 12 2020 07:04pm
Writing out how you're going to accomplish what you want to do before coding it will help. I often put comments into my code before writing the implementation.

Loop within a loop stuff is kind of hard to explain via text, but here are some comments:

I don't think increasing your "initialAmt" is what you want to do on each iteration. This won't compound your interest.
You're never using i.
You are only using one dimension of your array.

You could probably quite easily code the following on your own:
1. Initialize all columns of row 0. (in your inner loop you will be setting arr[0][0 to j])
2. Fill in all columns of row 1 through i a row at a time
- Read the row above the one you're about to write to (read arr[0][0] if you're about to write to arr[1][0] ///// read arr[1][3] if you're going to write to arr[2][3])
- Applying your formula
- Write to array

This post was edited by waraholic on Feb 12 2020 07:07pm
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll