Hello,
Now we will code for addition of matrices. To do that, we have to develop a logic and step by step you should proceed towards the execution of the program.
Steps involved in the execution are:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,r1,c1,mat1[10][10],mat2[10][10],sum[10][10];
clrscr();
printf("Enter dimensions of a square matrix \n");
scanf("%d %d",&r1,&c1);
printf("Enter elements of First Matrix:\n");
for (i = 0; i<r1;i++)
{
for (j=0;j<c1;j++)
{
printf("Enter mat1[%d][%d]=",i,j);
scanf("%d",&mat1[i][j]);
}
}
printf("Enter elements of Second Matrix:\n");
for (i = 0; i<r1;i++)
{
for (j=0;j<c1;j++)
{
printf("Enter mat2[%d][%d]=",i,j);
scanf("%d",&mat2[i][j]);
}
}
printf("Sum of entered matrices:-\n");
for (i= 0; i< r1; i++) {
for (j = 0 ; j < c1; j++) {
sum[i][j] = mat1[i][j] + mat2[i][j];
printf("| % d | \t", sum[i][j]);
}
printf("\n");
}
getch();
}
Now we will code for addition of matrices. To do that, we have to develop a logic and step by step you should proceed towards the execution of the program.
Steps involved in the execution are:
- Code for getting dimensions and getting the elements of both the matrix. You are preferably using for loops
- Print both the matrices on the console
- Add matrices and print them on the console screen.
You are highly encouraged to post the screenshot of the output in the comment section.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,r1,c1,mat1[10][10],mat2[10][10],sum[10][10];
clrscr();
printf("Enter dimensions of a square matrix \n");
scanf("%d %d",&r1,&c1);
printf("Enter elements of First Matrix:\n");
for (i = 0; i<r1;i++)
{
for (j=0;j<c1;j++)
{
printf("Enter mat1[%d][%d]=",i,j);
scanf("%d",&mat1[i][j]);
}
}
printf("Enter elements of Second Matrix:\n");
for (i = 0; i<r1;i++)
{
for (j=0;j<c1;j++)
{
printf("Enter mat2[%d][%d]=",i,j);
scanf("%d",&mat2[i][j]);
}
}
printf("Sum of entered matrices:-\n");
for (i= 0; i< r1; i++) {
for (j = 0 ; j < c1; j++) {
sum[i][j] = mat1[i][j] + mat2[i][j];
printf("| % d | \t", sum[i][j]);
}
printf("\n");
}
getch();
}
Comments
Post a Comment