# Help with Multi-dimensional Arrays. Help needed urgently!

• April 21st, 2014, 11:29 PM
Knowledge_Feeds_The_Mind
Help with Multi-dimensional Arrays. Help needed urgently!
I am having trouble fixing and figuring out how to change my code. My out put is very off, and I was wondering if anyone could give me insight to why, maybe even pose a solution,

"Enter a 3-by-3 matrix row by row:
1 1 1
1 1 1
1 1 1
Sum of the major diagonal is 6.0
Sum of the values of the column are: 10.0
Sum of the values of the column are: 20.0
Sum of the values of the column are: 30.0 "

I have tried a lot of things, but not much has helped.
Thank you and any help is greatly appreciated.

Code is below:
Code Java:

```  public static void main(String[] args) { double[][] m = new double[3][3]; m = createArray();   } private static double[][] createArray() {   Scanner input = new Scanner(System.in); double[][] matrix = new double[3][3]; System.out.println("Enter a " + matrix.length + "-by-" + matrix[0].length + " matrix row by row: "); for (double[] m1 : matrix) { for (int j = 0; j < matrix[0].length; j++) {   m1[j] = input.nextDouble();   } } System.out.println("Sum of the major diagonal is " + sumMajorDiagonal(matrix)); sumColumn (matrix, 0); return matrix; }   public static double sumColumn(double[][] matrix, int columnIndex){   double total = 0;//problem lies here double sum = 0; int row = 0; int last = matrix[0].length; for(columnIndex = 0; columnIndex < matrix[row].length; columnIndex++) { //total += matrix[0][1] + [1][1]; total += matrix[row][--last]; System.out.println("Sum of the values of the column are: " + (columnIndex + 1) + sum); }   return sum; }   public static double sumMajorDiagonal(double[][] m) {   double total = 0.0; int last = m[0].length;   for(int row = 0; row < m.length; row++) { total += m[row][row]; total += m[row][--last]; } return total; } }```
• April 22nd, 2014, 05:42 AM
Norm
Re: Help with Multi-dimensional Arrays. Help needed urgently!
Quote:

My out put is very off,
Can you explain what is wrong with the output and give an example of what it should be?
• April 22nd, 2014, 05:47 AM
GregBrannon
Re: Help with Multi-dimensional Arrays. Help needed urgently!
You're not concerned about the sum of the major diagonal?

I don't understand the purpose for the variable 'last'. Can you describe what that does? Oh yeah, that reminds me: comments in your code would be nice, probably even helpful to you and certainly helpful to those reviewing your code.

You have too many things going on in some of your methods. Methods other than main() should do one thing. The one thing they do should be described by the name of the method, usually a verb or containing a verb. The one thing each method does should be done very well.
• April 22nd, 2014, 09:17 AM
Knowledge_Feeds_The_Mind
Re: Help with Multi-dimensional Arrays. Help needed urgently!
Thank you very much for the fast response. :)
my output needs to look some thing like this. like this:

"Enter a 3-by-3 matrix row by row:
Row 1: 1 2 3
Row 2: 5 6 7
Row 3: 9 1 3

The sum of the elements at Column 1 is 16
The sum of the elements at Column 2 is 9
The sum of the elements at Column 3 is 13

The sum of the elements along the major diagonal is 10"

My main plan of action is to first get the problem figured out with the sum of the columns and then later, tackle the major diagonal.
I am currently trying to figure out what is exactly wrong with my methods.....and what is causing this incorrect results to appear.

Code Java:

