Vous êtes sur la page 1sur 3

#include<stdio.

h>
#include<math.h>
int main()
{
//define the integer variables
int rx,cx,a,ry,cy,b,i,j,k,sum=0;
//define the maximum size of matrix the user can input
int x[100][100],y[100][100],z[100][100];
printf("This is the program of matrices multiplication\n");
printf("You are free to enter any size of matrices up to [100][100]\n");
printf("\n");
printf("Please enter the size of the first matrix you are going to multiply\n");
scanf("%d%d",&rx,&cx);
//formula to define how many elements the user should input
a=rx*cx;
printf("Please define your first matrix\n");
printf("Enter %d elements of your matrix\n",a);
//loop for user to input elements
for(i=0;i<rx;i++)
for(j=0;j<cx;j++)
scanf("%d",&x[i][j]);
printf("Your first matrix is:\n");
//loop to print in matrix style
for(i=0;i<rx;i++)
{
for(j=0;j<cx;j++)

printf("%d\t",x[i][j]);
printf("\n");
}
printf("Please enter the size of the secondmatrix you are going to
multiply\n");
scanf("%d%d",&ry,&cy);
b=ry*cy;
//condition if the second matrix size is compatible
if(cx==ry)
{
printf("Please define your second matrix\n");
printf("Enter %d elements of your matrix\n",b);
for(i=0;i<ry;i++)
for(j=0;j<cy;j++)
scanf("%d",&y[i][j]);
printf("Your second matrix is:\n");
for(i=0;i<ry;i++)
{
for(j=0;j<cy;j++)
printf("%d\t",y[i][j]);
printf("\n");
}
//loop for calculation
for(i=0;i<rx;i++)
{
for(j=0;j<cy;j++)
{

for(k=0;k<ry;k++)
{
sum=sum+x[i][k]*y[k][j];
}
z[i][j]=sum;
sum=0;
}
}
printf("The product of your matrices is:\n");
for(i=0;i<rx;i++)
{
for(j=0;j<cy;j++)
printf("%d\t", z[i][j]);
printf("\n");
}
}
//condition if the second matrix is not compatible
else
{
printf("Multiplication is not possible, please re-enter the right matrix\n");
}
printf("Hope you enjoy the program\n");
return 0

;
}

Vous aimerez peut-être aussi