Not sure if anyone is willing to help. All I need is a starting point. I\'m conf
ID: 3664353 • Letter: N
Question
Not sure if anyone is willing to help. All I need is a starting point. I'm confused as to how to put a list of class within another, then within an object Mat. Any help/clarification of this problem would be greatly appreciated. High rating for anything that's helpful.
Compressed Row Matrix Algebra
1. Create an object Mat that will contain a list of class Row
2. Class Row will contain a list of class val
3. Class Val contains a value x and a column number c
Each Mat object should be able to display all of its contents
Each Mat object will have the following mathematical functions associated within:
Add two Mat objects
Multiply two Mat objects
You are to develop a menu driven interface that allows the user to:
A. Load Mat A from file
B. Load Mat B from file
C. Enter A from keyboard
D. Enter B from keyboard
E. Compute A = A+B
F. Compute A = A*B
G. Save A to file
H. Save B to file
I. Display A
J. Display B
K. Quit
Explanation / Answer
Any matrix can be called as collection of Rows & Columns.
any Matrix
[
[1 2 3] -- Row 1
[4 5 6] -- Row 2
[7 8 9] -- Row 3
]
As mentioned inquestion, MATRIX is a list of class ROWS
To start with, you need to
* Create structures where data will be stored.
A good way will be to Create the classes Row & Matrix. And inherit make Matrix to inherit from class Row.
*
Part (A).
class Val{
public :
int x; // value
int c; // colnumber
Val(int x, int c){
x=x; c=c;
}
};
class Row : public Val{
public :
int length;
Val *arr;
Row(int n){
length = n;
arr = new Val[length];
for(int i=0; i<n; i++)arr[i]=Val(0,i);
}
};
class Matrix : public Row{
int noOfrows, noOfcols;
class *RowInstance; // for a list of classes
public :
Matrix(int nrows, int ncols):Row(ncols){
noOfrows=nrows;
}
};
Part (B). How to read from file
#include <iostream>
#include <fstream>
using namespace std;
/* The data format is not mentioned in the question. And way of reading input will depend on pattern of data
Ex.
1 2 3 4
5 6 7 8
*/
LoadMatAFromFile{
ifstream ifs("InputFileName");
int first, second ,third, fourth;
while(ifs >> first)
{
ifs >> second;
ifs >> third;
ifs >> fourth;
/* Then assign the above values to respective class objects*/
}
}
Similarly when writing to a file
SaveAToFile{
ofstream myfile;
myfile.open ("OutputFileName");
myfile << "Writing this to a file. ";
----------
WHILE MATRIX is NOT EMPTY
READ a value MATRIX object
myfile << value;
-----------
myfile.close();
return 0;
}
Part (3):
Add two matrix
AddMatrix(Matrix A, Matrix B){
int row1= A.noOfrows;
int col1= A.noOfcols;
int row2= B.noOfrows
int col2= B.noOfcols;
-----------------
RUN two for loops & get the sum of matrix
Operations will be done on below :
A.(RowInstance[i]->arr[j]).x;
B.(RowInstance[i]->arr[j]).x;
-----------------
}
Similarly for Multiplication.
The main thing to do is get the length of rows & columns. Then it is the normal way using for loops to compute.
Part (4): Enter A from keyboard
This is the normal functioning of a program.
*Close all the file objects
*Use cin, cout, read, etc. methods
**to read from std input
**to display to std output
Part (5): Display matrixes
fetch noOfRow & noOfCols for matrix & Print the values with two for loops :
-----------
cout << MatrixInstance.(RowInstance[i]->arr[j]).x;
------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.