If I where to have a matrix of say:
123
456
789

How would I write a getVal() functions so that row[0]col[0]=1 and so for?

Recommended Answers

All 4 Replies

Are you talking a matrix Class or just making a matrix out of any array in main and using c style functions?

Are you talking a matrix Class or just making a matrix out of any array in main and using c style functions?

A matrix class.

A matrix class.

In that case you could do something like.

class Matrix {
public:  
        Matrix ();
        ~Matrix();
        int getVal (int row, int col)const;
   .........
private:
int ArrayName[what ever the row size][whatever the col size];
};

int Matrix::getVal (int row, int col)const {
       return ArrayName[row][col];
}

In that case you could do something like.

class Matrix {
public:  
        Matrix ();
        ~Matrix();
        int getVal (int row, int col)const;
   .........
private:
int ArrayName[what ever the row size][whatever the col size];
};

int Matrix::getVal (int row, int col)const {
       return ArrayName[row][col];
}

I did something similar:

#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);
	int getVal(int,int)const;
	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;
	}
}


int sMatrix::getVal(int row,int col)const
{
	return nmax[row][col];
}


void sMatrix::add()
{
	int i,j;
	for (i=0;i<=nr;i++)
	{	
		for (j=0;j<=nc;j++)
		{
			m(i,j)+=n(i,j);	//m and n undeclared identifier
		}
	}
}

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);	// function does not take 1 arguments
	

}

//For the rest of the project it 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

As you can see theres a few mistakes, part of the code was given in class and I've been trying to make it do what has to be done.

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.