사용자는 두 행렬의 순서와 두 행렬의 요소를 입력해야 합니다. 그런 다음 이 두 행렬을 비교합니다.
행렬 요소와 크기가 모두 같으면 두 행렬이 같다고 표시합니다.
행렬의 크기는 같지만 요소가 같지 않으면 행렬을 비교할 수 있지만 같지 않은 것으로 표시합니다.
크기와 요소가 일치하지 않으면 행렬을 비교할 수 없다고 표시합니다.
프로그램
다음은 두 행렬이 같은지 여부를 비교하는 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\n");
scanf("%d %d", &R1, &C1);
printf("Enter the order of the matrix B\n");
scanf("%d %d", &R2,&C2);
printf("Enter the elements of matrix A\n");
for(i=0; i<R1; i++){
for(j=0; j<C1; j++){
scanf("%d",&A[i][j]);
}
}
printf("Enter the elements of matrix B\n");
for(i=0; i<R2; i++){
for(j=0; j<C2; j++){
scanf("%d",&B[i][j]);
}
}
printf("MATRIX A is\n");
for(i=0; i<R1; i++){
for(j=0; j<C1; j++){
printf("%3d",A[i][j]);
}
printf("\n");
}
printf("MATRIX B is\n");
for(i=0; i<R2; i++){
for(j=0; j<C2; j++){
printf("%3d",B[i][j]);
}
printf("\n");
}
/* Comparing two matrices for equality */
if(R1 == R2 && C1 == C2){
printf("Matrices can be compared\n");
for(i=0; i<R1; i++){
for(j=0; j<C2; j++){
if(A[i][j] != B[i][j]){
flag = 0;
break;
}
}
}
}
else{
printf(" Cannot be compared\n");
exit(1);
}
if(flag == 1 )
printf("Two matrices are equal\n");
else
printf("But,two matrices are not equal\n");
} 출력
위의 프로그램이 실행되면 다음과 같은 결과가 생성됩니다 -
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