I am working with a Matrix class to deal with some overloaded operators. I am ha
ID: 3537348 • Letter: I
Question
I am working with a Matrix class to deal with some overloaded operators. I am having a problem of when i run the program I get the error
terminate called after throwing an instance of 'std::bad_alloc'
what(): St9bad_alloc
15112016777216Abort (core dumped)
I believe that to be caused from running off one of the arrays, but i'm not sure and can not find where.
Could really use some help solving this.
The code is as follows.
the input file is
in.txt
3 4
1 2 3 4
0 1 2 3
0 0 1 2
3 4
1 2 5 6
2 1 7 8
0 0 3 2
3 4
3 1 4 1
7 5 8 2
9 4 6 1
4 2
1 2
3 4
5 6
7 8
3 2
4 7
2 5
1 7
Matrix.h file
#ifndef MATRIX_H_
#define MATRIX_H_
#include <iosfwd>
using namespace std;
class Matrix
{
public:
Matrix();
Matrix(int rows, int columns);
Matrix(const Matrix& m); //copy constructor
virtual ~Matrix();
const Matrix& operator=(const Matrix & rightObject);
friend Matrix operator+ (const Matrix& a,
const Matrix& b);
friend Matrix operator- (const Matrix& a,
const Matrix& b);
friend Matrix operator* (const Matrix& a,
const Matrix& b);
friend bool operator== (const Matrix& a,
const Matrix& b);
friend bool operator!= (const Matrix& a,
const Matrix& b);
friend std::istream& operator>> (std::istream& is, Matrix& a);
friend std::ostream& operator<< (std::ostream& os, const Matrix& a);
int getRows() const;
int getColumns() const;
void swap(Matrix& other);
private:
int rowSize;
int columnSize;
double** matrix;
void initZero();
void initCopy(const Matrix& m);
};
#endif
Matrix.cpp file
#include <iostream>
#include <iomanip>
#include <stdexcept>
#include <algorithm>
#include "Matrix.h"
using namespace std;
Matrix::Matrix():rowSize(0),columnSize(0),matrix(NULL){}
Matrix::Matrix(int rows, int columns) : rowSize(rows),columnSize(columns){initZero();}
//copy constructor
Matrix::Matrix(const Matrix& m): rowSize(m.rowSize),columnSize(m.columnSize){initCopy(m);}
Matrix::~Matrix()
{
for (int i = 0; i < rowSize; ++i)
{
delete[] matrix[i];
}
delete matrix;
}
void Matrix::initZero()
{
if (rowSize==0&&columnSize==0)
{
matrix=NULL;
return;
}
if (rowSize == 0) throw runtime_error("rows cannot be zero if columns is not zero");
if (columnSize == 0) throw runtime_error("columns cannot be zero if rows is not zero");
matrix = new double*[rowSize];
for (int i = 0; i < rowSize; ++i)
{
matrix[i] = new double[columnSize](); //uses default value 0.0
}
}
void Matrix::initCopy(const Matrix& m)
{
//ToDo
for(int i = 0; i < rowSize; ++i)
{
for(int j = 0; j < columnSize; ++j)
{
matrix[i][j] = m.matrix[i][j];
}
}
}
void Matrix::swap(Matrix& other)
{
std::swap(rowSize,other.rowSize);
std::swap(columnSize,other.columnSize);
std::swap(matrix,other.matrix);
}
//overload the assignment operator
const Matrix& Matrix::operator=(const Matrix & rightObject)
{
Matrix temp(rightObject);
swap(temp);
return *this;
}
Matrix operator+ (const Matrix& a,
const Matrix& b)
{
if (a.rowSize != b.rowSize || a.columnSize != b.columnSize)
throw runtime_error("Cannot add different size matrices");
Matrix temp(a.rowSize,a.columnSize);
for (int i = 0; i < a.rowSize; i++)
{
for (int j = 0; j < a.columnSize; j++)
{
temp.matrix[i][j] = a.matrix[i][j] + b.matrix[i][j];
}
}
return temp;
}
Matrix operator- (const Matrix& a,
const Matrix& b)
{
//ToDo
if (a.rowSize != b.rowSize || a.columnSize != b.columnSize)
throw runtime_error("Cannot add different size matrices");
Matrix temp(a.rowSize,a.columnSize);
for (int i = 0; i < a.rowSize; i++)
{
for (int j = 0; j < a.columnSize; j++)
{
temp.matrix[i][j] = a.matrix[i][j] - b.matrix[i][j];
}
}
return temp;
}
Matrix operator* (const Matrix& a,
const Matrix& b)
{
//ToDo
Matrix prod(a.rowSize, b.columnSize);
for(int i = 0; i < prod.rowSize; ++i)
{
for(int j = 0; j < prod.columnSize; ++j)
{
for(int k = 0; k < a.columnSize; ++k)
{
prod.matrix[i][j] += a.matrix[i][k] * b.matrix[k][j];
}
}
}
return prod;
}
bool operator== (const Matrix& a,
const Matrix& b)
{
if (a.rowSize != b.rowSize || a.columnSize != b.columnSize)
return false;
for (int i = 0; i < a.rowSize; i++)
{
for (int j = 0; j < a.columnSize; j++)
{
if(a.matrix[i][j] != b.matrix[i][j]) return false;
}
}
return true;
}
bool operator!= (const Matrix& a,
const Matrix& b)
{
return !(a==b);
}
ostream& operator<< (ostream& os, const Matrix& a)
//overload the stream insertion operator <<
{
//ToDo
int rows, columns;
os << rows << columns;
if(!os) return os;
Matrix temp(rows, columns);
for(int i = 0; i < rows; ++i)
{
cout << i << endl;
for(int j = 0; j < columns; ++j)
{
cout << j << endl;
os << temp.matrix[i][j] << " ";
}
os << endl;
}
return os;
}
istream& operator>> (istream& is, Matrix& a)
//overload the stream extraction operator >>
{
int rows, columns;
is >>rows>>columns;
if (!is) return is;
Matrix temp(rows,columns);
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < columns; ++j) {
is >> temp.matrix[i][j];
if (!is) return is;
}
}
temp.swap(a);
return is;
}
int Matrix::getRows() const
{
return rowSize;
}
int Matrix::getColumns() const
{
return columnSize;
}
TestMatrix.cpp file
#include <iostream>
#include <string>
#include <fstream>
#include "Matrix.h"
using namespace std;
void info(int rows, int columns) //(FYI; I do not use this function in the program)
{
cout<<"input and output for matrix is in format: rows "<<rows<<" columns "<<columns <<" matrix elements"<<endl;
}
void readMatrix( istream& is, Matrix& m, const string& name ) {
//cout << "Enter matrix " << name << " in the following format " << "rows columns element[0,0] element[0,1] ... ";
//previous comment is for input from iostream(keyboard); here we use input from ifstream(file)
is >> m;
}
int main()
{
string fileName;
cout << "Enter the input file name ";
cin >> fileName;
ifstream inData;
ofstream outData;
inData.open(fileName.c_str());
outData.open("out.txt");
//To do test with this data, change size of matrices and input
//other data to input file in.txt(as many as you need) and
//test your program for those sets of data.
Matrix matrix1(3,4);
Matrix matrix2(3,4);
Matrix matrix3(3,4);
Matrix matrix4(4,2);
Matrix matrix5(3,2);
readMatrix( inData, matrix1, "matrix1 3x4" );
readMatrix( inData, matrix2, "matrix2 3x4" );
readMatrix( inData, matrix4, "matrix4 4x2" );
inData.close();
cout << "matrix1 "<< matrix1 << endl;
cout << "matrix2 "<< matrix2 << endl;
cout << "matrix4 "<< matrix4 << endl;
outData << "matrix1 "<< matrix1 << endl;
outData << "matrix2 "<< matrix2 << endl;
outData << "matrix4 "<< matrix4 << endl;
matrix3 = matrix1 + matrix2;
cout << "matrix3 = matrix1 + matrix2 "<< matrix3 << endl;
outData << "matrix3 = matrix1 + matrix2 "<< matrix3 <<" ";
//ToDo: test subtraction;
matrix3 = matrix1 - matrix2;
cout << "matrix3 = matrix1 - matrix2/n"<< matrix3 << endl;
outData << "matrix3 = matrix1 - matrix2/n"<< matrix3 << "/n";
matrix5 = matrix1 * matrix4;
cout << "matrix1 * matrix4 "<< (matrix1*matrix4) << endl;
cout <<"matrix5 "<<matrix5 << endl;
outData << "matrix1 * matrix4 "<< (matrix1*matrix4) << " ";
outData <<"matrix5 "<<matrix5 <<" ";
outData.close();
return 0;
}
Explanation / Answer
Are you needing technical help?
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.