2

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;
}
2
  • 1
    The total variables are only visible inside the for-loops. Define them outside the loops. Commented Oct 29, 2017 at 14:24
  • 2
    Possible duplicate of Scope of variable declared inside a for loop Commented Oct 29, 2017 at 14:26

1 Answer 1

2

The issue is with the variable scopes, Your code is flawed in these places

public static int columnsum(int[][] matrix){
System.out.println("Column Totals************************************");
for (int column = 0; column<matrix[0].length; column++){
    int total = 0; // total is defined within the for loop you cannot access it outside the for loop
    for (int row = 0; row<matrix.length; row++){
        total += matrix[row][column];
    System.out.println("The sum of column "+column+" is: "+total);
    }
return total; // Java does not know the total variable since it's already got destroyed after the for loop got terminated
}

How to correct this ??

public static int columnsum(int[][] matrix){
System.out.println("Column Totals************************************");
int total = 0; // define total here
for (int column = 0; column<matrix[0].length; column++){
    
    for (int row = 0; row<matrix.length; row++){
        total += matrix[row][column];
    System.out.println("The sum of column "+column+" is: "+total);
    }
return total;
}

DO this in all cases where total malfunctions

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.