I'm trying to implement sparse matrix addition(code attached), as c++ doesn't like 2d dynamic arrays I am using three different array each to represent RowIndex, ColumnIndex, and the corresponding values.
say i added A and B to get C.
C = A.add(B)
in the add member function, I am returning the address to the newly created C matrix.
Everything works fine before returning C, as A->C has expected values inside the arrays, but once I store the C in another identifier in the main function and then print the same array, via new object I find garbage in some of the arrays.
what i've tried :
- At first I was creating the object inside the Add function and then returning the object, tried it with/without the new keyword.
- I thought it might be a problem with the scope so, Now I am using an attribute of matrix A to instantiate the new C matrix, to store the newly-created matrix and then returning the address to it.
Debugging :
- The arrays are located at the same address before and after the .add() function's use still different values are printed.
class sparse{
public:
int rows , cols, len ;
int arr;
int *rindex, *cindex, *vals;
sparse *c;
sparse(){};
sparse( int r, int c, int nzs , int *rn , int *cn, int *values){
this->rows = r;
this->cols = c;
this->rindex = rn;
this->cindex = cn;
this->vals = values;
this->len = nzs;
}
sparse* add(sparse b){
int ap=0 , bp =0, cp =0; // pointers that help with merging in A,B,C matrices
int crindex[this->len+ b.len],
ccindex[this->len+ b.len],
cvals[this->len+ b.len];
int crows = this->rows, ccols = this->cols;
int cnzs = cp; //non-zero values in C
int crf[cnzs], ccf[cnzs], cvf[cnzs];
//merge sort approach to add two sparse matrices
for (int i=0; i< cp ; i++){ // to redcuce size individual arrays
crf[i] = crindex[i];
ccf[i] = ccindex[i];
cvf[i] = cvals[i];
}
this->c = new sparse(crows, ccols, cnzs, crf, ccf, cvf );
this->c->print();
// debugging statements
printf("\narray before recieving: ");
for (int i =0 ; i<this->c->len; i++ ){
printf( "%d ", this->c->rindex[i] );
}
printf("\n address :%d \n", this->c->rindex);
return c;
}
void print( ){
printf( "\nRow | column | value");
for (int i =0 ; i<this->len; i++ ){
printf( "\n %d \t %d \t %d", this->rindex[i], this->cindex[i], this->vals[i] );
}
}
};
int main(){
int ars= 20, acs= 15, anzs= 5, bnzs = 5 ;
// .
int ar [anzs] = { 0,0,0,4,7};
int ac [anzs] = { 0,1,7,1,0};
int av [anzs] = { 11,11,11,11,11,};
// .
int br [5] = { 0,1,3,7, 7};
int bc [5] = { 0,0,5,9,12 };
int bv [5] = { 22,22,22,22,22};
sparse a(ars, acs, anzs, ar, ac, av );
sparse b(ars, acs,bnzs , br, bc, bv );
sparse* c = a.add(b);
c->print();
printf("\narray after recieving: ");
for (int i =0 ; i<c->len; i++ ){
printf( "%d ",c->rindex[i] );
}
printf("\naddress : %d", c->rindex);
return 0;
}
Here's the output:
Row | column | value
0 0 33
0 1 11
0 7 11
1 0 22
3 5 22
4 1 11
7 0 11
7 9 22
7 12 22
array before recieving: 0 0 0 1 3 4 7 7 7
address :6421672
Row | column | value
-433312354 0 33
1 1 11
1996637728 7 11
0 0 22
1 5 22
12 1 11
0 0 11
6421672 9 22
7 12 22
array after recieving: -433312354 1 1996637728 0 1 13 0 6421672 7
address : 6421672