jeudi 24 septembre 2020

Im trying to print the sum in the main diagonal in a random array but my code wont work

int[][] arr = new int[rows][cols];

System.out.print("6. Output the sum of the elements in the main diagonal");

         for(i=0;i<rows;i++)
         { 
             for( j = 0; j < cols; j++)
          { 
              arr[i][j] = g.nextInt();
         }
     }
             for( i = 0; i < rows; i++)
         { 
              for( j = 0; j < cols; j++)
          { 
        if(i == j) 
     {
     sum = sum + arr[i][j];
     }
         }
        }
        System.out.print("SUM of DIAGONAL elements of the matrix = " + sum) ;



Aucun commentaire:

Enregistrer un commentaire