Hey all! I'm new here. I actually joined for physics help a couple of days ago, but I found the answer by searching through old threads and didn't have to post a new one. So here's my first post.(adsbygoogle = window.adsbygoogle || []).push({});

Anyway, so I procrastinated on my homework, and now I'm having trouble with it and no longer have time to ask my professor for help before it's due. I'm only in Intro to Comp. Sci 2, so hopefully there are plenty of people who can help me here!

I'm supposed to implement a matrix class. The professor has given us the class declaration and driver. Mine is compiling, but it's freezing when it executes. On the site where we are supposed to upload and test our code, this is the error message that is being displayed: "bash: line 1: 8948 Segmentation fault ./prog2.exe"

Ok, now here's the code. The professor has provided this driver:

Here is my header file, Matrix.h:Code (Text):#include <iostream>

#include <iomanip>

#include "Matrix.h"

using namespace std;

ostream& printMatrix(Matrix m, ostream& out);

void TestFunction1(Matrix, Matrix, Matrix);

int main(void)

{

//check to see if the static variable matrixCount has been created and init

cout << "matrixCount = " << Matrix::get_matrixCount() << endl;

cout << "(matrixCount should be zero)\n\n";

//create 3 matrices, check matrixCount, .and print matrix dimension

Matrix A(2,3),B(2,3),C(3,3);

A = B;

cout << "Matrix A\n";

cout << "\t Rows = " << A.getRows() << endl;

cout << "\t Cols = " << A.getColumns() << endl;

cout << "Matrix B\n";

cout << "\t Rows = " << B.getRows() << endl;

cout << "\t Cols = " << B.getColumns() << endl;

cout << "Matrix C\n";

cout << "\t Rows = " << C.getRows() << endl;

cout << "\t Cols = " << C.getColumns() << endl;

cout << endl;

cout << "matrixCount = " << Matrix::get_matrixCount() << endl;

cout << "(matrixCount should be three (3))\n\n";

//Test the copy constructor and destructor

cout << endl;

cout << "matrixCount = " << Matrix::get_matrixCount() << endl;

cout << "(before call the TestFunction1)\n\n";

TestFunction1(A,B,C);

cout << endl;

cout << "matrixCount = " << Matrix::get_matrixCount() << endl;

cout << "(after call the TestFunction1)\n\n";

//create some data arrays

int row, col, index;

int data1[]={1,2,3,4,5,6};

int data2[]={10,20,30,40,50,60};

int data3[]={-5,-10,-15,0,5,10,15,36,42};

//Fill Matricies A, B will initial values

//Test setElement

index = 0;

for(row = 0; row < 2; row++)

for(col = 0 ; col < 3; col++)

{

A.setElement(row, col, data1[index]);

B.setElement(row,col,data2[index]);

index++;

}

//Fill Matricies C will initial values

//Test overloaded []

index = 0;

for(row = 0; row < 3; row++)

for(col = 0 ; col < 3; col++)

{

//C.setElement(row, col, data3[index++]);

C[row][col] = data3[index++];

}

//Print Matricies A, B, C

cout<<"Matrix A\n\n";

printMatrix(A,cout);

cout << endl << endl;

cout<<"Matrix B\n\n";

printMatrix(B,cout);

cout << endl << endl;

cout<<"Matrix C\n\n";

printMatrix(C,cout);

cout << endl << endl;

//Addition

cout<<"Matrix A + B\n\n";

printMatrix(A.add(B),cout);

cout << endl << endl;

//Test Subtraction

cout<<"Matrix A - B\n\n";

printMatrix(A.subtract(B),cout);

cout << endl << endl;

//Test Multiplication

cout<<"Matrix A * C\n\n";

printMatrix(A.multiply(C),cout);

cout << endl << endl;

return 0;

}

//Function to print a Matrix

ostream& printMatrix(Matrix m, ostream& out)

{

int row, col;

int r,c;

row = m.getRows();

col = m.getColumns();

for(r = 0; r < row; r++)

{

for(c = 0 ; c < col; c++)

out <<setw(4)<< m.getElement(r, c)<<" ";

out<<endl;

}

return out;

}

