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:
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; } }