I have this so far class entry { public: entry(){row=0; col=0; val=0;} entry(int
ID: 3741558 • Letter: I
Question
I have this so far
class entry
{
public:
entry(){row=0; col=0; val=0;}
entry(int r, int c, double v){row=r; col=c; val=v;}
~entry(){}
double getval(){return val;}
int getcol(){return col;}
void display(){cout<<"("<
2 classes in c++:
Mat: a mat class contains a vector of lists of Entries. The User must provide the max number or rows and the max number of column entries.
You should be able to add a row (list of entries), do not allow the number of rows to exceed the max number
Add entries to a row, do not allow the number of entries to exceed the max column number
Display a row (used by display all rows)
Display all rows
CTX: this is a context class, an object that contains objects in order to perform various functions on them. You can think of it as a “chalkboard” if you will, a place where the matrices will be written and operators performed on them.
The CTX contains three Mat *, M1, M2, M3, all should initially be set to NULL
These pointers can be instantiated randomly with number of rows and cols provided by user or by loading a specific file name from the keyboard. M3 is intended to be a results matrix, but options to set M1 or M2 = M3 are required.
If the user attempts to perform a mathematically illegal operation tell them “That operation is illegal” for example if the attempt to add a 2x2 matrix and a 3x3. “That operation is illegal”
Explanation / Answer
#include <iostream.h>
class Mat
{
int M1[10][10],M2[10][10],M3[10][10],x,y,i,j;
public :
void Max_value();
void Display_all_rows();
void sum();
void diff();
};
void Mat::Max_value()
{
cout << "Enter the rows "; cin >> x;
cout << "Enter the columns "; cin >> y;
cout << "Enter elements of first matrix ";
for (i=1; i<=x; i++)
{
for ( j=1; j<=y; j++)
{
cin >> M1[i][j];
}
}
cout << "Enter elements of second matrix ";
for (i=1; i<=x; i++)
{
for (j=1; j<=y; j++)
{
cin >> M2[i][j];
}
}
}
Void Mat:: Display_all_rows()
{
Cout<<”Display all Rows of the first input matrix”;
for (i=1; i<=x; i++)
{
for ( j=1; j<=y; j++)
{
Cout<<M1[i][j];
}
}
Cout<<”Display all Rows of the Second input matrix”;
for (i=1; i<=x; i++)
{
for ( j=1; j<=y; j++)
{
Cout<< M2[i][j];
}
}
void Mat::sum()
{
If(M1[j]=M2[i])
{
cout << "Sum of Matrices 1 and 2 is ";
for (i=1; i<=x; i++)
{
for ( j=1; j<=y; j++)
{
M3[i][j]=M1[i][j]+M2[i][j];
cout << M3[i][j] << "";
}
cout << endl;
}
}
Else
{
Cout<<”operation is illegal”;
}
}
void Mat::diff()
{
If (M1[j]==M2[i])
{
cout << "Difference of Matrices 1 and 2 (1-2) is ";
for (i=1; i<=x; i++)
{
for ( j=1; j<=y; j++)
{
f[i][j] = a[i][j]-c[i][j];
cout << f[i][j] << "";
}
cout << endl;
}
Else
{
Cout<<”Operation is illegal “;
}
}
}
int main()
{
int input;
char ch;
Mat m;
m.values();
do
{
cout << "Enter your choice ";
cout << 1.Display_all_rows " 2. Sum of 1 and 2 " << " 3. Difference of 1 and 2 " ;
cin >> input;
switch (input)
{
Case 1:
m.Display_all_rows();
break;
case 2:
m.sum();
break;
case 3:
m.diff();
break;
}
cout << " Do another y/n?";
cin >> ch;
}
while (ch!= 'n');
cout << " ";
return 0;
}