//Test Function 1, test copy constructor

void TestFunction1(Matrix x, Matrix y, Matrix z)

{

cout << endl;

cout << "matrixCount = " << Matrix::get_matrixCount() << endl;

cout << "(In TestFunction1)\n\n";

return;

}

And here are the functions for the class:Code (Text):#ifndef _MATRIX_

#define _MATRIX_

class Matrix

{

public:

Matrix(int r=0, int c = 0);

Matrix(const Matrix& m);

~Matrix();

Matrix operator= (Matrix);

int* operator[] (int);

int getRows() const;

int getColumns() const;

int getElement(int r, int c) const;

void setElement (int r, int c, int v);

Matrix add (const Matrix& m) const;

Matrix subtract (const Matrix& m) const;

Matrix multiply (const Matrix& m) const;

static int get_matrixCount ();

private:

int rows, cols;

int **element;

static int matrixCount;

};

#endif //_MATRIX_

Can anyone figure out what's happening? I'm feeling kind of stupid right now, because I've been trying to figure it out forever. Oh, and if you see anything else I might need to fix, can you tell me that as well? Thanks!Code (Text):#include "Matrix.h"

int Matrix::matrixCount = 0;

Matrix::Matrix(int r, int c)

{

if (r > 0 && c > 0)

{

rows = r;

cols = c;

}

else

{

rows = 0;

cols = 0;

}

element = new int* [rows];

for (int i=0; i<rows; i++)

{

element[i] = new int[cols];

for (int n = 0; n<cols; n++)

{

element[i][n] = 0;

}

}

matrixCount++;

}

Matrix::Matrix(const Matrix& m)

{

for (int i=0; i<rows; i++)

{

delete []element[i];

}

delete []element;

rows = m.rows;

cols = m.cols;

element = new int* [rows];

for (int i=0; i<rows; i++)

{

element[i] = new int[cols];

for (int n = 0; n<cols; n++)

{

element[i][n] = m.element[i][n];

}

}

}

Matrix::~Matrix()

{

for (int i=0; i<rows; i++)

{

delete []element[i];

}

delete []element;

}

Matrix Matrix::operator=(Matrix m)

{

for (int i=0; i<rows; i++)

{

delete []element[i];

}

delete []element;

rows = m.rows;

cols = m.cols;

element = new int* [rows];

for (int i=0; i<rows; i++)

{

element[i] = new int[cols];

for (int n = 0; n<cols; n++)

{

element[i][n] = m.element[i][n];

}

}

return *this;

}

int* Matrix::operator[] (int n)

{

return element[n];

}

int Matrix::getRows() const

{

return rows;

}

int Matrix::getColumns() const

{

return cols;

}

int Matrix::getElement(int r, int c) const

{

return element[r][c];

}

void Matrix::setElement (int r, int c, int v)

{

if (r>=0 && r<=rows && c>=0 && c<=cols)

element[r][c] = v;

}

Matrix Matrix::add (const Matrix& m) const

{

for (int i=0; i<rows; i++)

{

for (int n = 0; n<cols; n++)

{

element[i][n] += m.element[i][n];

}

}

return *this;

}

Matrix Matrix::subtract (const Matrix& m) const

{

for (int i=0; i<rows; i++)

{

for (int n = 0; n<cols; n++)

{

element[i][n] -= m.element[i][n];

}

}

return *this;

}

Matrix Matrix::multiply (const Matrix& m) const

{

Matrix product;

product.rows = rows;

product.cols = m.cols;

for (int i=0; i<product.rows; i++)

{

for (int n = 0; n<product.cols; n++)

{

for (int m=0; m<cols; m++)

{

product.element[i][n] += element[m][m];

}

}

}

return product;

}

int Matrix::get_matrixCount()

{

return matrixCount;

}

**Physics Forums | Science Articles, Homework Help, Discussion**

Dismiss Notice

Join Physics Forums Today!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# C++ Homework

**Physics Forums | Science Articles, Homework Help, Discussion**