2つの行列が等しいかどうかを比較するCプログラム
ユーザーは、2つの行列と2つの行列の要素の順序を入力する必要があります。次に、これら2つのマトリックスが比較されます。
行列要素とサイズの両方が等しい場合、2つの行列が等しいことを示します。
行列のサイズは等しいが要素が等しくない場合、行列は比較できますが等しくないことが表示されます。
サイズと要素が一致しない場合は、行列を比較できないことが表示されます。
プログラム
以下は、2つの行列が等しいかどうかを比較する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>
-
Cのトークンは何ですか?
トークンは、コンパイラにとって意味のあるプログラムの最小要素に他なりません。プログラムを最小単位に分割するコンパイラはトークンと呼ばれ、これらのトークンはコンパイルのさまざまな段階に進みます。 タイプ トークンはさまざまなタイプに分類されます。以下に説明します- キーワード 識別子 定数 文字列 特別な記号 オペレーター 例 以下に示すのは、Cプログラムの識別子、キーワード、変数などの使用です。 。 #include <stdio.h> int main(){ int a,b,c; printf("ente
-
正方行列をCでZ形式で印刷するプログラム
プログラムの説明 正方行列の要素をZ形式で印刷します 正方行列は、同じ数の行と列を持つ行列です。 n行n列の行列は次数の正方行列として知られています アルゴリズム To print the elements of the Square Matrix in Z form We need to print the first row of matrix then diagonal and then last row of the square matrix. 例 /* Program to print a square matrix in Z form */ #include<st