Cプログラミング
 Computer >> コンピューター >  >> プログラミング >> Cプログラミング

Cで2次元配列に対して算術演算を実行するにはどうすればよいですか?


配列は、単一の名前で保存される関連データ項目のグループです。

たとえば、int student [30]; // studentは、単一の変数名を持つ30のデータ項目のコレクションを保持する配列名です

配列の操作

  • 検索 −特定の要素が存在するかどうかを確認するために使用されます

  • 並べ替え −配列内の要素を昇順または降順で配置するのに役立ちます。

  • トラバース −配列内のすべての要素を順番に処理します。

  • 挿入 −要素を配列に挿入するのに役立ちます。

  • 削除 −配列内の要素を削除するのに役立ちます。

2次元配列の算術演算を実行するために適用されるロジックは次のとおりです-

for(row = 0; row < i; row++){
   for(col = 0;col < j;col++){
      add[row][col] = A[row][col] + B[row][col];
      sub[row][col] = A[row][col] - B[row][col];
      mul[row][col] = A[row][col] * B[row][col];
      div[row][col] = A[row][col] / B[row][col];
      mod[row][col] = A[row][col] % B[row][col];
   }
}

2次元配列のすべての算術演算を出力するために適用されるロジック 次のとおりです-

printf("\nAdd\t Sub\t Mul\t Div\t Mod\n");
printf("-------------------------------\n");
for(row = 0; row < i; row++){
   for(col = 0; col < j; col++){
      printf("\n%d \t ", add[row][col]);
      printf("%d \t ", sub[row][col]);
      printf("%d \t ", mul[row][col]);
      printf("%.2f \t ", div[row][col]);
      printf("%d \t ", mod[row][col]);
   }
}

プログラム

以下は、2次元配列で算術演算を実行するCプログラムです-

#include<stdio.h>
int main(){
   int i, j, row, col,A[20][20], B[20][20];
   int add[10][10], sub[10][10], mul[10][10], mod[10][10];
   float div[10][10];
   printf("enter no: of rows and columns:\n");
   scanf("%d %d", &i, &j);
   printf("enter elements of 1st array:\n");
   for(row= 0; row < i; row++){
      for(col = 0;col < j;col++){
         scanf("%d", &A[row][col]);
      }
   }
   printf("enter elements of 2nd array:\n");
   for(row = 0; row < i; row++){
      for(col = 0;col < j;col++){
         scanf("%d", &B[row][col]);
      }
   }
   for(row = 0; row < i; row++){
      for(col = 0;col < j;col++){
         add[row][col] = A[row][col] + B[row][col];
         sub[row][col] = A[row][col] - B[row][col];
         mul[row][col] = A[row][col] * B[row][col];
         div[row][col] = A[row][col] / B[row][col];
         mod[row][col] = A[row][col] % B[row][col];
      }
   }
   printf("\nAdd\t Sub\t Mul\t Div\t Mod\n");
   printf("-------------------------------\n");
   for(row = 0; row < i; row++){
      for(col = 0; col < j; col++){
         printf("\n%d \t ", add[row][col]);
         printf("%d \t ", sub[row][col]);
         printf("%d \t ", mul[row][col]);
         printf("%.2f \t ", div[row][col]);
         printf("%d \t ", mod[row][col]);
      }
   }
   return 0;
}

出力

上記のプログラムを実行すると、次の結果が得られます-

enter no: of rows and columns:
3 4
enter elements of 1st array:
1 2 4 5 6 7 3 8 3 2 1 8
enter elements of 2nd array:
1 2 1 2 1 3 4 2 1 2 1 1
Add   Sub  Mul  Div  Mod
-------------------------------
2     0    1   1.00  0
4     0    4   1.00  0
5     3    4   4.00  0
7     3    10  2.00  1
7     5    6   6.00  0
10    4    21  2.00   1
7    -1    12  0.00   3
10    6    16   4.00  0
4    2     3    3.00  0
4    0    4    1.00   0
2    0    1    1.00   0
9    7    8    8.00    0

  1. Windows11/10のコマンドプロンプトで算術演算を実行する

    この投稿では、コマンドプロンプトで算術演算を実行するのを支援します。 Windows11/10の場合。コマンドプロンプトウィンドウには、除算の実行に役立つミニ電卓が付属しています 、追加 、乗算 、および減算 2つ以上の番号の場合。操作ごとに非常に簡単なコマンドがあります。 Windows 11/10には、標準、プログラマー、科学、およびその他の種類の電卓を提供する電卓アプリも用意されていますが、コマンドプロンプトを使用して基本的な数学(または小数点なしの初等算術演算)を実行したい場合は、この投稿が役立ちます。 コマンドプロンプトで算術演算を実行する 最初に、Windowsの[検索]ボック

  2. Windows11/10のコマンドプロンプトで算術演算を実行する

    この投稿では、コマンドプロンプトで算術演算を実行するのを支援します。 Windows11/10の場合。コマンドプロンプトウィンドウには、除算の実行に役立つミニ電卓が付属しています 、追加 、乗算 、および減算 2つ以上の番号の場合。操作ごとに非常に簡単なコマンドがあります。 Windows 11/10には、標準、プログラマー、科学、およびその他の種類の電卓を提供する電卓アプリも用意されていますが、コマンドプロンプトを使用して基本的な数学(または小数点なしの初等算術演算)を実行したい場合は、この投稿が役立ちます。 コマンドプロンプトで算術演算を実行する 最初に、Windowsの[検索]ボック