This should be solved in the matrix.cpp file. Write a program which .docx
1. This should be solved in the matrix.cpp file. Write a program
which multiplies two matrices. The matrices may be any size,
contain integers, and will come as input from the user. Each
matrix will be input with the columns separated by spaces and
the rows each on a new line. The end of each matrix will be
specified by an empty line with no integers. Your program
should print the resulting matrix with each column separated by
a space, and each row on a new line. Remember that the matrix
product is defined as: (AB)_jf = sum of (A_ik * B_kj) for k = 1
to m (where m is the number of columns in A) Your program
should output an error if the dimensions of the input matrices
are incompatible (the number of columns in the first is not equal
to the number of rows in the second). Each input matrix should
be stored in a multidimensional integer array. You may also
want to use a multidimensional array to store the result matrix.
All three matrices have sizes less or equal 10 by 10. The
program should print a string of text to the terminal before
getting input from the user. A session should look like one of
the following examples (including whitespace and formatting),
with a possibly matrix in the output: Enter first matrix: 1 2 3 m
Enter second matrix: 7 8 9 0 1 2 The product is: 28 14 79 44
Enter first matrix: 12 3 Enter second matrix: 4 5 6 The two
matrices have incompatible dimensions. Each string printed by
the program should include a newline at the end. but no other
trailing whitespace.
Solution
int main(){
2. int a[5][5],b[5][5],c[5][5],i,j,k,sum=0,m,n,o,p;
printf(" Enter the row and column of first matrix");
scanf("%d %d",&m,&n);
printf(" Enter the row and column of second matrix");
scanf("%d %d",&o,&p);
if(n!=o){
printf("Matrix mutiplication is not possible");
printf(" Column of first matrix must be same as row of second
matrix");
}
else{
printf(" Enter the First matrix->");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf(" Enter the Second matrix->");
for(i=0;i<o;i++)
for(j=0;j<p;j++)
scanf("%d",&b[i][j]);
printf(" The First matrix is ");
for(i=0;i<m;i++){
printf(" ");
for(j=0;j<n;j++){
printf("%dt",a[i][j]);
}
3. }
printf(" The Second matrix is ");
for(i=0;i<o;i++){
printf(" ");
for(j=0;j<p;j++){
printf("%dt",b[i][j]);
}
}
for(i=0;i<m;i++)
for(j=0;j<p;j++)
c[i][j]=0;
for(i=0;i<m;i++){ //row of first matrix
for(j=0;j<p;j++){ //column of second matrix
sum=0;
for(k=0;k<n;k++)
sum=sum+a[i][k]*b[k][j];
c[i][j]=sum;
}
}
}
printf(" The multiplication of two matrix is ");
for(i=0;i<m;i++){
printf(" ");
for(j=0;j<p;j++){
printf("%dt",c[i][j]);