Answered step by step
Verified Expert Solution
Question
1 Approved Answer
#include #include using namesp Homework 7 : , ( ) , . : ( ! ) , 0 - , . : ,
#include
#include
using namesp Homework
:
:
:
FIXME
:
:
:ace std;
struct SparseMatrix
int data;
int offsets;
int numRows;
int numCols;
;
SparseMatrix makeSparseconst int mat, const int numRows, const int numCols
SparseMatrix sm;
smnumRows numRows;
smnumCols numCols;
smdata new intnumRows numCols;
smoffsets new intnumRows;
int dataIndex ;
for int i ; i numRows; i
smoffsetsi dataIndex;
for int j ; j numCols; j
smdatadataIndex mati numCols j;
return sm;
void freeSparseSparseMatrix& sm
delete smdata;
delete smoffsets;
smnumRows ;
smnumCols ;
smdata nullptr;
smoffsets nullptr;
int getconst SparseMatrix& sm const int row, const int col
if row col row smnumRows col smnumCols
return ;
int dataIndex smoffsetsrow col;
return smdatadataIndex;
void testEasyconst int mat, const int numRows, const int numCols
SparseMatrix sm makeSparsemat numRows, numCols;
for int i ; i numRows; i
for int j ; j numCols; j
assertmati numCols j getsm i j;
freeSparsesm;
void testHardconst int mat, const int numRows, const int numCols
SparseMatrix sm makeSparsemat numRows, numCols;
int dataIndex ;
for int i ; i numRows; i
for int j ; j numCols; j
assertmatdataIndex getsm i j;
freeSparsesm;
int main
int numRows, numCols;
cout "Enter number of rows: ;
cin numRows;
cout "Enter number of cols: ;
cin numCols;
int mat new intnumRows numCols;
cout "Enter elements of the matrix rowwise:" endl;
for int i ; i numRows; i
for int j ; j numCols; j
cin mati
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started