Hey all,
So i created a program that performs matrix addition and subtraction.
It compiles but when I try to run it, it shows "SEGMENTATION FAULT"

Help Please!

Here are my codes:

Matrix.h

#ifndef MATRIX_H
#define MATRIX_H

class Matrix
{
private:
  const int numRows, numCols;
  int **data;

public:
  
  //constructors and destructors
  Matrix(int m, int n); //parametric constructor, creates matrix of size mxn and reads values from stdin
  Matrix(const Matrix &M); //copy constructor
  ~Matrix(); //destructor
  
  //accessors/mutators
  int getValue(int i, int j) const; //returns data at (i, j)
  void setValue(int i, int j, int value); //sets data at (i, j) to value
  
  int getNumRows() const; //returns number of rows
  int getNumCols() const; //returns number of columns
  //setNumRows and setNumCols intentionally not implemented
  
  //IO
  void display() const; //displays data
  void read(); //reads in matrix values from stdin (possibly redirected from file)
  
  //Arithmetic
  void operator += (const Matrix &M); //performs matrix addition between calling object and argument M, stores result in calling object
  void operator -= (const Matrix &M); //performs matrix subtraction between calling object and argument M, stores result in calling object
  
};

#endif //MATRIX_H

Matrix.cpp

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include "Matrix.h"

using namespace std;

/* 
 * Constructors and Destructors
 */

Matrix::Matrix(int m, int n):numRows(m), numCols(n){  //parametric constructor, creates matrix of size mxn and reads values from stdin

	data= new int* [numRows];	

	for (int i =0; i < numRows; ++i){
		data[i] = new int [numCols];
	}

	for (int i = 0; i < numRows; ++i){
		for(int j =0; j < numCols; ++i){
			data[i][j]=0;
		}
	}
} 

Matrix::Matrix(const Matrix &M): numRows(M.numRows), numCols(M.numCols){ //copy constructor

	data = new int* [numRows];
	
	for(int i = 0; i < numRows; ++i){
		data[i] = new int [numCols];
	}
	for(int i = 0; i < numRows; ++i){
		for (int j =0; j < numCols; ++j){
			data[i][j] = M.data[i][j];
		}
	}
} 

Matrix::~Matrix(){ //destructor

	for(int i = 0; i < numRows; ++i){
		delete [] data[i];
	}
	delete [] data;
} 
  
/*
 * Accessors/mutators
 */

int Matrix::getValue(int i, int j) const{ //returns data at (i, j)

	

	if((i >= 0) && (i < numRows) && (j >=0) && (j < numCols)){
		return data[i][j];
	}
	else {
		cout << "ERROR!! Out of bounds." << endl;
		exit(1);
	}
} 

void Matrix::setValue(int i, int j, int value){ //sets data at (i, j) to value

	
	if (i > -1 && i < numRows){
		if(j > -1 && j < numCols){
			data[i][j] = value;
		}
	}	
	else {
		cout << "ERROR. Out of bounds. " << endl;
		exit(1);
	}
} 
  
int Matrix::getNumRows() const{//returns number of rows

	return(numRows);

} 
 
int Matrix::getNumCols() const{

	return(numCols);

} //returns number of columns
  //setNumRows and setNumCols intentionally not implemented
  //IO

void Matrix::display() const{ //displays data

	for(int i = 0; i < numRows; ++i){
		for(int j = 0; j < numCols; ++j){
			cout << data[i][j];
		}
		cout << endl;
	}
} 

void Matrix::read(){ //reads in matrix values from stdin (possibly redirected from fi

	for(int i = 0; i < numRows; ++i){
		for(int j = 0; j < numCols; ++j){
      			cin >> data[i][j];
    		}
	}
} 

/*
 * Arithmetic
 */

void Matrix::operator += (const Matrix &M){ //performs matrix addition between calling object and argument M, stores result in calling object
	
	if ((numRows == M.numRows) && (numCols == M.numCols)){

		for(int i=0; i < numRows; ++i){
			for(int j = 0; j< numCols; ++j){
				data[i][j] += M.data[i][j];
				cout << data[i][j];
			}
		}
	}
	else {
		cout << "ERROR!!! Unable to perform matrix addition." << cout;
		exit(1);
	}
} 

void Matrix::operator -= (const Matrix &M){ //performs matrix subtraction between calling object and argument M, stores result in calling objec

	
	if ((numRows == M.numRows) && (numCols == M.numCols)){

		for(int i=0; i < numRows; ++i){
			for(int j = 0; j< numCols; ++j){
				data[i][j] -= M.data[i][j];
				cout << data[i][j];
			}
		}
	}
	else {
		cout << "ERROR!!! Unable to perform matrix addition." << cout;
		exit(1);
	}
}

Main.cpp

#include <iostream>
#include "Matrix.h"

using namespace std;

/*
 * Main Function
 */
int main(int argc, char **argv)
{
  
  //Create two Matrix obejcts
  int m,n;
  cout << "Enter number of rows:" << endl;
  cin >> m;
  cout << "Enter number of columns:" << endl;
  cin >> n;
  
  Matrix A(m,n); //using parametric constructor to create object and read values from console
  Matrix B (A); //using copy constructor to copy from A
  
  
  
  /*
   * Test +=
   */
  cout << "-----------------------" << endl;
  cout << "Testing the += operator" << endl;
  cout << "-----------------------" << endl;
  
  //Display the two matrices
  cout << "A:" << endl;
  A.display();
  cout << "B:" << endl;
  B.display();
  
  //Perform Matrix addition
  A += B;
  
  //Display result of A+=B
  cout << "A+=B:" << endl;
  A.display();
  
  
  
  /*
   * Test -=
   */
  cout << "-----------------------" << endl;
  cout << "Testing the -= operator" << endl;
  cout << "-----------------------" << endl;
  
  //Display the two matrices
  cout << "A:" << endl;
  A.display();
  cout << "B:" << endl;
  B.display();
  
  //Perform Matrix subtraction
  B -= A;
  
  //Display result of B-=A
  cout << "B-=A:" << endl;
  B.display();
    
  //clean up and exit
  cout << endl;
  return(0);
  
}//end Main

Recommended Answers

All 2 Replies

Run the code under a debugger, then tell us on which line it segfault.
If you run windows, you can use Visual Studio or whichever IDE you use.

You can't really expect people to invest time into reading your code (or copy/pasting + compiling themselves), when you can easily pinpoint where the error is yourself.

I figured it out. Thanks.

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.