0

# I want a working code for compression of sparse matrix.

If a row ends with zeros we have to remove them and if if a row has all elements 0 then keep only first and remove others.

3 Answers

+ 3

Laiba Goher First try yourself and show your attempts if you get any trouble.

0

#include<iostream>
using namespace std;
class CompactMatrix {
private:
int rows, columns;
double** compactMatrix;
public:
friend ostream& operator<<(ostream& out, CompactMatrix& obj);
int elements[200];
CompactMatrix(){
compactMatrix = nullptr;
rows = 0;
columns = 0;
}
CompactMatrix(int** matrixNull, int r, int c){
int x = 0;
for (int i = 0; i < r; i++){
int count = 0;
for (int j = 0; j < c; j++){
if (matrixNull[i][j] != 0){
count++;
}
}elements[x++] = count;
}
rows = r; columns = x;
compactMatrix = new double* [rows];
int a = 0;
for (int i = 0; i < rows; a++, i++){
int s = elements[a] * 2 + 1;
compactMatrix[i] = new double[s];
}
a = 0;
for (int i = 0; i < r; i++){
compactMatrix[i][0] = elements[a++];
}
for (int i = 0; i < r; a++, i++){
for (int j = 0, b = 1; b < elements[a] * 2 + 1; j++){
if (matrixNull[i][j] != 0){
compactMatrix[i][b++] = j;
compactMatrix[i][b++] = matrixNull[i][j];
}
}
}
}
~CompactMatrix()

0

AJ Anant #G3 this is the code i tried but i couldn't make main out of it