I'm having a bit of trouble with a program I am writing for school. Essentially, I have to use multiple methods and pass a two-dimensional array through them. To do that I have to use nested for loops in the methods, but the methods are not letting me return the "total". You can see the methods I am using below. The bottom two methods are working fine, just the ones with the total variables are not. (Yes, I have tried using other names.)
//method sum of each row
public static int rowsum(int [][] matrix){
System.out.println("Row Totals***************************************");
for (int row = 0; row<matrix.length; row++){
int total = 0;
for (int column = 0; column<matrix.length; column++)
total += matrix[row][column];
System.out.println("The sum of row "+row+" is: "+total);
}
return total;
}
//method sum of each column
public static int columnsum(int[][] matrix){
System.out.println("Column Totals************************************");
for (int column = 0; column<matrix[0].length; column++){
int total = 0;
for (int row = 0; row<matrix.length; row++){
total += matrix[row][column];
System.out.println("The sum of column "+column+" is: "+total);
}
return total;
}
return total;
}
//method product if rows
public static int rowprod(int [][] matrix){
System.out.println("Row Product**************************************");
for (int row = 0; row<matrix.length; row++){
int total = 0;
for (int column = 0; column<matrix.length; column++)
total *= matrix[row][column];
System.out.println("The sum of column "+row+" is: "+total);
}
return total;
}
//method product of columns
public static int columnprod(int [][] matrix){
System.out.println("Column Product***********************************");
for (int column = 0; column<matrix[0].length; column++){
int total = 0;
for (int row = 0; row<matrix.length; row++)
total = total*matrix[row][column];
System.out.println("The sum of column "+column+" is: "+total);
}
return total;
}
//method highest value in matrix
public static int highest(int [][] matrix){
System.out.println("This greatest value in this matrix is: ");
int high = matrix [0][0];
for (int row = 0; row<matrix.length;row++){
for(int column=0;column<matrix.length; column++){
if(high<matrix[row][column]){
high=matrix[row][column];
}
}
}
return high;
}
//method lowest value of matrix
public static int lowest(int [][] matrix){
System.out.println("The lowest value in this matrix is: ");
int low = matrix [0][0];
for (int row = 0; row<matrix.length;row++){
for(int column=0;column<matrix.length; column++){
if(low>matrix[row][column]){
low=matrix[row][column];
}
}
}
return low;
}
totalvariables are only visible inside the for-loops. Define them outside the loops.