Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

void compare_with_each_row(int m[][N], int q[], int n) void compare_with_each_co

ID: 3743176 • Letter: V

Question



void compare_with_each_row(int m[][N], int q[], int n)

void compare_with_each_column(int m[][N], int q[], int n)

can it be in programming language C, thanks

4. (20 pt) You are given an NxN 2D-array of integers (matrix) and a 1D-array of N integers (query). You are asked to write two functions to . compare the given ID array (query) with each row (left-to-rihtof the matrix . compare the given 1D array (query) with each column (top-to-bottom |) of the matrix If there is a match, these functions will print the matched row numbers and column numbers. Here is an example showing how the functions that you will implement in the next page can be used in main). suppose std C libraries are included here / fde fine N 4 /this number can be changed / main() int matrix [N] IN110, 10, 30, 45) 14, 10, 32, 11), 20, 30, 40, 50) 135 ,45, 25, 15) int query [N] int ii printf ("Enter ed integers to search in 2D array", N); printf ("Enter query [id] scanf("%d", &que ry [i]); ", i); = compare with each row matrix, query, N) compare with each column matrix, query, N); When the above program is executed, suppose a user enters 10 10 30 45 as the query Then the program should print: Row 0 is the same as query Column 1 is the same as query t/ void compare with each row (int m[ IN], int q, int n) / left-to-right

Explanation / Answer

void compare_with_each_row(int m[][N], int q[], int n)

{

    int i, j;

   

    // raverse the row

    for( i = 0 ; i < n ; i++ )

    {

        // becomes 0 if the current row is not same as q

        int flag = 1;

       

        for( j = 0 ; j < n ; j++ )

        {

            // if current j th element pf i th row is not equal to j th element of q

            if( m[i][j] != q[j] )

            {

                flag = 0;

                break;

            }

        }

       

        // if the i th row is identical to q

        if( flag == 1 )

            printf("Row %d is same as query " , i);

    }

}

void compare_with_each_column(int m[][N], int q[], int n)

{

    int i, j;

   

    // raverse the columns

    for( j = 0 ; j < n ; j++ )

    {

        // becomes 0 if the current row is not same as q

        int flag = 1;

       

        for( i = 0 ; i < n ; i++ )

        {

            // if current i th element pf j th column is not equal to i th element of q

            if( m[i][j] != q[i] )

            {

                flag = 0;

                break;

            }

        }

       

        // if the i th row is identical to q

        if( flag == 1 )

            printf("Column %d is same as query " , j);

    }

}