C Program code to add two matrices

This is a sample C Program Code to add two matrices of any order.

If you find any error or if you could suggest any changes in the code please let me know in the comment section.


PROGRAM CODE: 
#include<stdio.h>
#include<conio.h>
void getmatrix(int a[100][100],int r,int c);
void dispmatrix(int a[100][100],int r,int c);
int main()
{
int a[100][100],b[100][100],s[100][100],i,j,r1,c1,r2,c2,r,c;
printf("Enter the number of rows of the first matrix : ");
scanf("%d",&r1);
printf("Enter the number of columns of the first matrix : ");
scanf("%d",&c1);
printf("Enter the first matrix : ");
getmatrix(a,r1,c1);
dispmatrix(a,r1,c1);
printf("\nEnter the number of rows of the second matrix : ");
scanf("%d",&r2);
printf("Enter the number of columns of the second matrix : ");
scanf("%d",&c2);
printf("Enter the second matrix : ");
getmatrix(b,r2,c2);
dispmatrix(b,r2,c2);
if(r1>r2)
r=r1;
else
r=r2;
if(c1>c2)
c=c1;
else
c=c2;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
*(*(s+i)+j)=0;
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
*(*(s+i)+j)=*(*(a+i)+j)+*(*(b+i)+j);
}
}
printf("\nThe sum of the matrix is : \n");
dispmatrix(s,r,c);
return(0);
}
void getmatrix(int a[100][100],int r,int c)
{
int i,j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",*(a+i)+j);
}
}
}
void dispmatrix(int a[100][100],int r,int c)
{
int i,j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("\t%d",*(*(a+i)+j));
}
printf("\n");
}
}
view raw gistfile1.txt hosted with ❤ by GitHub


Comments