Question

In: Computer Science

Write a function named "check_matrix" which takes two matrices as parameters and returns 1 if the...

Write a function named "check_matrix" which takes two matrices as parameters and returns 1 if the matrices are same or 0 otherwise. Set appropriate parameters and return type if necessary.

Solutions

Expert Solution

Please give a thumbs up if you find this answer helpful.
If it doesn't help, please comment before giving a thumbs down.


Since you have not mentioned the programming language, I have provided the answer in C++.
If you want answer in different programming language, then let me know in comment section.

Please look at my code and in case of indentation issues check the screenshots.

---------main.cpp---------------

#include <iostream>
using namespace std;
#define max 1000

//this function takes two matrices and their dimensions as input
//returns 1 is matrices are same
//returns 0 if otherwise
int check_matrix(int matrix1[][max], int m1, int n1, int matrix2[][max], int m2, int n2)
{
   if(m1!= m2 || n1!=n2)   //check if dimensions of both matrices are different
       return 0;           //return 0 if dimensions are different

   for(int i = 0; i < m1; i++){   //loop through every row
       for(int j = 0; j < n1; j++){   //loop through every column
           if(matrix1[i][j] != matrix2[i][j])   //if any element at same position mismatches, then return 0
               return 0;                  
       }
   }
   return 1;   //return 1 if all elements matched
}

int main()
{
   int m1, n1;   //read dimensions of matrix 1
   cout << "Enter number of rows in matrix 1: ";
   cin >> m1;
   cout << "Enter number of columns in matrix 1: ";
   cin >> n1;

   int matrix1[m1][max];   //declare matrix 1
   for(int i = 0; i < m1; i++){
       cout << "Enter row " << i+1 << " of matrix 1: ";
       for(int j = 0; j < n1; j++){
           cin >> matrix1[i][j];       //read matrix 1
       }
   }

   cout << "\nThe matrix 1 is: \n";
   for(int i = 0; i < m1; i++){
       for(int j = 0; j < n1; j++){
           cout<< matrix1[i][j] << " ";   //print matrix 1
       }
       cout << endl;
   }

   int m2, n2;   //read dimensions of matrix 1
   cout << "\n\nEnter number of rows in matrix 2: ";
   cin >> m2;
   cout << "Enter number of columns in matrix 2: ";
   cin >> n2;

   int matrix2[m2][max];   //declare matrix 2
   for(int i = 0; i < m2; i++){
       cout << "Enter row " << i+1 << " of matrix 2: ";
       for(int j = 0; j < n2; j++){
           cin >> matrix2[i][j];   //read matrix 2
       }
   }
   cout << "\nThe matrix 2 is: \n";
   for(int i = 0; i < m2; i++){
       for(int j = 0; j < n2; j++){
           cout<< matrix2[i][j] << " ";   //print matrix 2
       }
       cout << endl;
   }

   int val = check_matrix(matrix1, m1, n1, matrix2, m2, n2);   //check if matrices are same
   if(val == 1)                                               //if return value is 1, they are same
       cout << "\nmatrix 1 and matrix 2 are same" << endl;
   else                                                        //if return value is 0, they are different
       cout << "\nmatrix 1 and matrix 2 are different" << endl;
   return 0;
}

----------Screenshots--------------

----------Output--------------

------------------------------------------------

Please Do comment if you need any clarification.
Please let me know if you are facing any issues.
Please Rate the answer if it helped.

Thankyou


Related Solutions

Write a recursive function named multiply that takes two positive integers as parameters and returns the...
Write a recursive function named multiply that takes two positive integers as parameters and returns the product of those two numbers (the result from multiplying them together). Your program should not use multiplication - it should find the result by using only addition. To get your thinking on the right track: 7 * 4 = 7 + (7 * 3) 7 * 3 = 7 + (7 * 2) 7 * 2 = 7 + (7 * 1) 7 *...
Write a C++ function which takes an int array and its size as parameters. It returns...
Write a C++ function which takes an int array and its size as parameters. It returns an int indicating how many multiples of 3 are contained in the array. For example, if the array contains {2, 6, 8} your function should return 1. If the array contains {3, 10, 5, 6} your function should return 2. Here is the function prototype: // int array[]: array to search // size: number of elements in the array int countMult(const int array[], int...
Write a function named "characters" that takes a string as a parameter and returns the number...
Write a function named "characters" that takes a string as a parameter and returns the number of characters in the input string
In C++ Write a function which takes two parameters: an array of ints and an int...
In C++ Write a function which takes two parameters: an array of ints and an int size of the array and prints every element greater than 5 to the screen. As an example, if the array has the following 10 elements: 2 5 8 9 7 1 0 2 6 3, your function should print out 8 9 7 6. You may assume that the parameters passed to the function are valid. Your function must have the following signature: void...
Challenge 1 – 2D Array Maker Create a function that takes in two parameters and returns...
Challenge 1 – 2D Array Maker Create a function that takes in two parameters and returns a created two-dimension array. var twoD = Make2D(<width>, <height>); Challenge 2 – Fill 2D Array Create a function that takes a single parameter and fills the 2D array with that parameter Use Cases: Fill2D(<twoD array>, <fill value>); //fills twoD twoD = fill2D(<twoD array>, <fill value>); //fills twoD, but also returns reference Challenge 3 – Make 2D Array Write a function that takes 3 parameters...
Write a MATLAB function named numberWords() that takes a whole number as an argument and returns...
Write a MATLAB function named numberWords() that takes a whole number as an argument and returns a string containing the number word for the whole numbers 0 - 999. For example:  numberWords(234) would return 'two hundred thirty-four' If the input value is not a whole number between 0 - 999 then the function should return a string equivalent to 'ERROR'.
Write a function that will accept two integer matrices C and D by reference parameters. The...
Write a function that will accept two integer matrices C and D by reference parameters. The function will compute the transpose of C and store it in D. For your information, the transpose of matrix C is D, where D[j][i] = C[i][j]. [7 marks] Explain the time complexity of this function inside of the function code as a comment. [3 marks] in C++
Write a program that contains the following Write a function copies with two int parameters, named...
Write a program that contains the following Write a function copies with two int parameters, named n and x. It should dynamically allocate a new array of size n.  The function should then copy the value in x to every position in the array. The function should return a pointer to the new array. In the main function, call the copies function from part 1. with size 5 and value -1.  Output the resulting array to the screen, and deallocate the array....
2 Write a function named equivalentArrays that has two array arguments and returns 1 if the...
2 Write a function named equivalentArrays that has two array arguments and returns 1 if the two arrays contain the same values (but not necessarily in the same order), otherwise it returns 0. Your solution must not sort either array or a copy of either array! Also you must not modify either array, i.e., the values in the arrays upon return from the function must be the same as when the function was called. Note that the arrays do not...
Write a function in c++, called afterAll that takes two parameters, a vector of string and...
Write a function in c++, called afterAll that takes two parameters, a vector of string and a string. The function returns true if the 2nd parameter comes after all of the strings in the vector, order-wise, false if not. As an example, "zoo" comes after "yuzu".
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT