In my cs2 class the assignment is to implement a sparse matrix with pointers without using a 2d array. Here is the exact text:
A sparse matrix can be represented using an array of pointers called "arrayOfPointer" and an array of integer called "matrixInfo". The size of "arrayOfPointer" will be equal to the number of rows in the matrix X. Each pointer arrayOfPointer will point to an array-of-objects, where each object stores the column index, and the value X[columnindex] for an element different from zero of the matrix X. The size of each array-of-objects will be equal to the number of elements different from zero of the corresponding row in the matrix X. We will store in "matrixInfo" the number of rows in the matrix, and for each row the size of the corresponding array-of-objects.
Here is the code I have done so far:
#include<iostream>
#include<cstdlib>
using namespace std;
//Class implimentations
class ObjectClass
{
private:
int collumnNumber;
double value;
public:
int getColumnNumber() const;
double getValue() const;
void setColumnNumber();
void setValue();
};
//******************************************************************************
class SparseMatrix
{
private:
int matrixInfo[11];
ObjectClass ** arrayOfPointer;
public:
//Main constructor
SparseMatrix (matrixInfo)
{//compiler says error on this line.
// says "invalid member function decaration".
for(int n=0; n<=matrixInfo[0] ; n++)
{
if( **arrayOfPointer[n]!=0)
{
arrayOfPointer= new objectClass*[matrixInfo[n]];
arrayOfPointer.columnNumber[n]=(rand()%10);
}
if(arrayOfPointer.collumnNumber! =0)
{
*arrayOfPointer=new objectClass []
arrayOfPointer.value[n]=(rand()%1000);
//If the row is to have values different from zero
//constructor gives a random number for which collumn
//The non zero value will be in and then places a random
//number into that slot.
}
}
}
//*****OBJECT'S MEMBER FUNCTIONS PROTOTYPES*****
void print() const;
int getMatrixInfo() const;
void setMatrixInfo();
};
Here is the main supplied to me in class:
int main()
{
int tab[11];
tab[0]=10;
for(int i=1; i<11; i++)
{
tab[i]=1+rand()%5;
}
// The constructor uses one array of integers as parameter and will fill the
// matrix with randomly selected values. The parameter contains the num
// of rows, and for each row the number of elements different from zero
// in the matrix X.
//SparseMatrix s(tab);
cout << "The matrix s: " << endl;
// s.print();
cout << endl << endl;
/*
int tab1[11];
tab1[0]=10;
for(int i=1; i<11; i++)
{
tab1[i]=1+rand()%5;
}
SparseMatrix s1(tab1);
s1.print;
cout<< endl << endl;
SparseMatrix s2=s+s1;
s2.print();
cout<< endl << endl;
*/
return 0;
}
I omitted the member function implementation for the classes because currently they are empty and don't do anything and I also temporary disabled most of the main function for now.
I have been getting an error as marked in the code for the SparseMatrix class and cannot seem to understand what is wrong there, also I need to know if my SparseMatrix class is creating a sparse array according to the directions, and if not what I should do to fix it.
Thank you for your help.