# 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, it has 4 rows, each row consisting 5 elements i.e matrix has 5 elements, matrix 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 is added with matrix2.
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, matrix2, sum, 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;
}

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);
columns = atoi(argv);

// 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

### 7 Responses

1. Aman Rustagi says:

Thanks nice set of programs for beginners

2. Rushikesh Pawar says:

Nice
I like it very much sir

3. Rushikesh Pawar says:

Nice
I like it very much sir

4. Saladi Karthik says:

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

5. Sajha Manch says:

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

6. Anonymous says:

crystal clear

7. Shuvo kumer Ray says:

nice post . thank you 