Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include #include using namesp Homework 7 : , ( ) , . : ( ! ) , 0 - , . : ,

#include
#include
using namesp Homework 7
: ,
(),
.
:
(!),
""
0
-,
.
: ,
, FIXME .
.
:
:
:ace std;
struct SparseMatrix {
int* data;
int* offsets;
int numRows;
int numCols;
};
SparseMatrix makeSparse(const int* mat, const int numRows, const int numCols){
SparseMatrix sm;
sm.numRows = numRows;
sm.numCols = numCols;
sm.data = new int[numRows * numCols];
sm.offsets = new int[numRows];
int dataIndex =0;
for (int i =0; i < numRows; ++i){
sm.offsets[i]= dataIndex;
for (int j =0; j < numCols; ++j){
sm.data[dataIndex++]= mat[i * numCols + j];
}
}
return sm;
}
void freeSparse(SparseMatrix& sm){
delete[] sm.data;
delete[] sm.offsets;
sm.numRows =0;
sm.numCols =0;
sm.data = nullptr;
sm.offsets = nullptr;
}
int get(const SparseMatrix& sm, const int row, const int col){
if (row <0|| col <0|| row >= sm.numRows || col >= sm.numCols){
return 0;
}
int dataIndex = sm.offsets[row]+ col;
return sm.data[dataIndex];
}
void testEasy(const int* mat, const int numRows, const int numCols){
SparseMatrix sm = makeSparse(mat, numRows, numCols);
for (int i =0; i < numRows; ++i){
for (int j =0; j < numCols; ++j){
assert(mat[i * numCols + j]== get(sm, i, j));
}
}
freeSparse(sm);
}
void testHard(const int* mat, const int numRows, const int numCols){
SparseMatrix sm = makeSparse(mat, numRows, numCols);
int dataIndex =0;
for (int i =0; i < numRows; ++i){
for (int j =0; j < numCols; ++j){
assert(mat[dataIndex++]== get(sm, i, j));
}
}
freeSparse(sm);
}
int main(){
int numRows, numCols;
cout << "Enter number of rows: ";
cin >> numRows;
cout << "Enter number of cols: ";
cin >> numCols;
int* mat = new int[numRows * numCols];
cout << "Enter elements of the matrix row-wise:" << endl;
for (int i =0; i < numRows; ++i){
for (int j =0; j < numCols; ++j){
cin >> mat[i

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Main Memory Database Systems

Authors: Frans Faerber, Alfons Kemper, Per-Åke Alfons

1st Edition

1680833243, 978-1680833249

More Books

Students also viewed these Databases questions

Question

Identify the cause of a performance problem. page 363

Answered: 1 week ago