Sunday, July 12, 2009

C++ Program Help! (Using classes)?

I already have the class:


class Matrix{


public:


int getrow() {return rows;};


int getcol() {return cols;};


char getname() {return name;};





Matrix();


~Matrix();





// void Add(const Matrix %26amp;A, const Matrix %26amp;B);





private:


int rows;


int cols;


char name;


double *valuePtr;


void setrow(int r){rows = (r%26gt;=0)? r : 0; };


void setcol(int c){cols = (c%26gt;=0)? c : 0; };





};





//constructor: allocates memory for variables accordingly//


Matrix::Matrix(){


rows = 0;


cols = 0;


name = 'A';


*valuePtr = NULL;


setrow(rows);


setcol(cols);


}





// Input: data is a double matrix of size rows * columns //


Matrix::Matrix(int r, int c, char n, double *data){


setrow(r);


setcol(c);


name = n;


valuePtr = new double[rows*cols];


for (int i=0; i%26lt;rows*cols; i++){


valuePtr[i] = data








Code for the read function? How to run through the class accoridngly???

C++ Program Help! (Using classes)?
"double *valuePtr" is the wrong variable to use for a matrix. A matrix in your case is a dynamic 2-D array. Here is how you declare and initialize one:





double** valuePtr = new double*[rows];





for(int i = 0; i %26lt; rows; i++)


valuePtr[i] = new double[cols];





// Now, "double *data" in your matrix constructor also needs


// to be changed to "double** data". Then, here is how you


// copy values from data into the matrix:





for(int i =0; i %26lt; rows; i++)


for(int j =0; j %26lt; cols; j++)


valuePtr[i][j] = data[i][j];





// In your first constructor, change " *valuePtr = NULL;" to


// "valuePtr = NULL;"





// If you need more help let me know


No comments:

Post a Comment