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.