Send edited code package part2 public class TotalSum privat

Send edited code

package part2;

public class TotalSum {

   private static int ROWS = 7;

   private static int COLUMNS = 7;

   public static void main(String[] args) {

      

       int[][] arr = { {29, 11, 39, 2, 6, 27, 47},

                       { 5, 26, 29, 45, 6, 5, 39},

                       {10, 49, 25, 28, 11, 41, 27},

                       {43, 19, 29, 7, 3, 35, 16},

                       {12, 8, 38, 27, 7, 4, 46},

                       {26, 30, 46, 6, 5, 38, 42},

                       {15, 47, 14, 7, 49, 7, 16} };

       // part 2

       // write code that will print out the total sum of all integers in the 2D array called arr

       // YOUR CODE STARTS HERE ( DO NOT EDIT ABOVE THIS LINE )

      

   }

}

Part 2: Iterating through 2D Arrays For part 2 you will use the code in TotalSum.java. A 2D array called arr is already declared for you in this code. Your task is to write code that will print out the total sum of all integers in the 2D array called arr. · Hint: Use one loop to iterate through the rows and another to loop through the columns

Solution

package part2;

public class TotalSum {

   private static int ROWS = 7;

   private static int COLUMNS = 7;

   public static void main(String[] args) {

      

       int[][] arr = { {29, 11, 39, 2, 6, 27, 47},

                       { 5, 26, 29, 45, 6, 5, 39},

                       {10, 49, 25, 28, 11, 41, 27},

                       {43, 19, 29, 7, 3, 35, 16},

                       {12, 8, 38, 27, 7, 4, 46},

                       {26, 30, 46, 6, 5, 38, 42},

                       {15, 47, 14, 7, 49, 7, 16} };

       // part 2

       // write code that will print out the total sum of all integers in the 2D array called arr

       // MY CODE STARTS HERE

int sum=0; // initialize a variable which will store the sum
   for(int i=0;i<rows;i++){ // First for loop will iterate over the rows
       for (int j=0;j<columns;j++){ // Second for loop will iterate over the columns
           sum = arr[i][j] + sum; // Adding each element within an array
       }
   }
  
   System.out.println(sum); // Display the sum of 2D array \"arr\"
  
   }

      

   }

}

Send edited code package part2; public class TotalSum { private static int ROWS = 7; private static int COLUMNS = 7; public static void main(String[] args) { in
Send edited code package part2; public class TotalSum { private static int ROWS = 7; private static int COLUMNS = 7; public static void main(String[] args) { in

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site