As a C Programmer ,you need to deal with data structures likes an array.Programmers often needs to use a Multidimensional array in the form of matrix to do mathematical Calculations.The Program below Demonstrates the addition of any two user specified matrices together and obtain the result in a new Matrix.

The Complete Source Code to implement matrix addition by using 2D Integer Array is given Below.

The Complete Source Code to implement matrix addition by using 2D Integer Array is given Below.

```
#include<stdio.h>
#include<conio.h>
void main()
{
int A[50][50],B[50][50],C[50][50];
int i,j,r1,r2,c1,c2;
clrscr();
printf(" * Matrix Addition* ");
printf("\nEnter The Size Of First Matrix:");
scanf("%d%d",&r1,&c1);
printf("\nEnter The Elements Of First Matrix:");
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
scanf("%d",&A[i][j]);
printf("\n\nEnter The Size Of Second Matrix:");
scanf("%d%d",&r2,&c2);
printf("\nEnter The Elements Of Second Matrix:");
for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
scanf("%d",&B[i][j]);
if(r1!=r2||c1!=c2)
{
printf("\n\nThe Matrix Cant Be Added!!!");
getch();
exit(0);
}
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
C[i][j]=A[i][j]+B[i][j];
printf("\n\nThe Sum Of Matrices Is:");
for(i=0;i<r1;i++)
{ printf("\n");
for(j=0;j<c1;j++)
printf("%d ",C[i][j]);
}
getch();
}
```

Hi.

Is it possible in 2d array that we can add two arrays with different Rows and Cols.

e.g.

A[6][6] + B[3][3]

//Sum of two matrices using two dimensional array

#include

#include

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[4][5], it has 4 rows, each row consisting 5 elements i.e matrix[0] has 5 elements, matrix[1] has 5 element and so on. In this example two matrices are added and result is displayed. Addition is done with corresponding elements of individual matrix i.e. matrix1[0][0] is added with matrix2[0][0].

Nice post. One suggestion remove clrscr(); as it is not supported in compiler like dev c and codeblocks.

See this : write a c program to reverse a string using pointers and recursion

Great and Useful Article.

Java Online Course

Java Online Training

Java EE training

Java Course in Chennai

Java Training in Chennai

Java Training Institutes in Chennai

Java Interview Questions

Java Interview Questions

Thanks a lot. This program is very useful in understanding fundamental concepts of matrix and loops. Here is a similar code

Matrix addition in C

Matrix subtraction in C

Matrix multiplication in C