Design a program to perform matrix multiplication. The product C of two matrices
ID: 655302 • Letter: D
Question
Design a program to perform matrix multiplication. The product C of two matrices A and B is defined as
where is summed over for all possible values of and
In order for matrix multiplication to be defined, the dimensions of the matrices must satisfy
where denotes a matrix with rows and columns. Writing out the product explicitly,
where
For example,
Requirements:
Your program needs to prompt the user for input file directory and file name.
Your program needs to be able to handle up to 6 by 6 matrix calculation.
Your program needs to determine if the two input matrices satisfy the condition expressed in equation (2), don
Explanation / Answer
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[6][6],b[6][6],c[6][6],m,n,p,q,i,j,k;
cout<<"Enter r and c of 1st matrix:";
cin>>m>>n;
cout<<"Enter r and c of 2nd matrix:";
cin>>p>>q;
if(n==p)
{
cout<<" Enter 1st matrix: ";
for(i=0;i<m;++i)
for(j=0;j<n;++j)
cin>>a[i][j];
cout<<" Enter 2nd matrix: ";
for(i=0;i<p;++i)
for(j=0;j<q;++j)
cin>>b[i][j];
cout<<" The new matrix is: ";
for(i=0;i<m;++i)
{
for(j=0;j<q;++j)
{
c[i][j]=0;
for(k=0;k<n;++k)
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
cout<<c[i][j]<<" ";
}
cout<<" ";
}
}
else
cout<<" mm not possible ";
getch();
}
/*use for file formation*/
int main() {
FILE *fp;
struct mm;
fp = fopen("output.txt", "w");
fwrite(&s1, sizeof(mm), 1, fp);
fclose(fp);
fp = fopen("input.txt", "r");
fread(&s2, sizeof(mm), 1, fp);
fclose(fp);
printf(" multiplication : %d", mm.multiplication);
return (0);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.