Hello, I hope you are able to add two matrices as per the previous post.
Now, we will go a step further and multiply the matrices. To multiply matrices, it should satisfy the basic condition i.e. number of columns in the first matrix should be equal to the number of rows in the second matrix.
Let's try it out.
Don't forget to post the screenshot of the output in the comment section.
#include <stdio.h>
#include<conio.h>
void main()
{
int mat1[10][10], mat2[10][10], mul[10][10], r1, c1, r2, c2, i, j, k;
clrscr();
printf("Enter rows and column for first matrix: \n");
scanf("%d %d", &r1, &c1);
printf("Enter rows and column for second matrix: \n");
scanf("%d %d",&r2, &c2);
// Column of first matrix should be equal to column of second matrix and
while (c1 != r2)
{
printf("Error! column of first matrix not equal to row of second.\n\n");
printf("Enter rows and column for first matrix:\n ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and column for second matrix: \n");
scanf("%d %d",&r2, &c2);
}
// Storing elements of first matrix.
printf("\nEnter elements of matrix 1:\n");
for(i=0; i<r1; i++)
for(j=0; j<c1; j++)
{
printf("Enter elements mat1[%d][%d]: ",i+1, j+1);
scanf("%d", &mat1[i][j]);
}
// Storing elements of second matrix.
printf("\nEnter elements of matrix 2:\n");
for(i=0; i<r2; i++)
for(j=0; j<c2; j++)
{
printf("Enter elements mat2[%d][%d]: ",i+1, j+1);
scanf("%d",&mat2[i][j]);
}
// Initializing all elements of mul matrix to 0
for(i=0; i<r1; i++)
for(j=0; j<c2; j++)
{
mul[i][j] = 0;
}
// Multiplying matrices mat1 and mat2 and
// storing result in mul matrix
for(i=0; i<r1; i++)
for(j=0; j<c2; j++)
for(k=0; k<c1; k++)
{
mul[i][j]+=mat1[i][k]*mat2[k][j];
}
// Displaying the mul
printf("\nOutput \n Multiplication Matrix:\n");
for(i=0; i<r1; i++)
for(j=0; j<c2; j++)
{
printf("| % d | \t", mul[i][j]);
if(j == c2-1)
printf("\n\n");
}
getch();
}
Now, we will go a step further and multiply the matrices. To multiply matrices, it should satisfy the basic condition i.e. number of columns in the first matrix should be equal to the number of rows in the second matrix.
Let's try it out.
Don't forget to post the screenshot of the output in the comment section.
#include <stdio.h>
#include<conio.h>
void main()
{
int mat1[10][10], mat2[10][10], mul[10][10], r1, c1, r2, c2, i, j, k;
clrscr();
printf("Enter rows and column for first matrix: \n");
scanf("%d %d", &r1, &c1);
printf("Enter rows and column for second matrix: \n");
scanf("%d %d",&r2, &c2);
// Column of first matrix should be equal to column of second matrix and
while (c1 != r2)
{
printf("Error! column of first matrix not equal to row of second.\n\n");
printf("Enter rows and column for first matrix:\n ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and column for second matrix: \n");
scanf("%d %d",&r2, &c2);
}
// Storing elements of first matrix.
printf("\nEnter elements of matrix 1:\n");
for(i=0; i<r1; i++)
for(j=0; j<c1; j++)
{
printf("Enter elements mat1[%d][%d]: ",i+1, j+1);
scanf("%d", &mat1[i][j]);
}
// Storing elements of second matrix.
printf("\nEnter elements of matrix 2:\n");
for(i=0; i<r2; i++)
for(j=0; j<c2; j++)
{
printf("Enter elements mat2[%d][%d]: ",i+1, j+1);
scanf("%d",&mat2[i][j]);
}
// Initializing all elements of mul matrix to 0
for(i=0; i<r1; i++)
for(j=0; j<c2; j++)
{
mul[i][j] = 0;
}
// Multiplying matrices mat1 and mat2 and
// storing result in mul matrix
for(i=0; i<r1; i++)
for(j=0; j<c2; j++)
for(k=0; k<c1; k++)
{
mul[i][j]+=mat1[i][k]*mat2[k][j];
}
// Displaying the mul
printf("\nOutput \n Multiplication Matrix:\n");
for(i=0; i<r1; i++)
for(j=0; j<c2; j++)
{
printf("| % d | \t", mul[i][j]);
if(j == c2-1)
printf("\n\n");
}
getch();
}
Comments
Post a Comment