Program to Add Two Matrices Using Multi-dimensional Arrays - ( C Programs )
Example
#include <stdio.h>
int main()
{
int matrix1[3][3], matrix2[3][3], resultMatrix[3][3];
int i, j;
printf("Enter the elements of matrix 1:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d", &matrix1[i][j]);
}
}
printf("Enter the elements of matrix 2:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d", &matrix2[i][j]);
}
}
printf("Resultant Matrix:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
resultMatrix[i][j] = matrix1[i][j] + matrix2[i][j];
printf("%d ",resultMatrix[i][j]);
}
printf("\n");
}
return 0;
}
Output
Enter the elements of matrix 1:
1 2 3
4 5 6
7 8 9
Enter the elements of matrix 2:
9 8 7
6 5 4
3 2 1
Resultant Matrix:
10 10 10
10 10 10
10 10 10
Explanation
This program takes two matrices (each matrix of size 3x3) and adds them together to create a new matrix. The matrices are entered by the user, and the program performs the addition operation using nested for loops.
Use
This program can be used to add two matrices of any size. It can be modified accordingly by changing the size of matrices and modifying the loops in the program.
Summary
The program to add two matrices using multi-dimensional arrays is a simple C program that takes two matrices as input, uses nested for loops to add them together, and creates a new matrix as output. The program can be modified to work with matrices of different sizes.