Matrix Program #1
8. Read two n x n matrices and perform addition of matrices into third matrix and print it.
#include<stdio.h>
void main()
{
int i, j, mat1[10][10], mat2[10][10], mat3[10][10];
int row1, col1, row2, col2;
printf("Enter the number of Rows of Mat1 : ");
scanf("%d", &row1);
printf("Enter the number of Columns of Mat1 : ");
scanf("%d", &col1);
printf("Enter the number of Rows of Mat2 : ");
scanf("%d", &row2);
printf("Enter the number of Columns of Mat2 : ");
scanf("%d", &col2);
/* Before accepting the Elements Check if no of
rows and columns of both matrices is equal */
if (row1 != row2 || col1 != col2)
{
printf("\nOrder of two matrices is not same\n");
return ;
}
printf("\n");
void main()
{
int i, j, mat1[10][10], mat2[10][10], mat3[10][10];
int row1, col1, row2, col2;
printf("Enter the number of Rows of Mat1 : ");
scanf("%d", &row1);
printf("Enter the number of Columns of Mat1 : ");
scanf("%d", &col1);
printf("Enter the number of Rows of Mat2 : ");
scanf("%d", &row2);
printf("Enter the number of Columns of Mat2 : ");
scanf("%d", &col2);
/* Before accepting the Elements Check if no of
rows and columns of both matrices is equal */
if (row1 != row2 || col1 != col2)
{
printf("\nOrder of two matrices is not same\n");
return ;
}
printf("\n");
//Accept the Elements in Matrix 1
for (i = 0; i < row1; i++)
{
for (j = 0; j < col1; j++)
{
printf("Enter the Element a[%d][%d] : ", i, j);
scanf("%d", &mat1[i][j]);
}
}
printf("\n");
//Accept the Elements in Matrix 2
for (i = 0; i < row2; i++)
{
for (j = 0; j < col2; j++)
{
printf("Enter the Element b[%d][%d] : ", i, j);
scanf("%d", &mat2[i][j]);
}
}
//Print out the Matrix 1
printf("\nThe First Matrix is : \n");
for (i = 0; i < row1; i++)
{
for (j = 0; j < col1; j++)
{
printf("%d\t", mat1[i][j]);
}
printf("\n");
}
//Print out the Matrix 2
printf("\nThe Second Matrix is : \n");
for (i = 0; i < row2; i++)
{
for (j = 0; j < col2; j++)
{
printf("%d\t", mat2[i][j]);
}
printf("\n");
}
//Addition of two matrices
for (i = 0; i < row1; i++)
{
for (j = 0; j < col1; j++)
{
mat3[i][j] = mat1[i][j] + mat2[i][j];
}
}
//Print out the Resultant Matrix
printf("\nThe Addition of two Matrices is : \n");
for (i = 0; i < row1; i++)
{
for (j = 0; j < col1; j++)
{
printf("%d\t", mat3[i][j]);
}
printf("\n");
}
}
Output
- Enter the number of Rows of Mat1 : 3
Enter the number of Columns of Mat1 : 3
Enter the number of Rows of Mat2 : 3
Enter the number of Columns of Mat2 : 3
Enter the Element a[0][0] : 1
Enter the Element a[0][1] : 2
Enter the Element a[0][2] : 3
Enter the Element a[1][0] : 1
Enter the Element a[1][1] : 2
Enter the Element a[1][2] : 3
Enter the Element a[2][0] : 1
Enter the Element a[2][1] : 2
Enter the Element a[2][2] : 3
Enter the Element b[0][0] : 1
Enter the Element b[0][1] : 2
Enter the Element b[0][2] : 3
Enter the Element b[1][0] : 1
Enter the Element b[1][1] : 2
Enter the Element b[1][2] : 3
Enter the Element b[2][0] : 1
Enter the Element b[2][1] : 2
Enter the Element b[2][2] : 3
The First Matrix is :
1 2 3
1 2 3
1 2 3
The Second Matrix is :
1 2 3
1 2 3
1 2 3
The Addition of two Matrices is :
2 4 6
2 4 6
2 4 6
- Enter the number of Rows of Mat1 : 2
Enter the number of Columns of Mat1 : 3
Enter the number of Rows of Mat2 : 3
Enter the number of Columns of Mat2 : 2
Order of two matrices is not same
Note:
- One Matrix can be added with another only if the order of both matrices is Equal
- No of rows of MAT-1 = No of rows of MAT-2
- No of col of MAT-1 = No of col of MAT-2
- During addition a[0][0] is added with b[0][0] and result is stored in c[0][0]
Comments
Post a Comment