C Program to Add Two Matrices Using Multi-dimensional Arrays
In this program, the user is asked to enter the number of rows r and columns c. The value of r and c should be less than 100 in this program.
The user is asked to enter elements of two matrices (of order r*c).
Then, the program adds these two matrices, saves it in another matrix (two-dimensional array), and displays it on the screen.
Example: Program to Add Two Matrices
#include <stdio.h>
int main(){
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
printf("Enter number of rows (between 1 and 100): ");
scanf("%d", &r);
printf("Enter number of columns (between 1 and 100): ");
scanf("%d", &c);
printf("\nEnter elements of 1st matrix:\n");
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf("Enter element a%d%d: ",i+1,j+1);
scanf("%d",&a[i][j]);
}
printf("Enter elements of 2nd matrix:\n");
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf("Enter element a%d%d: ",i+1, j+1);
scanf("%d", &b[i][j]);
}
// Adding Two matrices
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
sum[i][j]=a[i][j]+b[i][j];
}
// Displaying the result
printf("\nSum of two matrices: \n");
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
printf("%d ",sum[i][j]);
if(j==c-1)
{
printf("\n\n");
}
}
return 0;
}
Output
Enter number of rows (between 1 and 100): 2 Enter number of columns (between 1 and 100): 3 Enter elements of 1st matrix: Enter element a11: 2 Enter element a12: 3 Enter element a13: 4 Enter element a21: 5 Enter element a22: 2 Enter element a23: 3 Enter elements of 2nd matrix: Enter element a11: -4 Enter element a12: 5 Enter element a13: 3 Enter element a21: 5 Enter element a22: 6 Enter element a23: 3 Sum of two matrices: -2 8 7 10 8 6
C program to add two matrices using multi-dimensional arrays:
#include <stdio.h>
#define ROW 3
#define COL 3
int main()
{
int matrix1[ROW][COL], matrix2[ROW][COL], result[ROW][COL];
int i, j;
// Input matrix1
printf("Enter elements of matrix1 (%d x %d):\n", ROW, COL);
for (i = 0; i < ROW; i++) {
for (j = 0; j < COL; j++) {
scanf("%d", &matrix1[i][j]);
}
}
// Input matrix2
printf("Enter elements of matrix2 (%d x %d):\n", ROW, COL);
for (i = 0; i < ROW; i++) {
for (j = 0; j < COL; j++) {
scanf("%d", &matrix2[i][j]);
}
}
// Add matrix1 and matrix2
for (i = 0; i < ROW; i++) {
for (j = 0; j < COL; j++) {
result[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
// Output result
printf("\nResult matrix (%d x %d):\n", ROW, COL);
for (i = 0; i < ROW; i++) {
for (j = 0; j < COL; j++) {
printf("%d ", result[i][j]);
}
printf("\n");
}
return 0;
}
This program first defines two matrices matrix1
and matrix2
with dimensions ROW x COL
, as well as a result matrix result
with the same dimensions. It then prompts the user to input the elements of matrix1
and matrix2
, and adds them together using a nested loop. Finally, it outputs the resulting matrix result
.
Note that the program assumes that the dimensions of matrix1
and matrix2
are the same in order for the addition to be valid. If this is not the case, the program will not produce correct results.