Write a function matrix_multiplication() to multipy two matrices and put the res
ID: 3764198 • Letter: W
Question
Write a function matrix_multiplication() to multipy two matrices and put the result into a third matrix.
The function takes six arguments altogether, i.e. pointers to the matrices to be multiplied and the resulting matrix and three values for the number of rows, columns and the shared dimension. We want to assume that all values are of type integer. This said, the prototype of the function is:
matrix_multiplication(int* left_matrix, int* right_matrix, int* result_matrix, int rows, int cols, int shared);
Write a quick program that makes use of matrix_multiplication(). You are allowed to "hard-code" the values of the example matrices to be multiplied.
Please i need to have the code with comments.
also the program i need it in C program.
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int a[10][10],b[10][10],c[10][10],n=0,m=0,i=0,j=0,p=0,q=0,k=0;
int *left_matrix,*right_matrix,*result_matrix;
printf("Enter size of 1st 2d array : ");
scanf("%d %d",&n,&m);
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("Enter element no. %d %d :",i,j);
scanf("%d",&a[i][j]);
}
}
printf("Enter size of 2nd 2d array : ");
scanf("%d %d",&p,&q);
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf("Enter element no. %d %d :",i,j);
scanf("%d",&b[i][j]);
}
}
if(m!=p)
{
printf("Multiplication cannot be done ");
exit (0);
}
left_matrix=&a[0][0];
right_matrix=&b[0][0];
result_matrix=&c[0][0];
for(i=0;i<n;i++)
{
for(k=0;k<q;k++)
{
*(result_matrix+(i*10+k))=0;
for(j=0;j<m;j++)
{
*(result_matrix+(i*10+k))+=*(left_matrix+(i*10+j))**(right_matrix+(j*10+k));
}
}
}
for(i=0;i<n;i++)
{
for(j=0;j<q;j++)
{
printf("%d ",c[i][j]);
}
printf(" ");
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.