Click here to Skip to main content
15,896,111 members

Comments by carlmack (Top 23 by date)

carlmack 6-Dec-11 2:41am View    
the code submitted was taken from a work sheet exercise, now naturally i would like to understand fully what am doing when a code has errors where to look and what to look for when those errors a given after compiling, so i posted that example for guidance as to how to approach a solution in removing the errors, so far the responses have been good, thank u much
carlmack 5-Dec-11 5:58am View    
// Declarations
class Matrix;
double Det(const Matrix& a);
Matrix Diag(const int n);
Matrix Diag(const Matrix& v);
Matrix Inv(const Matrix& a);
Matrix Ones(const int rows, const int cols);
int Size(const Matrix& a, const int i);
Matrix Zeros(const int rows, const int cols);




/*
* a simple exception class
* you can create an exeption by entering
* throw Exception("...Error description...");
* and get the error message from the data msg for displaying:
* err.msg
*/
class Exception
{
public:
const char* msg;
Exception(const char* arg)
: msg(arg)
{
}
};






class Matrix
{
public:
// constructor
Matrix()
{
//printf("Executing constructor Matrix() ...\n");
// create a Matrix object without content
p = NULL;
rows = 0;
cols = 0;
}


// constructor
Matrix(const int row_count, const int column_count)
{
// create a Matrix object with given number of rows and columns
p = NULL;


if (row_count > 0 && column_count > 0)
{
rows = row_count;
cols = column_count;


p = new double*[rows];
for (int r = 0; r < rows; r++)
{
p[r] = new double[cols];


// initially fill in zeros for all values in the matrix;
for (int c = 0; c < cols; c++)
{
p[r][c] = 0;
}
}
}
}


// assignment operator
Matrix(const Matrix& a)
{
rows = a.rows;
cols = a.cols;
p = new double*[a.rows];
for (int r = 0; r < a.rows; r++)
{
p[r] = new double[a.cols];


// copy the values from the matrix a
for (int c = 0; c < a.cols; c++)
{
p[r][c] = a.p[r][c];
}
}
}


// index operator. You can use this class like myMatrix(col, row)
// the indexes are one-based, not zero based.
double& operator()(const int r, const int c)
{
if (p != NULL && r > 0 && r <= rows && c > 0 && c <= cols)
{
return p[r-1][c-1];
}
else
{
throw Exception("Subscript out of range");
}
}


// index operator. You can use this class like myMatrix.get(col, row)
// the indexes are one-based, not zero based.
// use this function get if you want to read from a const Matrix
double get(const int r, const int c) const
{
if (p != NULL && r > 0 && r <= rows && c > 0 && c <= cols)
{
return p[r-1][c-1];
}
else
{
throw Exception("Subscript out of range");
}
}


// assignment operator
Matrix& operator= (const Matrix& a)
{
rows = a.rows;
cols = a.cols;
p = new double*[a.rows];
for (int r = 0; r < a.rows; r++)
{
p[r] = new double[a.cols];


// copy the values from the matrix a
for (int c = 0; c < a.cols; c++)
{
p[r][c] = a.p[r][c];
}
}
return *this;
}


// add a double value (elements wise)
Matrix& Add(const double v)
{
for (int r = 0; r < rows; r++)
{
for (int c = 0; c < cols; c++)
{
p[r][c] += v;
}
}
return *this;
}


// subtract a double value (elements wise)
Matrix& Subtract(const double v)
{
return Add(-v);
}
carlmack 4-Dec-11 3:40am View    
I am very new to this, topic, fairly and i wand to be finish. so each day i spen here i learn quite a lot. Thank you
carlmack 4-Dec-11 3:36am View    
Can you tell me whats the reason for all these warnings?
carlmack 4-Dec-11 3:35am View    
Ok Original, just a little bit out of it, just trying to get this working properly, and am not being rude , just tired have not slept 4 days almost. I will try to be more alert, dont know by how much