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?