I have a 3x3 matrix with values:
(0,0)=3
(1,1)=4
(2,0)=2.4
the rest of the values are zero
and the code:
#include <iostream>
using namespace std;
class sMatrix
{
public:
sMatrix();
sMatrix(int,int);
int getR();
int getC();
bool rValid(int);
bool cValid(int);
void setEl(int,int,double);
double getEl(int i,int j);
void print();
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(int r)
{
if (r<0 || r>=nr)
return false;
return true;
}
bool sMatrix::cValid(int r)
{
if (r<0 || r>=nc)
return false;
return true;
}
void sMatrix::setEl(int r,int 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;
}
}
void main ()
{
sMatrix M(3,3),N(3,3);
M.setEl(0,0,3);
M.setEl(1,1,4);
M.setEl(2,0,2.4);
M.print();
N.setEl(0,0,3);
N.setEl(1,1,4);
N.setEl(2,0,2.4);
N.print();
M.add(N); //I know this needs to be in the code someplace
M.print();
}
This part is given by the professor and I have to work with it:
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;
}
Needs:
1) a getVal function that retrieves the value of the element row i and column j. (The function mst check both i and j values are in range)
2)Provide a putVal function that puts a value v in rown i and column j. This operation must make sure that i and j values do not exists in the storage.
3) a funtion to print the matrix in full form or in triple form.
4) should do addition, subtraction, and multiplication
I've been playing with the code to get the things I need but nothing is working.