C Program to add two dimensional matrices

Write a C program to add two-dimensional matrices by fixing the row and column size as 2 using macros?

Multi dimensional array are those type of array, which has a finite number of rows and a finite number of columns. The 2-dimensional array is declared by
Data_type Array_name [row size][column size];

This program takes two matrices of order r*c and stores it in a two-dimensional array. Then, the program adds these two matrices and displays it on the screen.

C Program Code

#include<stdio.h>
#include<stdlib.h>
#define m 2
int main()
{
int i,j;
int a[m][m],b[m][m],c[m][m];
void add(int(*)[m]);

printf(“Enter the elements of matrix 1:”);
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
scanf(“%d”,a[i][j]);
}
}
printf(“Enter 2nd matrix elements:”);
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
scanf(“%d”,b[i][j]);
}
}
printf(“The sum of 2 entered matrices is :”);
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
printf(“%d”,c[i][j]);
}
printf(“\n”);
}
void add(int c[m][m])
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
c[i][j]=a[i]+b[i][j];
}

}

 

Leave a comment