Write a code (c language) that multiples 2 matrices and shows what terms were mu
ID: 3688570 • Letter: W
Question
Write a code (c language) that multiples 2 matrices and shows what terms were multiplied in producing the results.
Your code should use the symbolic constants M, N, Q to define the dimensions according to below.
Declare the arrays A, B, C of the type ‘double’.
A (MxN) x B (NxQ) = C (MxQ)
Below is a sample output.
Matrix A is 4x3
1 2 3
4 5 6
7 8 9
10 11 12
Matrix B is 3x2
1 2
3 4
5 6
Multiplying C = A x B...
C[0][0] = 1*1 + 3*2 + 5*3 = 22
C[0][1] = 2*1 + 4*2 + 6*3 = 28
C[1][0] = 1*4 + 3*5 + 5*6 = 49
C[1][1] = 2*4 + 4*5 + 6*6 = 64
C[2][0] = 1*7 + 3*8 + 5*9 = 76
C[2][1] = 2*7 + 4*8 + 6*9 = 100
C[3][0] = 1*10 + 3*11 + 5*12 = 103
C[3][1] = 2*10 + 4*11 + 6*12 = 136
Matrix C is 4x2
22 28
49 64
76 100
103 136
Explanation / Answer
#include <stdio.h>
double main()
{
double A[10][10], B[10][10], C[10][10],sum;
int i, j, k, M, N, Q;
printf("Enter the number of rows of 1st matrix: ");
scanf("%d", &M);
printf("Enter the number of columns of the 1st matrix or number of rows of the 2nd matric: ");
scanf("%d",&N);
printf("Enter the number of columns of 2nd matrix: ");
scanf("%d", &Q);
/* Storing elements of first matrix. */
printf(" The First matrix is ");
for(i=0; i<M; i++)
for(j=0; j<N; j++)
{
scanf("%lf",&A[i][j]);
}
/* Storing elements of second matrix. */
printf(" The Second matrix is ");
for(i=0; i<N; i++)
for(j=0; j<Q; j++)
{
scanf("%lf",&B[i][j]);
}
/* Displaying elements of First matrix. */
printf(" Matrix A is %d*%d: ",M,N);
for(i=0;i<M;i++){
printf(" ");
for(j=0;j<N;j++){
printf("%lf ",A[i][j]);
}
}
/* Displaying elements of second matrix. */
printf(" Matrix B is %d*%d: ",N,Q);
for(i=0;i<N;i++){
printf(" ");
for(j=0;j<Q;j++){
printf("%lf ",B[i][j]);
}
}
/* Computing the value of the third matrix matrix. */
for(i=0;i<M;i++)
for(j=0;j<Q;j++)
C[i][j]=0;
for(i=0;i<M;i++){ //row of first matrix
for(j=0;j<Q;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;
}
}
/* Displaying the multiplication of two matrix. */
printf(" Matrix C is %d * %d: ",M,Q);
for(i=0;i<M;i++){
printf(" ");
for(j=0;j<Q;j++){
printf("%lf ",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.