I have the following code: my add function and M.add(N) statement keeps turning up errors I'm not sure how to fix it. I also need a getVal function but I'm not sure what to do with it.

#include <iostream>
using namespace std;

class sMatrix
{
public:
	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();
	void& add();
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 sMatrix::add()
{
	int i,j;
	for (i=0;i<=n;i++)
	{
		for (j=0;j<=n;j++)
		{
			m[i,j]+=n[i,j];
		}
	}
}

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,1,0);
        N.setEl(1,0,2.3);
        N.setEl(0,0,0);
        M.add(N);

}

//For the rest of the project it needs:
//getVal function
//sMatrix (M(3,3), N(3,3))
//M.print();
//N.print();
//M.add(N);
//for (i=0;i<=n;i++)
//for (j=0;j<=n;j++)
//m[i,j]+=n(i,j)
//M.sub(N);

Recommended Answers

All 5 Replies

look at line 89 m[i,j] I expect your compiler is complaining that this is undefined
you need to define how to set_val in a matrix
I think you are missing a few storage

so

double val = m.get_val(i, j) + n.get_val(i,j);
m.set_val(i, j, val);

is the approach you need but you also need
somewhere to store the values in your matrix

look at line 89 m[i,j] I expect your compiler is complaining that this is undefined
you need to define how to set_val in a matrix
I think you are missing a few storage

so

double val = m.get_val(i, j) + n.get_val(i,j);
m.set_val(i, j, val);

is the approach you need but you also need
somewhere to store the values in your matrix

How would I go about? I've been stuck for a long time. It took me a long time just to figure out what I already posted.

Now there are some unusual things happening in your matrix class
normally a matrix stores the data
as a m by n array or vector of vectors

It is difficult to give exact answers without knowing if SetEl
is doing what you want

Say you wanted a matrix with m columns and n rows
then mathematically you need a value for each of the elements in the matrix

this is normally found using a column and row coord
like you are doing

but the data would be m*n doubles
this can be organised as a single list

so data would be a simple array or vector of values
and if you want column c & row r (starting from 0)
the index for the value you want becomes

index = r + m*c

so an init() method would create your data and then
you woud have a

void init(int number_of_cols, int number_of_rows);
double get(int c, int  r);
void set(int c, int r, double val);
bool valid_coord(int c, int r);
void clear();

These are the basic methods that you need
You have the outline for these methods with your constructor
being init(); it should be more like
nmax = nr*nc;
data = new double[nmax];

But I need to be sure that this is what you are trying to design


How would I go about? I've been stuck for a long time. It took me a long time just to figure out what I already posted.

Now there are some unusual things happening in your matrix class
normally a matrix stores the data
as a m by n array or vector of vectors

It is difficult to give exact answers without knowing if SetEl
is doing what you want

Say you wanted a matrix with m columns and n rows
then mathematically you need a value for each of the elements in the matrix

this is normally found using a column and row coord
like you are doing

but the data would be m*n doubles
this can be organised as a single list

so data would be a simple array or vector of values
and if you want column c & row r (starting from 0)
the index for the value you want becomes

index = r + m*c

so an init() method would create your data and then
you woud have a

void init(int number_of_cols, int number_of_rows);
double get(int c, int  r);
void set(int c, int r, double val);
bool valid_coord(int c, int r);
void clear();

These are the basic methods that you need
You have the outline for these methods with your constructor
being init(); it should be more like
nmax = nr*nc;
data = new double[nmax];

But I need to be sure that this is what you are trying to design

Just about all of my code was provided by my professor. My job is to:
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

This part is what my professor wants to see (I've added things on my own too)

void main ()
{
	sMatrix M(3,3), N(3,3);
	M.setEl(0,0,3);   //(row,col,val)
	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);	// function does not take 1 arguments
}

Thread continued here.

Please stop making new threads for the same problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.