Computer >> Computer tutorials >  >> Programming >> C programming

How to perform arithmetic operations on two-dimensional array in C?


An array is a group of related data items that are stored with single name.

For example, int student[30]; //student is an array name that holds 30 collection of data items with a single variable name

Operations of array

  • Searching − It is used to find whether particular element is present or not

  • Sorting − It helps in arranging the elements in an array either in ascending or descending order.

  • Traversing − It processes every element in an array sequentially.

  • Inserting − It helps in inserting the elements in an array.

  • Deleting − It helps in deleting an element in an array.

The logic applied to perform arithmetic operation of two-dimensional array is as follows −

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];
   }
}

The logic applied to print all arithmetic operation of two-dimensional array is as follows −

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]);
   }
}

Program

Following is the C program to perform arithmetic operations on two-dimensional array −

#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;
}

Output

When the above program is executed, it produces the following result −

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