1

My problem is based on the compound interest formula. A = P(1 + r)^n. I have a number of values for r and n which I must store as an array. I also must output my range of final values A as an array also. I think I have stored r and n correctly as an array. However my problem is with the final values of A and storing each value A. SO far this is what I have written.

import java.util.Scanner;
import javax.swing.*;

public class Prin {
public static void main (String [] args){

    System.out.println("Please enter the Principal you wish to invest =>");
    Scanner stdio = new Scanner (System.in);
    int principal = stdio.nextInt(); 
    stdio.nextLine();

    System.out.println("This program will now calculate the final amount A for (years) n = 1,2,3,4 and the rate r = 1%, 1.5%, 2%, 2.5%, 3%");

    int yearsarray[] = {1,2,3,4};
    double ratearray[] = {0.010, 0.015, 0.020, 0.025, 0.030};

    double amountarray[];
    amountarray = new double[19];


    for(int i=0; i<=3; i++)
    {
        for(int j=0; j<=5; j++){

            amountarray[k] = principal * Math.pow((1 + ratearray[j]), yearsarray[i]);

            System.out.println(" answer " + amountarray[k] );


    }
}

Do I need another for loop to increase the values of k in amountarray[]?

I would like to have all values of amountarray i.e amountarray[0], amountarray[1], amountarray[2], .......and so on.

Thanks in advance for any help.

1
  • k = i*5 + j .. google row major order :) Commented Aug 20, 2013 at 12:42

4 Answers 4

3
amountarray = new double[19];

The code above is false because you need to have 4x5 = 20 double values

This code will always work properly, You should use this and you need to learn write your codes like this:

public class Prin {
public static void main (String [] args){

    //...
    int[] yearsarray = {1,2,3,4};
    double[] ratearray = {0.010, 0.015, 0.020, 0.025, 0.030};
    double[] amountarray = new double[yearsarray.length * ratearray.length];

    int k = 0;

    for(int i=0; i<yearsarray.length; i++){
        for(int j=0; j<ratearray.length; j++){
            amountarray[k] = principal * Math.pow((1 + ratearray[j]), yearsarray[i]);
            System.out.println(" answer " + amountarray[k] );
            k++;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I corrected my code. In you code, the programmer will get an ArrayIndexOutOfBoundsException. 'amountarray = new double[19];'
indeed, I took over that part verbatim from OP. As I felt there were several minor issues I rewrote the whole loop thing to propose improvements for all those that I spotted, have a look at the new version/
2

"This program will now calculate the final amount A for (years) n = 1,2,3,4 and the rate r = 1%, 1.5%, 2%, 2.5%, 3%"

... this implies your answer comes in the form of a 2-dimensional matrix.

Hence your amountarray needs to be defined as:

double amountarray[][] = new double[yearsarray.length][ratearray.length];

Then you would calculate:

amountarray[i][j] = principal * Math.pow((1 + ratearray[j]), yearsarray[i]);

1 Comment

That does make much more sense. The specification on my question was for 2 individual arrays. I much prefer your way.
1

No, you don't

int k=0; // initialization
for(int i=0; i<=3; i++)
{
    for(int j=0; j<=5; j++){
        // use post-increment
        amountarray[k] = principal * Math.pow((1 + ratearray[j]), yearsarray[i]);
        System.out.println(" answer " + amountarray[k] );
        // now we can increment k
        k = k+1;
}

Also this: as you seem to be using yearsarray just to get a value that's i+1, why just not do

amountarray[k] = principal * Math.pow((1 + ratearray[j]), i+1);

That way you can get rid of yearsarray, at least in this case

EDIT: a reworked version that also handles a couple of other minor issues and reduced usage of "magic numbers"

public class Prin {

    public static void main(String[] args) {

        System.out.println("Please enter the Principal you wish to invest =>");
        Scanner stdio = new Scanner(System.in);
        int principal = stdio.nextInt();
        stdio.nextLine();

        System.out.println("This program will now calculate the final amount A for (years) n = 1,2,3,4 and the rate r = 1%, 1.5%, 2%, 2.5%, 3%");

        int yearsarray[] = {1, 2, 3, 4};
        double ratearray[] = {0.010, 0.015, 0.020, 0.025, 0.030};

        double amountarray[];
        // this way the array will follow the size of yearsarray and ratearray
        amountarray = new double[yearsarray.length * ratearray.length];


        int k = 0; // initialization
        for (int i = 0; i <= yearsarray.length; i++) {
            System.out.println("years=" + yearsarray[i]);
            for (int j = 0; j < ratearray.length; j++) {
                // use post-increment
                amountarray[k] = principal * Math.pow((1 + ratearray[j]), yearsarray[i]);
                System.out.println("  " + ratearray[j] + " answer " + amountarray[k]);
                k+=1;
            }
        }
    }
}

2 Comments

Variable increment k++ hidden inside the System.out.println line? Ouch.
@Stewart yeah I agree, blame it on a series of small edits. And that's also why I offered the postincrement-less version. But you are 100% right, and that's why I removed all references to postincrements.
0

Try to store your data in a Map, this is an example of your loop

 for(int i=0; i<=yearsarray.length; i++) {
     for(int j=0; j<=ratearray.length; j++) {
         double answer = (yearsarray, principal * Math.pow((1 + ratearray[j]), yearsarray[i]));
         Collection<Double> aux;
         if (!answers.containsKey(yearsarray[i])){
             aux = new ArrayList<Double>();         
             aux.add(answer);
         } else {
             aux = answers.get(yearsarray[i]);
             aux.add(answer);
         }
         answers.put(yearsarray[i], aux);
         // include the answers in a Map 
    }
}

Thanks

2 Comments

I am not familiar with maps but it is something I will research. Thanks.
you shouldn't use arrays their solution is valid but isn't correct, stackoverflow.com/questions/1589813/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.