in C Write a function matrix_multiplication() to multipy two matrices and put th
ID: 3817323 • Letter: I
Question
in C
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.
Explanation / Answer
#include<stdio.h>
int main()
{
int a[10][10],b[10][10],c[10][10],sum=0;
int m1,m2,n1,n2,i,j,k;
int *ptr1,*ptr2,*ptr3;
ptr1=a;ptr2=b;ptr3=c;
printf("enter no of row and column for 1st matrix ");
scanf("%d%d",&m1,&n1);
printf("enter no of row and column for 2nd matrix ");
scanf("%d%d",&m2,&n2);
if(n1==m2)
{
printf("for 1st matrix ");
for(i=0;i<m1;i++)
for(j=0;j<n1;j++)
{
printf("a[%d][%d]=",i,j);
scanf("%d",ptr1+i*10+j);
}
printf(" for 2nd matrix ");
for(i=0;i<m2;i++)
for(j=0;j<n2;j++)
{
printf("b[%d][%d]=",i,j);
scanf("%d",ptr2+i*10+j);
}
for(i=0;i<m1;i++)
{
for(j=0;j<n2;j++)
{
for(k=0;k<n2;k++)
sum+=*(ptr1+i*10+k)**(ptr2+k*10+j);
*(ptr3+i*10+j)=sum;
sum=0;
}
}
printf("product of entered matrices :- ");
for(i=0;i<m1;i++)
{
for(j=0;j<n2;j++)
printf("%d ",*(ptr3+i*10+j));
printf(" ");
}
}
else
printf("multiplication is not possible");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.