दो मैट्रिक्स M1[r][c] और M2[r][c] 'r' पंक्तियों की संख्या और 'c' कॉलम की संख्या के साथ, हमें यह जांचना होगा कि दोनों दिए गए मैट्रिक्स समान हैं या नहीं। यदि वे समान हैं तो प्रिंट करें "मैट्रिसेस समान हैं" अन्यथा प्रिंट करें "मैट्रिस समान नहीं हैं"
समान मैट्रिक्स
दो आव्यूह M1 और M2 समरूप कहलाते हैं जब -
- दोनों मैट्रिक्स की पंक्तियों और स्तंभों की संख्या समान है।
- M1[i][j] के मान M2[i][j] के बराबर हैं।
जैसे नीचे दी गई आकृति में 3x3 के दोनों आव्यूह m1 और m2 समान हैं -
$$M1[3][3]=\begin{bmatrix} 1 &2 &3 \\ 4 &5 &6 \\ 7 &8 &9 \\ \end {bmatrix} \:\:\:\:M2 [3] [3] =\ start {bmatrix} 1 और 2 और 3 \\ 4 और 5 और 6 \\ 7 और 8 और 9 \\ \end{bmatrix} $$
उदाहरण
Input: a[n][n] = { {2, 2, 2, 2},
{2, 2, 2, 2},
{3,3, 3, 3},
{3,3, 3, 3}};
b[n][n]= { {2, 2, 2, 2},
{2, 2, 2, 2},
{3, 3, 3, 3},
{3, 3, 3, 3}};
Output: matrices are identical
Input: a[n][n] = { {2, 2, 2, 2},
{2, 2, 1, 2},
{3,3, 3, 3},
{3,3, 3, 3}};
b[n][n]= { {2, 2, 2, 2},
{2, 2, 5, 2},
{3, 3, 3, 3},
{3, 3, 3, 3}};
Output: matrices are not identical दृष्टिकोण
दोनों मैट्रिसेस a[i][j] और b[i][j] को पुनरावृत्त करें, और a[i][j]==b[i][j] चेक करें यदि सभी के लिए सही है तो प्रिंट करें वे समान हैं अन्यथा प्रिंट वे हैं समान नहीं।
एल्गोरिदम
Start
Step 1 -> define macro as #define n 4
Step 2 -> Declare function to check matrix is same or not
int check(int a[][n], int b[][n])
declare int i, j
Loop For i = 0 and i < n and i++
Loop For j = 0 and j < n and j++
IF (a[i][j] != b[i][j])
return 0
End
End
End
return 1
Step 3 -> In main()
Declare variable asint a[n][n] = { {2, 2, 2, 2},
{2, 2, 2, 2},
{3, 3, 3, 3},
{3, 3, 3, 3}}
Declare another variable as int b[n][n] = { {2, 2, 2, 2},
{2, 2, 2, 2},
{3, 3, 3, 3},
{3, 3, 3, 3}}
IF (check(a, b))
Print matrices are identical
Else
Print matrices are not identical
Stop उदाहरण
#include <bits/stdc++.h>
#define n 4
using namespace std;
// check matrix is same or not
int check(int a[][n], int b[][n]){
int i, j;
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
if (a[i][j] != b[i][j])
return 0;
return 1;
}
int main(){
int a[n][n] = { {2, 2, 2, 2},
{2, 2, 2, 2},
{3, 3, 3, 3},
{3, 3, 3, 3}};
int b[n][n] = { {2, 2, 2, 2},
{2, 2, 2, 2},
{3, 3, 3, 3},
{3, 3, 3, 3}};
if (check(a, b))
cout << "matrices are identical";
else
cout << "matrices are not identical";
return 0;
} आउटपुट
matrices are identical