Question

In: Computer Science

Declare two 2d arrays n0[MAX_ROW][MAX_COL], n1 [MAX_ROW][MAX_COL]. Then, loop through the 2d array and save the...

Declare two 2d arrays n0[MAX_ROW][MAX_COL], n1 [MAX_ROW][MAX_COL].
Then, loop through the 2d array and save the results in n0 and n1:

for (i=0; i<MAX_ROW; i++)
  for (j=0; j<MAX_COL;j++){
       //calculate number of zeros around entry a[i][j]
       n0[i][j] =
       //calculate number of ones around entry a[i][j]
       n1[i][j] =
  }

Set the MAX_ROW and MAX_COL to a small number and display all three 2d arrays on the screen to verify that the calculations are correct. You may still use command-line inputs as in the example program to set the actual size of the array (must be less than or equal to MAX_ROW, MAX_COL.

Solutions

Expert Solution

import java .io.*; 

class GFG 
{ 
static int R = 4; 
static int C = 4; 

static int getTotalCoverageOfMatrix(int [][]mat) 
{ 
        int res = 0; 


        for (int i = 0; i < R; i++) 
        { 
                boolean isOne = false; 

                
                for (int j = 0; j < C; j++) 
                { 
                         if (mat[i][j] == 1) 
                                isOne = true; 

                         
                        else if (isOne) 
                                res++; 
                } 

                isOne = false; 
                for (int j = C - 1; j >= 0; j--) 
                { 
                        if (mat[i][j] == 1) 
                                isOne = true; 
                        else if (isOne) 
                                res++; 
                } 
        } 

        for (int j = 0; j < C; j++) 
        { 
                
                boolean isOne = false; 
                for (int i = 0; i < R; i++) 
                { 
                        if (mat[i][j] == 1) 
                                isOne = true; 
                        else if (isOne) 
                                res++; 
                } 

                isOne = false; 
                for (int i = R - 1; i >= 0; i--) 
                { 
                        if (mat[i][j] == 1) 
                                isOne = true; 
                        else if (isOne) 
                                res++; 
                } 
        } 
        return res; 
} 


static public void main (String[] args) 
{ 
        int [][]mat = {{0, 0, 0, 0}, 
                                {1, 0, 0, 1}, 
                                {0, 1, 1, 0}, 
                                {0, 1, 0, 0}}; 

System.out.println( 
                getTotalCoverageOfMatrix(mat)); 
} 
} 



Related Solutions

In C programming Declare two 2d arrays n0[MAX_ROW][MAX_COL], n1 [MAX_ROW][MAX_COL]. Then, loop through the 2d array...
In C programming Declare two 2d arrays n0[MAX_ROW][MAX_COL], n1 [MAX_ROW][MAX_COL]. Then, loop through the 2d array and save the results in n0 and n1: for (i=0; i<MAX_ROW; i++)   for (j=0; j<MAX_COL;j++){        //calculate number of zeros around entry a[i][j]        n0[i][j] =        //calculate number of ones around entry a[i][j]        n1[i][j] =   } Set the MAX_ROW and MAX_COL to a small number and display all three 2d arrays on the screen to verify that the calculations are correct. You may still use command-line...
Lab 7 - 2D Arrays (C++) In main, declare and fill a 2D array with one...
Lab 7 - 2D Arrays (C++) In main, declare and fill a 2D array with one hundred rows and fifty columns. Iterate through each element and assign it a random value between -72 and 75 inclusive. Have your random number seed be 25. Create functions that do the following: 1. A function called “sum” that returns the sum of all the elements in your 2D Array. 2. A function called “average” that return the average value of the elements in...
This program needs to be in Java Exercise on Single Dimensional Arrays Declare an array reference...
This program needs to be in Java Exercise on Single Dimensional Arrays Declare an array reference variable arrayInt for an array of integers. Create the array of size 100, and assign it to arrayInt. (2 points) Write a method to populate the array with Random numbers between 1 to 25. Signature of the method is: populateArray( int[] ) which returns nothing. Call this method from main with arrayInt--> populateArray(arrayInt). (2 points) Write a method to print an array. Signature of...
. As input you are given two arrays: an array of numbers ? and an array...
. As input you are given two arrays: an array of numbers ? and an array ? of queries where each query is a target number. The array ? is unsorted and may contain duplicates. Your goal is, for each query ? in the array ?, count the number of pairs in the array ? that sums up to ?; that is, the number of distinct pairs of indices [?, ?], with ? < ?, such that ?[?] + ?[?]...
True or False: It's easy to loop through an array using a for loop in C++...
True or False: It's easy to loop through an array using a for loop in C++ because you can use the .size() function to get the total number of spaces in the array True or False: It's easy to loop through a vector using a for loop in C++ because you can use the .size() function to get the total number of spaces in the vector
1.Declare a two-dimensional array of Strings namedchessboard.
1. Declare a two-dimensional array of Strings named chessboard.2. Declare a two-dimensional array of integers named tictactoe.3. Declare and create a two-dimensional array of chars,tictactoe, with 3 rows, each with 3 elements.4. Create a two-dimensional array of ints, plan, with 2 rows, and and 3 columns and initialize the first row to 8, 20, 50 and the second row to 12, 30, 75. Use member initializer syntax.
Multidimensional Arrays Design a C program which uses two two-dimensional arrays as follows: - an array...
Multidimensional Arrays Design a C program which uses two two-dimensional arrays as follows: - an array which can store up to 50 student names where a name is up to 25 characters long - an array which can store marks for 5 courses for up to 50 students The program should first obtain student names and their corresponding marks for a requested number of students from the user. Please note that the program should reject any number of students that...
Multidimensional Arrays Design a C program which uses two two-dimensional arrays as follows: - an array...
Multidimensional Arrays Design a C program which uses two two-dimensional arrays as follows: - an array which can store up to 50 student names where a name is up to 25 characters long - an array which can store marks for 5 courses for up to 50 students The program should first obtain student names and their corresponding marks for a requested number of students from the user. Please note that the program should reject any number of students that...
Write a function in JAVASCRIPT that accepts an array as argument. The function should loop through...
Write a function in JAVASCRIPT that accepts an array as argument. The function should loop through the array elements and accumulate the sum of ASCII value of each character in element and return the total. For example: function([‘A’, ‘bc’, 12]); // returns 361 which is the sum of 65 + 98 + 99 + 49 + 50 Use of any built in string functions or built in array functions is not allowed, Any help would be much appreciated
I'm trying to get the union of two arrays and I tried making a loop that...
I'm trying to get the union of two arrays and I tried making a loop that does so. I tried also making the loop give me the size of the array as well from the union of the two arrays. Please check my loop to see what is wrong with it because it is not doing what I want it to do. I'm also not sure how to get the correct size of the array after the two arrays have...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT