This code calculates the product of two matrices entered by the user. It uses the standard libraries `stdio.h`, `stdlib.h`, and `string.h`. After clearing the screen, it prompts the user to enter the number of rows and columns for the matrices. It then declares three matrices: `a`, `b`, and `product`. The code proceeds to prompt the user to enter the elements of the first and second matrices. Next, it computes the product of the matrices using nested loops and stores the result in the `product` matrix. Finally, it prints the resulting matrix.
Source Code :
MatrixProductSameOrder.C
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
int row,columns;
// this line will clear the screen using stdlib.h file
system("cls");
// getting number of rows and columns from user
printf("Enter the number of Rows and Columns of the matrices : ");
scanf("%d %d", &row,&columns);
printf("\n");
// sorry i forget to declare matrices
// you should declare matrix after
// geting row and columns
int a[row][columns],b[row][columns],product[row][columns];
// now to get elements of matrix from user
printf("-> Enter elements of matrix 1 of order %d X %d\n", row,columns);
printf("\n");
// starting loop
// matrix 1
for (int i = 0; i < row; i++)
{
for (int j = 0; j < columns; j++)
{
printf("Enter the value of %d %d element : ", i+1, j+1);
scanf("%d", &a[i][j]);
}
}
printf("\n");
printf("-> Enter elements of matrix 2 of order %d X %d\n", row,columns);
printf("\n");
// starting loop
// matrix 2
for (int i = 0; i < row; i++)
{
for (int j = 0; j < columns; j++)
{
printf("Enter the value of %d %d element : ", i+1, j+1);
scanf("%d", &b[i][j]);
}
}
// now to make logic to find product of these two matrix
for (int i = 0; i < row; i++)
{
for (int j = 0; j < columns; j++)
{
product[i][j]=0;
for (int k = 0; k < columns; k++)
{
product[i][j] += a[i][k] * b[k][j];
}
}
}
// To print the resultant matrix
printf("\n");
printf("Product of the matrices is : \n");
for (int i = 0; i < row; i++)
{
for (int j = 0; j < columns; j++)
{
printf("%d\t", product[i][j]);
}
printf("\n");
}
return 0;
}
Follow for more code and there source codes
THANK YOU
Comments
Post a Comment