1. Implement a program that contains the following functions: a) A function that
ID: 3537792 • Letter: 1
Question
1. Implement a program that contains the following functions:
a) A function that reads a numeric matrix from the input. The function will first read the dimensions of the matrix and then the elements of the matrix.
b) A function that prints a matrix.
c) A function that performs the sum of two matrices.
d) A function that performs the multiplication of two matrices.
e) A main function that uses the above functions to read two matrices A and B, then compute and print their sum and their multiplication
Explanation / Answer
please rate - thanks
any changes needed, let me know, I'll do it (tested using DEV C++)
#include <iostream>
#include <iomanip>
void input(int[][10],int);
void multMatrix(int[][10], int[][10],int[][10],int);
void addMatrix(int[][10], int[][10],int[][10],int);
void print(int[][10],int,char[]);
using namespace std;
int main()
{
int n,m,i,j;
int a[10][10],b[10][10],c[10][10];
cout<<"Enter number of rows and columns(max 10): ";
cin>>n;
while(n>10)
{cout<<"invalid size ";
cout<<"Enter number of rows (max 10): ";
cin>>n;
}
cout<<"Enter matrix 1 ";
input(a,n);
cout<<"Enter matrix 2 ";
input(b,n);
cout<<"The starting matrices: ";
print(a,n,"1");
print(b,n,"2");
multMatrix(a,b,c,n);
print(c,n, "after multiplication");
addMatrix(a,b,c,n);
print(c,n, "after addition");
system("pause");
return 0;
}
void input(int a[][10],int n)
{int i,j;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{cout<<"Enter element ["<<i<<"]["<<j<<"]: ";
cin>>a[i][j];
}
}
void multMatrix(int a[][10],int b[][10],int c[][10],int n )
{int i,j,k;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
c[i][j]=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
for(k=0;k<n;k++)
c[i][j]+=a[i][k]*b[k][j];
}
void print(int a[][10],int n,char mess[])
{int i,j;
cout<<" matrix "<<mess<<endl;
for(i=0;i<n;i++)
{for(j=0;j<n;j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
}
void addMatrix(int a[][10],int b[][10],int c[][10],int n )
{int i,j,k;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
c[i][j]=a[i][j]+b[i][j];
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.