Question

In: Computer Science

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 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

I am adding the code with the output and i have commented for each condition so that it becomes easy to understand for you...

I have Checked all the possible cases for better and proper outcomes...

if you have any doubt,please ask me in the comment box... please try to understand the logic by making a 2D array on a papper and then see the condition strp by step... it will help you...

code

#include<stdio.h>
#define MAX_ROW 5
#define MAX_COL 5
int count0(int a[][MAX_COL],int i,int j){    //for counting number of 0 around position i,j
        int count=0;  // initialising count = 0
        if(i!=0 && i!=MAX_ROW-1 && j!=0 && j!=MAX_COL-1){          //if i and j are not at the age position
                if(a[i][j+1]==0)  count++;
                if(a[i][j-1]==0)  count++;
                if(a[i+1][j]==0)  count++;
                if(a[i-1][j]==0)  count++;
                if(a[i-1][j+1]==0)  count++;
                if(a[i+1][j+1]==0)  count++;
                if(a[i-1][j-1]==0)  count++;
                if(a[i+1][j-1]==0)  count++;
        }
        else if(i==0 &&  j!=0 && j!=MAX_COL-1){          // i.e first row except first and last column
                if(a[i][j+1]==0)  count++;
                if(a[i][j-1]==0)  count++;
                if(a[i+1][j+1]==0)  count++;
                if(a[i+1][j]==0)  count++;
                if(a[i+1][j-1]==0)  count++;
                }
        else if(j==0 &&   i!=0 && i!=MAX_ROW-1){      //// i.e first column except first and last row
                if(a[i-1][j]==0)  count++;
                if(a[i-1][j+1]==0)  count++;
                if(a[i][j+1]==0)  count++;
                if(a[i+1][j+1]==0)  count++;
                if(a[i+1][j]==0)  count++;
                }
        else if(i==MAX_ROW-1 &&  j!=0 && j!=MAX_COL-1){      //// i.e last row except first and last culumn
                if(a[i][j+1]==0)  count++;
                if(a[i][j-1]==0)  count++;
                if(a[i-1][j]==0)  count++;
                if(a[i-1][j+1]==0)  count++;
                if(a[i-1][j-1]==0)  count++;
                }
        else if(j==MAX_COL-1   && i!=0 && i!=MAX_ROW-1){    //// i.e last column except first and last row
                if(a[i+1][j]==0)  count++;
                if(a[i-1][j]==0)  count++;
                if(a[i-1][j-1]==0)  count++;
                if(a[i+1][j-1]==0)  count++;
                if(a[i][j-1]==0)  count++;
                }
        else if(i==0 && j==0){              //i.e upper left  corner element
                if(a[i][j+1]==0)  count++;
                if(a[i+1][j+1]==0)  count++;
                if(a[i+1][j]==0)  count++;
                }
        else if(i==0 && j==MAX_COL-1){     //i.e upper right  corner element
                if(a[i][j-1]==0)  count++;
                if(a[i+1][j-1]==0)  count++;
                if(a[i+1][j]==0)  count++;
                }
        else if( i== MAX_ROW-1 && j==0){    //i.e lower left  corner element
                if(a[i][j+1]==0)  count++;
                if(a[i-1][j+1]==0)  count++;
                if(a[i-1][j]==0)  count++;
                }
        else if(i== MAX_ROW-1 && j==MAX_COL-1){    //i.e lower right  corner element
                if(a[i][j-1]==0)  count++;
                if(a[i-1][j-1]==0)  count++;
                if(a[i-1][j]==0)  count++;
                }
        return count;   //returning count
        }
