Hello, I am writing a program that does matrix Multiplication using 2-dimensional arrays and a Matrix Class. The program compiles without any errors but when I get to the part of the program where it actually multiplies the 2 matrices it throws an exception. I've looked through the code several times and I can't figure out why it's doing this.
Here is the error I get
Unhandled exception at 0x01002e23 in mm.exe: 0xC0000005: Access violation reading location 0x00000000.
Here is my code:
//matrix.h
#include <iostream>
#include <fstream>
#include <string>
#include "date.h"
#define isnan(x) ((x) != (x))
//#define DEBUG
#define ERROR
using namespace std;
class cMatrix
{
private:
//Declare Matricies
int** m_matrix1; //First Matrix
int** m_matrix2; //Second Matrix
int** m_product; //Product Matrix
//Declare Columns and Rows of Matrix
int m_row1, m_col1, //Matrix 1
m_row2, m_col2; //Matrix 2
//Declare Number of Times Program Has Run
//int runThru;
//Declare Date Class
cDate* m_date;
//Declare Error State
bool m_error;
//Declare Private Functions
bool getRowsnCols(int&, int&); //Initialize Matrices
bool initMatrices(int**, int, int); //Retrieve Input from User
void multiplyMatrices(); //Multiply Matricies
public:
cMatrix();
~cMatrix();
void Run();
bool Error() { return m_error; }
};
//matrix.cpp
#include "matrix.h"
ifstream din;
ofstream mout, dout;
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
// //
// Matrix Class Constructor............................. //
// .............................................{cMCC} //
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
cMatrix::cMatrix()
: m_matrix1(NULL), m_matrix2(NULL), m_row1(0), m_col1(0),
m_row2(0), m_col2(0), m_date(NULL), m_error(false)
{
din.open("debug.in");
mout.open("product.out", ios::app);
dout.open("debug.out");
}
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
// //
// Get Rows & Columns................................... //
// ............................................{cMgRC} //
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
bool cMatrix::getRowsnCols(int& row, int& col)
{
//Get Number of Rows
cout << "Enter the Number of Rows\n"
<< "$> ";
cin >> row;
cout << endl;
if (row == 0)
{
cout << "ERROR: Number of Rows Cannot Be 0!\n";
m_error = 1;
system("pause");
return 0;
}
#ifdef isnan
if (isnan(row))
{
cout << "ERROR: Number of Rows must be a number!\n";
m_error = 1;
system("pause");
return 0;
}
#endif //isnan
//Get Number of Columns
cout << "Enter the Number of Columns\n"
<< "$> ";
cin >> col;
cout << endl;
if (col == 0)
{
cout << "ERROR: Number of Columns Cannot Be 0!\n";
m_error = 1;
system("pause");
return 0;
}
#ifdef isnan
if (isnan(col))
{
cout << "ERROR: Number of Columns must be a number!\n";
m_error = 1;
system("pause");
return 0;
}
#endif //isnan
mout << "Rows: " << row << endl;
mout << "Cols: " << col << endl << endl;
return 1;
}
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
// //
// Initialize Matrices.................................. //
// .............................................{cMiM} //
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
bool cMatrix::initMatrices(int** mat, int r, int c)
{
mat = new int* [r];
int x, y;
for (x = 0; x < r; x++)
mat[x] = new int[c];
mat[0][0] = 0;
for (x = 0; x < r; x++)
{
for (y = 0; y < c; y++)
{
cout << "Enter Number for Position\n"
<< "(" << x << "," << y << ")$> ";
cin >> mat[x][y];
cout << endl;
}
}
for (x = 0; x < r; x++)
{
for (y = 0; y < c; y++)
{
cout << mat[x][y] << " ";
mout << mat[x][y] << " ";
}
cout << endl;
mout << endl;
}
mout << endl;
return 1;
}
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
// //
// Multiply Matricies................................... //
// .............................................{cMmM} //
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
void cMatrix::multiplyMatrices()
{
m_product = new int* [m_row1];
int y,x,z;
for (int i = 0; i < m_row1; i++)
m_product[i] = new int[m_col2];
for (x = 0; x < m_row1; x++)
{
for (y = 0; y < m_col2; y++)
{
m_product[x][y] = 0;
for (z = 0; z < m_row1; z++)
{
m_product[x][y] += m_matrix1[x][z] * m_matrix2[z][y];
}
cout << m_product[x][y] << " ";
mout << m_product[x][y] << " ";
}
cout << endl;
mout << endl << endl;
}
}
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
// //
// Run Matrix Program................................... //
// ............................................{cMrun} //
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
void cMatrix::Run()
{
m_date = new cDate;
string date = m_date->dtStr();
mout << "================================================"
<< endl << date << endl
<< "================================================"
<< endl << endl;
cout << "#####################\n"
<< "## Matrix 1 ##\n"
<< "#####################\n\n";
mout << "#####################" << endl
<< "## Matrix 1 ##" << endl
<< "#####################" << endl << endl;
getRowsnCols(m_row1, m_col1);
cout << "##\n"
<< "## Input numbers\n"
<< "#####################\n\n";
initMatrices(m_matrix1, m_row1, m_col1);
cout << "#####################\n"
<< "## Matrix 2 ##\n"
<< "#####################\n\n";
mout << "#####################" << endl
<< "## Matrix 2 ##" << endl
<< "#####################" << endl << endl;
getRowsnCols(m_row2, m_col2);
cout << "##\n"
<< "## Input numbers\n"
<< "#####################\n\n";
initMatrices(m_matrix2, m_row2, m_col2);
cout << "The Product of the 2 Matricies Are:\n";
multiplyMatrices();
}
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
// //
// Matrix Class Destructor.............................. //
// .............................................{cMCD} //
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
cMatrix::~cMatrix()
{
delete m_matrix1;
delete m_matrix2;
delete m_product;
delete m_date;
}
//main.cpp
#include "matrix.h"
cMatrix* m_matrix;
int main()
{
m_matrix = new cMatrix;
cout << "############################################\n"
<< "############################################\n"
<< "## ##\n"
<< "## Welcome to Matrix Multiplier ##\n"
<< "## by David Levinson ##\n"
<< "## ##\n"
<< "############################################\n"
<< "############################################\n\n\n";
if (m_matrix->Error())
{
cout << "Program Error! Exiting Program!\n";
system("pause");
return 0;
}
m_matrix->Run();
system("pause");
return 0;
}