Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Goal: Implement matrix compression in the way we last discussed in the multidimensional arrays session ( with offset for each row ) , as well
Goal: Implement matrix compression in the way we last discussed in the multidimensional arrays session with offset for each row as well as a function to retrieve an element from the compressed content. in C
Briefly:
All nonzero elements and only them! from the matrix are placed in a linear array, with an "offset" calculated for the elements of each row.
The offset for the first row is always
The offset for each subsequent row is the smallest possible such that no element of this row coincides with any element of the previous rows.
The offset for each subsequent row is AT LEAST as much as that of the previous row.
A few examples:
offset
offset
offset
offset
offset
offset
offset
offset
For this, use the following implementation skeleton, and put whatever code you deem necessary in each place marked with FIXME. Modifying the existing part of the code is prohibited.
Contains everything needed for fullfledged work of a compressed matrix WITHOUT keeping a copy of the original.
struct SparseMatrix
FIXME
;
Allocates memory for the compressed representation any necessary auxiliary information,
performs the compression, and returns a structure containing all the data.
IMPORTANT: we cannot precompute how much memory will be needed for compression
for the purposes of this assignment, you can allocate an array of size numRowsnumCols, and populate it
SparseMatrix makeSparseconst int mat, const int numRows, const int numCols
FIXME
Returns the element at the given position in the original matrix.
int getconst SparseMatrix& sm const int row, const int col
FIXME: check if row and col are valid indices
return smdatasmoffsetsrow col;
Frees the memory allocated in makeSparse and stored in the member data of sm
void freeSparseSparseMatrix& sm
FIXME
Tip: calculating the offsets and populating the linear array don't necessarily have to be separate steps :
You can use the following two functions to test your solutions:
#include
int testEasyconst int mat, const int numRows, const int numCols
SparseMatrix sm makeSparsemat numRows, numCols;
for int i ; i numRows; i
for int j ; i numCols; j
assertmatij matij getsm i j;
freeSparsesm;
int testHardconst int mat, const int numRows, const int numCols
SparseMatrix sm makeSparsemat numRows, numCols;
for int i ; i numRows; i
for int j ; i numCols; j
Problem: if matij in the compressed representation this hole might
have been filled by an element from a different row :
How can we distinguish them with as little additional information as possible, stored in the SparseMatrix object?
assertmatij getsm i j;
freeSparsesm;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started