Write a program that de%uFB01nes 3 matrices A, B and C of size 3x3 with int elem
ID: 3536542 • Letter: W
Question
Write a program that de%uFB01nes 3 matrices A, B and C of size 3x3 with int elements. The
matrizes should be represented as 2D-arrays. Have the user input matrizes A and B row-by
row, as shown in the below example. Compute the matrix product of A and B and store the
result in C (i.e., C = A*B). Print matrix C on the screen. You can %uFB01nd more information on the matrix product at the following wikipedia page (scroll down to %u201CMatrix Product%u201D):
http://en.wikipedia.org/wiki/Matrix_multiplication.
To format the output of matrix C, you must determine the largest number occurring in matrix C,
and output all numbers with this %uFB01eld width to make matrix columns aligned. For example, in
the below result matrix C, the largest number is 12. 12 has 2 digits, so all numbers must be
output with a %uFB01eld width of two. Note also the additional blank to separate columns.
Enter matrix A:
1 2 3
1 2 3
1 1 1
Enter matrix B:
1 2 1
1 2 1
2 2 3
Product matrix C:
9 12 12
9 12 12
4 6 5
Explanation / Answer
#include<stdio.h>
int main(){
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("%d ",a[i][j]);
}
}
printf(" The Second matrix is ");
for(i=0;i<o;i++){
printf(" ");
for(j=0;j<p;j++){
printf("%d ",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("%d ",c[i][j]);
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.