```  //Write a program that reads a 3 by 3 array of double values row by row, stores //values into a two dimensional array of three rows by three columns   public static void main(String[] args) { double[][] matrix = new double[3][3];// the 3 - 3 matrix matrix = createArray();   } private static double[][] createArray() {   Scanner input = new Scanner(System.in); double[][] matrix = new double[3][3]; System.out.println("Enter a 3-by-3 matrix row by row "); for (double[] m1 : matrix) { System.out.println("Row: "); for (int j = 0; j < matrix[0].length; j++) {   m1[j] = input.nextDouble();   } } System.out.println("Sum of the major diagonal: " + sumMajorDiagonal(matrix)); sumColumn (matrix, 0); return matrix; }   public static double sumColumn(double[][] matrix, int columnIndex){   double sum = 0; for (int column = 0; column < matrix.length; column++) { for (int row = 0; row < matrix[column].length; row++){ sum += matrix[row][column]; } System.out.println("Sum of 1st column is " + column + sum); } return sum; }   public static double sumMajorDiagonal(double[][] matrix) {   double sum = 0.0; int last = matrix[0].length;   for(int row = 0; row < matrix.length; row++) { sum += matrix[row][row]; sum += matrix[row][--last]; //calculate the last value and ends } return sum; } }```
• April 22nd, 2014, 09:25 AM
Norm
Re: Help with Multi-dimensional Arrays. Help needed urgently!
The posted code won't compile for testing. It needs the class definition.

One observation:
Code :

``` double[][] matrix = new double[3][3];// the 3 - 3 matrix <<< Here creates an array matrix = createArray(); //<<< Here IMMEDIATELY destroys/replaces the array created above```
There is no need to create an array if the next statement is going to replace it. Define it and assign it a value in one step:
double[][] matrix = createArray();

The posted code does not contain this String: "The sum of the elements at Column"
where is the code to do that summation?

The calls to the methods to work with the array should NOT be nested inside of the create method.
Each method should be called separately in the main method:
create the array
sum this way
sum that way
sum the other way

The created array should be passed as an arg to each of the sum... methods.
• April 22nd, 2014, 09:50 AM
Knowledge_Feeds_The_Mind
Re: Help with Multi-dimensional Arrays. Help needed urgently!
this is the code where the summation is completed:

Code Java:

``` public static double sumColumn(double[][] matrix, int columnIndex){   double sum = 0; //below is the code to solve for the column sum/ summation for (int column = 0; column < matrix.length; column++) { for (int row = 0; row < matrix[column].length; row++) { sum += matrix[row][column]; } System.out.println("Sum of 1st column is " + column + sum); } return sum; }```
• April 22nd, 2014, 09:56 AM
Norm
Re: Help with Multi-dimensional Arrays. Help needed urgently!
What does that code do with the columnIndex value that is passed to it?
If you were to write a statement to add the contents of column 1 for three rows (0,1,2) of the matrix, what would that statement look like?

The method should return the value, not print it. The caller of the method should do the printing.
val = sum() // get the value
print val // print the value
• April 22nd, 2014, 10:39 AM
Knowledge_Feeds_The_Mind
Re: Help with Multi-dimensional Arrays. Help needed urgently!
So, I solved it. Now I only need help with calculating the major diagonal

Code Java:

``` public static double sumColumn(double[][] matrix, int column){   int row; double sum = 0; //below is the code to solve for the column for (column = 0; column < matrix[0].length; column++) { sum = 0; for (row = 0; row < matrix.length; row++) sum = sum + matrix[row][column]; System.out.println("Sum of column " + (column + 1) + " = " + sum); } return sum; }```

This is the code that I am still working on. It calculated the diagonal twice instead of only once. Any insights.
Thank you very much for your patience and assistance, I am deeply appreciative.

Code Java:

```the full code   public static double sumMajorDiagonal(double[][] matrix) {   double sum = 0.0; int last = matrix[0].length;   for(int row = 0; row < matrix.length; row++) { sum += matrix[row][row]; sum += matrix[row][--last]; //calculate the last value and ends } return sum; } }```
• April 22nd, 2014, 10:43 AM
Norm
Re: Help with Multi-dimensional Arrays. Help needed urgently!
sumColumn() should need only one loop to go down the rows and add the values in the selected column.

In the sumMajorDiagonal() method, What are the elements that should be summed to get the value you want?
• April 22nd, 2014, 11:27 AM
Knowledge_Feeds_The_Mind
Re: Help with Multi-dimensional Arrays. Help needed urgently!
Thanks, I finally solved my problem. I really appreciate all the help you have given me! :D :D
If it had not been for your insight it probably would have taken me much longer, thank you so much again.
-Vex :cool:
• April 22nd, 2014, 11:47 AM
Norm
Re: Help with Multi-dimensional Arrays. Help needed urgently!
Glad its working for you now.