int count1(int a[][MAX_COL],int i,int j){             //for counting number of 1 around position i,j
        int count=0;
                if(i!=0 && i!=MAX_ROW-1 && j!=0 && j!=MAX_COL-1){    //if i and j are not at the age position
                if(a[i][j+1]==1)  count++;
                if(a[i][j-1]==1)  count++;
                if(a[i+1][j]==1)  count++;
                if(a[i-1][j]==1)  count++;
                if(a[i-1][j+1]==1)  count++;
                if(a[i+1][j+1]==1)  count++;
                if(a[i-1][j-1]==1)  count++;
                if(a[i+1][j-1]==1)  count++;
                }
        else if(i==0 &&   j!=0 && j!=MAX_COL-1){     // i.e first row except first and last column
                if(a[i][j+1]==1)  count++;
                if(a[i][j-1]==1)  count++;
                if(a[i+1][j+1]==1)  count++;
                if(a[i+1][j]==1)  count++;
                if(a[i+1][j-1]==1)  count++;
                }
        else if(j==0 && i!=0 && i!=MAX_ROW-1){    //// i.e first column except first and last row
                if(a[i-1][j]==1)  count++;
                if(a[i-1][j+1]==1)  count++;
                if(a[i][j+1]==1)  count++;
                if(a[i+1][j+1]==1)  count++;
                if(a[i+1][j]==1)  count++;
                }
        else if(i==MAX_ROW-1   && j!=0 && j!=MAX_COL-1){      /// i.e last row except first and last culumn
                if(a[i][j+1]==1)  count++;
                if(a[i][j-1]==1)  count++;
                if(a[i-1][j]==1)  count++;
                if(a[i-1][j+1]==1)  count++;
                if(a[i-1][j-1]==1)  count++;
                }
        else if(j==MAX_COL-1   && i!=0 && i!=MAX_ROW-1){       // i.e last column except first and last row
                if(a[i+1][j]==1)  count++;
                if(a[i-1][j]==1)  count++;
                if(a[i-1][j-1]==1)  count++;
                if(a[i+1][j-1]==1)  count++;
                if(a[i][j-1]==1)  count++;
                }
        else if(i==0 && j==0){         //i.e upper left  corner element
                if(a[i][j+1]==1)  count++;
                if(a[i+1][j+1]==1)  count++;
                if(a[i+1][j]==1)  count++;
                }
        else if(i==0 && j==MAX_COL-1){    //i.e upper right  corner element
                if(a[i][j-1]==1)  count++;
                if(a[i+1][j-1]==1)  count++;
                if(a[i+1][j]==1)  count++;
                }
        else if( i== MAX_ROW-1 && j==0){     //i.e lower left  corner element
                if(a[i][j+1]==1)  count++;
                if(a[i-1][j+1]==1)  count++;
                if(a[i-1][j]==1)  count++;
                }
        else if(i== MAX_ROW-1 && j==MAX_COL-1){    ////i.e lower right  corner element
                if(a[i][j-1]==1)  count++;
                if(a[i-1][j-1]==1)  count++;
                if(a[i-1][j]==1)  count++;
                }

        return count;  //returning count
        }

int main(){
        int a[MAX_ROW][MAX_COL],n0[MAX_ROW][MAX_COL],n1[MAX_ROW][MAX_COL],i,j;
        printf("\nEnter the Elements in the array ' a ' \n");
        for (i=0; i<MAX_ROW; i++){
                for (j=0; j<MAX_COL;j++){
                        scanf("%d",&a[i][j]);   //scanning array a
                        n0[i][j]=n1[i][j]=0;    //initialising n1,n0 by 0
                        }
                }
        for (i=0; i<MAX_ROW; i++){
                for (j=0; j<MAX_COL;j++){
                        n0[i][j]=count0(a,i,j);            //calling function for each position of array a
                        n1[i][j]=count1(a,i,j);
                        }
                }
        printf("\n  ARRAY 'a'      ARRAY n0      ARRAY n1  \n");
         for (i=0; i<MAX_ROW; i++){                  //printing the arrays side by side
                for (j=0; j<MAX_COL;j++)
                        printf("%2d",a[i][j]);
                printf("  |  ");
                for (j=0; j<MAX_COL;j++)
                        printf("%2d",n0[i][j]);
                printf("  |  ");
                for (j=0; j<MAX_COL;j++)
                        printf("%2d",n1[i][j]);
                printf("\n");
                }
        return 0;
        }

output....


Related Solutions

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...
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...
Explain the difference between array and structure based on their usage in C++ programming. Declare a...
Explain the difference between array and structure based on their usage in C++ programming. Declare a structure called studentScore which contains name of student, registration number of students, course code and marks. Declare structure variable named studentCS680 based on the structure in (b) to store fifty (50) students’ data. Write a program that prompts a user to enter data for 50 students in a structure variable declared in (b) and calculate the average mark.
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
In C programming, Thank you Declare an array that can contain 5 integer numbers. Use a...
In C programming, Thank you Declare an array that can contain 5 integer numbers. Use a for loop to ask the user for numbers and fill up the array using those numbers. Write a program to determine the 2 largest integers in the array, and print those numbers. Hint: You can declare integer variables “largest” and “secondLargest” to keep track as you make comparisons using the if...elseif statement. The following is an example of how your program should look when...
USE C PROGRAMMING, call all functions in main, and use a 2 by 3 2d array...
USE C PROGRAMMING, call all functions in main, and use a 2 by 3 2d array to test, thanks. get_location_of_min This function takes a matrix as a 2-D array of integers with NUM_COLS width, the number of rows in the matrix and two integer pointers. The function finds the location of the minimum value in the matrix and stores the row and column of that value to the memory location pointed to by the pointer arguments. If the minimum value...
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...
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...
Exercises on Arrays –using C++programming 1. You want an array with the numbers 100 – 105....
Exercises on Arrays –using C++programming 1. You want an array with the numbers 100 – 105. In the boxes below, fill in what your array should have. Fill in the index of each element below it. Array Index Open up your editor and write the following program: Declare an array that has the numbers 100 to 105. (How many elements are there in the array?) Print the array. Save your file and test it. Compare your results with your table...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT