#include <iostream>
using namespace std;
class sMatrix
{
public:
sMatrix(int,int);
sMatrix (sMatrix&);
int getR();
int getC();
bool rValid(double);
bool cValid(double);
void setEl(double,double,double);
double getEl(int i,int j);
int getVal();
void print();
void add(sMatrix&);
private:
int nr, nc; //number of row and cols
int nent; //number of entries
int nmax; //the max number of entries
double *data; //store the data, the size of the storage is nmax*3
};
sMatrix::sMatrix(int rr,int cc)
{
nr=rr;
nc=cc;
nent=0;
nmax=(nr*nc)/2;
data=new double [3*nmax];
for (int i=0; i<3*nmax; i++)
data [i]=0.0;
}
int sMatrix::getR()
{
return nr;
}
int sMatrix::getC()
{
return nc;
}
bool sMatrix::rValid(double r)
{
if (r<0 || r>=nr)
return false;
return true;
}
bool sMatrix::cValid(double r)
{
if (r<0 || r>=nc)
return false;
return true;
}
void sMatrix::setEl(double r,double c, double val)
{
int i;
if (rValid(r) && cValid(c))
{
i=3*nent;
data[i]=r;
data[i+1]=c;
data[i+2]=val;
nent++;
}
}
void sMatrix::print()
{
int i;
for (i=0;i<3*nent;i+=3)
{
cout<<"<"<<data[i]<<",";
cout<<""<<data[i+1]<<",";
cout<<""<<data[i+2]<<">"<<endl;
}
}
int sMatrix::getVal()
{
int i,j;
for (j=0;nent--;j++)
{
i=j*3;
if (nr==data[i] && nc==data[i+1])
{
return data[i+2];
}
}
return 0;
}
void sMatrix::add(sMatrix &N)
{
int i,j;
for (i=0;i<=nr;i++)
{
for (j=0;j<=nc;j++)
{
double Val1=getVal();
double Val2=N.getVal();
setEl(i,j,Val1+Val2);
}
}
}
void main ()
{
sMatrix M(3,3);
M.setEl(0,0,3);
M.setEl(1,1,4);
M.setEl(2,0,2.4);
M.print();
sMatrix N(3,3);
N.setEl(0,0,0);
N.setEl(0,0,0);
N.setEl(0,0,0);
N.print();
M.add(N); //prints everything before this point
M.print();
}
I was told theres an issue with my getVal function. I doesn't take in arguments? I'm at a complete loss. the complier prints to a certain point and then crashes.