
用户必须输入两个矩阵的顺序以及两个矩阵的元素。然后,比较这两个矩阵。
如果矩阵元素和大小都相等,则表明两个矩阵相等。
如果矩阵大小相等但元素相等不相等,则显示矩阵可以比较,但不相等。
如果大小和元素不匹配,则显示矩阵无法比较。
程序
以下是C程序,用于比较两个矩阵是否相等 -
#include <stdio.h>
#include <conio.h>
main(){
int A[10][10], B[10][10];
int i, j, R1, C1, R2, C2, flag =1;
printf("Enter the order of the matrix A<p>");
scanf("%d %d", &R1, &C1);
printf("Enter the order of the matrix B</p>
<p>");
scanf("%d %d", &R2,&C2);
printf("Enter the elements of matrix A</p>
<p>");
for(i=0; i<r1 i for j the elements of matrix b><p>");
for(i=0; i<r2 i for j a is><p>");
for(i=0; i<r1 i for j printf><p>");
}
printf("MATRIX B is</p>
<p>");
for(i=0; i<r2 i for j printf><p>");
}
/* Comparing two matrices for equality */
if(R1 == R2 && C1 == C2){
printf("Matrices can be compared</p>
<p>");
for(i=0; i<r1 i j if b printf cannot be compared><p>");
exit(1);
}
if(flag == 1 )
printf("Two matrices are equal</p>
<p>");
else
printf("But,two matrices are not equal</p>
<p>");
}</p></r1></p></r2></p></r1></p></r2></p></r1></p></conio.h></stdio.h>
输出
当执行上述程序时,会产生以下结果 -
Run 1: Enter the order of the matrix A 2 2 Enter the order of the matrix B 2 2 Enter the elements of matrix A 1 2 3 4 Enter the elements of matrix B 1 2 3 4 MATRIX A is 1 2 3 4 MATRIX B is 1 2 3 4 Matrices can be compared Two matrices are equal Run 2: Enter the order of the matrix A 2 2 Enter the order of the matrix B 2 2 Enter the elements of matrix A 1 2 3 4 Enter the elements of matrix B 5 6 7 8 MATRIX A is 1 2 3 4 MATRIX B is 5 6 7 8 Matrices can be compared But,two matrices are not equal











