Sum of two matrices using two dimensional array in C

Matrix is the perfect example of a two-dimensional array. It has row and column. A row represents one dimension and column represents the second dimension. For example matrix[4][5], it has 4 rows, each row consisting 5 elements i.e matrix[0] has 5 elements, matrix[1] has 5 elements and so on. In this example, two matrices are added and the result is displayed. Addition is done with corresponding elements of individual matrix i.e. matrix1[0][0] is added with matrix2[0][0].
The complete source code and output are given here….(The code is also available on GitHub). Here are related articles about matrix operation.

  1. Multiplication of Two Matrices in C

Source Code

//Sum of two matrices using two dimensional array
 
#include <stdio.h>
#include <stdlib.h>
 
int main(){
 int matrix1[10][10], matrix2[10][10], sum[10][10], i, j, m,n,p,q;
 printf("Enter the order of first matrix: ");
 scanf("%d%d",&m,&n);
 printf("Enter the order of second matrix: ");
 scanf("%d%d",&p,&q);
 
 if(m!=p && n!=q){
  printf("Order of matrix did not matched!!");
  exit(0);
 }
 
 
 printf("Enter first matrix: \n");
 for(i = 0 ; i < m; i++){
  for(j = 0; j < n; j++)
   scanf("%d", &matrix1[i][j]);
 }
 
 
 printf("Enter second matrix: \n");
 for(i = 0 ; i < p; i++){
  for(j = 0; j < q; j++)
   scanf("%d", &matrix2[i][j]);
 }
 
 
 for(i = 0 ; i < m; i++){
  for(j = 0; j < n; j++)
  sum[i][j] = matrix1[i][j] + matrix2[i][j];
 }
 
 
 printf("The sum of the matrix is :\n");
 for(i = 0 ; i < m; i++){
  for(j = 0; j < n; j++){
   printf("%d", sum[i][j]);
   printf("\t");
  }
  printf("\n");
 }
 
 return 0;
}

 

matrix
Output

The above program allocates the fixed memory to store the matrix. The program below shows the dynamic memory allocation to allocates the memory to store the matrix.

#include <stdio.h>
#include <stdlib.h>
 
int main(int argc, char *argv[]) {
 int rows, columns, i, j;
 int **matrix1, **matrix2;
 
 if (argc != 3) {
  printf("Usage: outputfile rows columns\n");
  exit(1);
 }
 
 rows = atoi(argv[1]);
 columns = atoi(argv[2]);
 
 // allocate memory for matrices
 matrix1 = (int**) malloc (sizeof(int*) * columns);
 for (i = 0; i < columns; i++) {
  matrix1[i] = (int*) malloc(sizeof(int*) * rows);
 }
 
 matrix2 = (int**) malloc (sizeof(int*) * columns);
 for (i = 0; i < columns; i++) {
  matrix2[i] = (int*) malloc(sizeof(int*) * rows);
 }
 
 // get matrices from user
 printf("Enter the first matrix: \n");
 for(i = 0 ; i < rows; i++)
        for(j = 0; j < columns; j++)
            scanf("%d", &matrix1[i][j]);
     
    printf("Enter the second matrix: \n");
 for(i = 0 ; i < rows; i++)
        for(j = 0; j < columns; j++)
            scanf("%d", &matrix2[i][j]);
 
    // add two matrices
    for(i = 0 ; i < rows; i++){
        for(j = 0; j < columns; j++)
            matrix2[i][j] = matrix1[i][j] + matrix2[i][j];
    }
 
    // display the result
    printf("The sum of the matrix is :\n");
    for(i = 0 ; i < rows; i++){
        for(j = 0; j < columns; j++){
            printf("%d ", matrix2[i][j]);
        }
        printf("\n");
    }
 
    return 0;
 
}
SHARE Sum of two matrices using two dimensional array in C

You may also like...

7 Responses

  1. Thanks nice set of programs for beginners

  2. Nice
    I like it very much sir

  3. Nice
    I like it very much sir

  4. nice
    it is very easy to understand.
    thank u.. sir

  5. how can i write an algorithm for this problem ??? may i know sir

  6. Anonymous says:

    crystal clear

Leave a Reply

Your email address will not be published.

Share