0

I just want to start off by saying that everything calculates accurately. The only problems I have are trying to print back the 2D array I entered and how to format the totals I get for each column.

This is part of a 3 in 1 program. For the second part, I must enter in 12 numbers in a 3x4 2D array. The console then returns the array I entered, and the sum column by column.

This is how it should look:

Enter 3 rows and 4 columns:
1 2 3 4
5 6 7 8
9 10 11.2 12.5
You entered:
1.0 2.0 3.0 4.0
5.0 6.0 7.0 8.0
9.0 10.0 11.2 12.5
The sums are:
15.0 18.0 21.2 24.5

This is my code so far:

else if(choice == 2) {
            // declare the 3x4 array
            System.out.print("Enter a 3 by 4 matrix row by row: ");
            double[][] myArray = new double[3][4];
            // set up the array as an input
            for (int i = 0; i < 3; i++)
              for (int j = 0; j < 4; j++)
                myArray[i][j] = input.nextDouble();

            feature2(myArray);

        } // end of choice 2 block

private static void feature2(double[][] myArray){


    System.out.println("You entered: ");
    // return the entered array in double form
    for (int i = 0; i < myArray.length; i++) {
          System.out.print(myArray[i] + " ");
        }
    // calculate the sums column by column and display the results
    for(int column = 0; column < myArray[0].length; column++) {
        double total = 0;
        for(int row = 0; row < myArray.length; row++)
            total += myArray[row][column];
        System.out.println("The sums are: " + total);

    }
} // end of feature 2

As the code stands, this is what reads on the console:

Enter a 3 by 4 matrix row by row: 
1 2 3 4
5 6 7 8
9 10 11.2 12.5
You entered: 
[D@3d4eac69 [D@42a57993 [D@75b84c92 The sums are: 15.0
The sums are: 18.0
The sums are: 21.2
The sums are: 24.5

As you can see, it calculates correctly, but it doesn't format correctly. If I could just have help with the formatting, I can take it from here.

0

1 Answer 1

1

To traverse through a two-dimensional array, you need a nested for-loop. The value of myArray[i] is just the address of one of the arrays at index i. To access the elements of the arrays within the 2D array, you need to access not only the index of the array but also the index of the element within the array, myArray[row][column]. The outer loop should go through each row (array), and the inner loop should go through each column (array element) within the array. Finally, after printing out the elements of one of the arrays, a newline should be printed for the next array's elements. Here's what it should look like,

System.out.println("You entered: ");
    // return the entered array in double form
for (int i = 0; i < myArray.length; i++) {
    for (int j = 0; i < myArray[i].length; j++){
       System.out.print(myArray[i][j] + " ");
    }
    System.out.println();
}
System.out.println("The sums are: ");
// calculate the sums column by column and display the results
    for(int column = 0; column < myArray[0].length; column++) {
        double total = 0;
        for(int row = 0; row < myArray.length; row++)
            total += myArray[row][column];
        System.out.print(total + " ");

    }
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. The only thing left is that I need the sums to be printed on one line, as shown at the very end of the first block of "code" in my post.
check my answer now, I added some stuff regarding your comment.

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.