Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

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 &A, const Matrix &B);

private:

int rows;

int cols;

char name;

double *valuePtr;

void setrow(int r){rows = (r>=0)? r : 0; };

void setcol(int c){cols = (c>=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<rows*cols; i++){

valuePtr[i] = data

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

1 Answer

Relevance
  • 1 decade ago
    Favorite Answer

    "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 < 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 < rows; i++)

    for(int j =0; j < 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

Still have questions? Get your answers by asking now.