In this program , we have to input the elements in 2 2D arrays which are a[ ][ ] & b[ ][ ] . So , the no. of rows & columns are stored in m & n respectively . The matrix can only be added if no. of rows & cloumns of both matrices are same . Then the values of 2 matrices a[ ][ ] & b[ ][ ] are inserted.
a[00] + b[00] = c[00]
a[01] + b[01] = c[01]
a[10] + b[10] = c[10]
a[11] + b[11] = c[11]
Using this procedure & statement c[i][j] = a[i][j] +b[i][j] , the matrices are added & result is stored in
matrix c[ ][ ]..
#include<stdio.h>
#include<conio.h>
void main()
{ int a[10][10],b[10][10],c[10][10],i=0,j=0,m,n;
clrscr();
printf("\n program to add 2 matrices");
printf("\n (rows & column should be same to add)");
printf("\n enter no. of rows : ");
scanf("%d",&m);
printf(" enter no. of column : ");
scanf("%d",&n);
printf("now enter 1st matrix : \n");
for(i=0;i<m;i++)
{ for(j=0;j<n;j++)
{ printf("enter a[%d][%d] = ",i,j);
scanf("%d",&a[i][j]);
}
}
printf("now enter 2nd matrix : \n");
for(i=0;i<m;i++)
{ for(j=0;j<n;j++)
{ printf("enter a[%d][%d] = ",i,j);
scanf("%d",&b[i][j]);
}
}
printf("\n matrix after addition = \n");
for(i=0;i<m;i++)
{ for(j=0;j<n;j++)
{ c[i][j] = a[i][j] + b[i][j];
printf("\t %d",c[i][j]);
}
printf("\n");
}
getch();
}
a[00] + b[00] = c[00]
a[01] + b[01] = c[01]
a[10] + b[10] = c[10]
a[11] + b[11] = c[11]
Using this procedure & statement c[i][j] = a[i][j] +b[i][j] , the matrices are added & result is stored in
matrix c[ ][ ]..
#include<stdio.h>
#include<conio.h>
void main()
{ int a[10][10],b[10][10],c[10][10],i=0,j=0,m,n;
clrscr();
printf("\n program to add 2 matrices");
printf("\n (rows & column should be same to add)");
printf("\n enter no. of rows : ");
scanf("%d",&m);
printf(" enter no. of column : ");
scanf("%d",&n);
printf("now enter 1st matrix : \n");
for(i=0;i<m;i++)
{ for(j=0;j<n;j++)
{ printf("enter a[%d][%d] = ",i,j);
scanf("%d",&a[i][j]);
}
}
printf("now enter 2nd matrix : \n");
for(i=0;i<m;i++)
{ for(j=0;j<n;j++)
{ printf("enter a[%d][%d] = ",i,j);
scanf("%d",&b[i][j]);
}
}
printf("\n matrix after addition = \n");
for(i=0;i<m;i++)
{ for(j=0;j<n;j++)
{ c[i][j] = a[i][j] + b[i][j];
printf("\t %d",c[i][j]);
}
printf("\n");
}
getch();
}

No comments:
Post a Comment
Guys if you think something is wrong or should be edit than please do comment.