C++(preferably not C++11) NOTE! - I will expect these projects to utilize good d
ID: 3844190 • Letter: C
Question
C++(preferably not C++11)
NOTE! - I will expect these projects to utilize good design, such as separation of interface from the implementation (ie. classes separated out into separate files, with .h files). Do not put all of the processing in main(). Failure to do this will result in a lower grade.
Create a matrix calculator. This calculator will ask the user to input 2 matrices or a scalar and one matrix. It will ask first for the number of rows, then the number of columns. It will ask for an operation *, +, - It will check to see if the result can be calculated, and will then calculate and display the matrix from the operation selected.
Explanation / Answer
The program is given below:
/*Sum Difference and Product of two matrices*/
#include<iostream>
using namespace std;
void read(int a[][50],int r,int c)
{
cout<<"Enter the elements: ";
int i,j;
for(i=0;i<r;i++)
for(j=0;j<c;j++)
cin>>a[i][j];
cout<<"The matrix is: ";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
}
void add(int a[][50],int b[][50],int r,int c)
{
int i,j,s[50][50];
for(i=0;i<r;i++)
for(j=0;j<c;j++)
s[i][j]=b[i][j]+a[i][j];
cout<<"The sum matrix is:"<<endl;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
cout<<s[i][j]<<" ";
cout<<endl;
}
}
void sub(int a[][50],int b[][50],int r,int c)
{
int i,j,s[50][50];
for(i=0;i<r;i++)
for(j=0;j<c;j++)
s[i][j]=a[i][j]-b[i][j];
cout<<"The difference matrix is: ";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
cout<<s[i][j]<<" ";
cout<<endl;
}
}
void multiply(int a[][50],int b[][50],int m,int n,int p,int q)
{
int i,j,k,flag=0;
int c[50][50];
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]+=a[i][k]*b[k][j];
}
}
cout<<" The product matrix is: ";
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
cout<<c[i][j]<<" ";
cout<<endl;
}
}
main()
{
char x;
int n,i,r[2],c[2];
int a[50][50],b[50][50];
do
{
cout<<"Enter your choice:"<<endl;
cout<<"1. Addition"<<endl;
cout<<"2. Subtraction"<<endl;
cout<<"3. Multiplication"<<endl;
cin>>n;
cout<<"Please enter the two matrices:"<<endl;
for(i=0;i<2;i++)
{
cout<<"Enter the details of the "<<i+1<<"th matrix:"<<endl;
cout<<"Please enter the dimensions of the matrix:"<<endl;
cin>>r[i]>>c[i];
if(i==0)
read(a,r[i],c[i]);
else
read(b,r[i],c[i]);
}
switch(n)
{
case 1:
if(r[0]==r[1] && c[0]==c[1])
add(a,b,r[0],c[0]);
else
cout<<"Dimensions don't match.Addition not possible."<<endl;
break;
case 2:
if(r[0]==r[1] && c[0]==c[1])
sub(a,b,r[0],c[0]);
else
cout<<"Dimensions don't match.Subtraction not possible."<<endl;
break;
case 3:
if(c[0]==r[1])
multiply(a,b,r[0],c[0],r[1],c[1]);
else
cout<<"Matrices are incompatible for multiplication."<<endl;
break;
default:
cout<<"Wrong Choice. Please Try again."<<endl;
}
cout<<"Do you want to continue?(Y/N)";
cin>>x;
}while(x!='n' && x!='N');
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.