Home > database >  Why is my Index always going out of bound whenever I try to display an Array?
Why is my Index always going out of bound whenever I try to display an Array?

Time:11-21

I am trying to modify a given code and add an average to all the elements within a user given 2d array. I'm initializing the array ave to have the same elements of array sum and then displaying it outside the for loop to do the calculation.

import java.util.Scanner;

public class Arrays2D_GeneratorRevised {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        final int R=4, C=3;
        int r,c;
        double[][] volt = new double[R][C];
        double[] sum = new double[R];
        double[] ave = new double[R];
        System.out.println("Enter the output voltages for the following generators :");
        for(r=0; r<R; r  ) {
            System.out.print("Generator " (r 1) " :\n");
            for(c=0; c<C; c  ) {
                volt[r][c]=in.nextInt();
                sum[r] =volt[r][c];
                ave[r] =sum[r];
            }
        }
        //display table
        System.out.print("\n\t           Generator Test Results");
        System.out.printf("\n\tss%9ss","Output 1","Output 2","Output 3","Average");
        for(r=0; r<R; r  ) {
            System.out.print("\nGenerator " (r 1));
            for(c=0; c<C; c  ) {
                System.out.printf(".2f",volt[r][c]);
            }
            System.out.printf(".2f",sum[r]/C);
        }
        System.out.printf(".2f",ave[r]/=(R*C));
        System.out.print("\n\n");
    }
}

I tried making the ave to a 2d array and assigning it different variables of R,C,c,r. This also happens whenever I make a 1d array and display it using the argument array[i]. Somehow it only works whenever I use the Arrays.toString() to display or manipulate the elements inside.

CodePudding user response:

The error is in this line:

for(r=0; r<R; r  )
{
    [...]
}
System.out.printf(".2f",ave[r]/=(R*C));
                            // ^ HERE

After the for loop, r == R, which is one past the end of the array.

Looking at the code, should that printf be inside the loop, not outside?

CodePudding user response:

If ave is supposed to be the average for all four generators, then your calculation is wrong. ave should be a scalar and not an array.

import java.util.Scanner;

public class Arrays2D_GeneratorRevised {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        final int R=4, C=3;
        int r,c;
        double[][] volt = new double[R][C];
        double[] sum = new double[R];
        double ave = 0.0;
        System.out.println("Enter the output voltages for the following generators :");
        for(r=0; r<R; r  ) {
            System.out.print("Generator " (r 1) " :\n");
            for(c=0; c<C; c  ) {
                volt[r][c]=in.nextInt();
                sum[r] =volt[r][c];
                ave =sum[r];
            }
        }
        //display table
        System.out.print("\n\t           Generator Test Results");
        System.out.printf("\n\tss%9ss","Output 1","Output 2","Output 3","Average");
        for(r=0; r<R; r  ) {
            System.out.print("\nGenerator " (r 1));
            for(c=0; c<C; c  ) {
                System.out.printf(".2f",volt[r][c]);
            }
            System.out.printf(".2f",sum[r]/C);
        }
        System.out.printf("%n.2f",ave/=(R*C));
        System.out.print("\n\n");
    }
}

Here is a sample run:

Enter the output voltages for the following generators :
Generator 1 :
1 4 9
Generator 2 :
16 25 36
Generator 3 :
49 64 81
Generator 4 :
100 121 144

               Generator Test Results
            Output 1  Output 2 Output 3   Average
Generator 1      1.00      4.00      9.00      4.67
Generator 2     16.00     25.00     36.00     25.67
Generator 3     49.00     64.00     81.00     64.67
Generator 4    100.00    121.00    144.00    121.67
     99.67
  • Related