Hi, I'm very close to finish my last planned console program, which multiplies two matrices, but i'm having unhandled exception I check my code million of times but i dont know what's wrong
can anyone plz take a look ??
btw, this is NOT home work i'm just doing that for fun and increase the skills
header file :
Code:
#ifndef MATRIX_H
#define MATRIX_H
class Matrix
{
//Data members
private:
int m_Rows;
int m_Columns;
float** m_pMatrice;
//Member functions
public:
//Constructor
Matrix::Matrix();
//Destructor
~Matrix();
//Building function
void build(int& rows, int& columns);
//Overloading the multiplication operator
Matrix operator*(const Matrix& aMatrix);
//Resize result
void resizeResult(int rows, int columns);
};
#endif
implementation file :
Code:
#include "stdafx.h"
#include <iostream>
#include "matrix.h"
using namespace std;
//Contructor
Matrix::Matrix(): m_pMatrice(0), m_Rows(0), m_Columns(0)
{
//Empty constructor body
}
//Destructor
Matrix::~Matrix()
{
for(int i = 0; i < m_Rows; ++i)
delete[] m_pMatrice[i]; // delete array for each row
delete[] m_pMatrice; // delete column of pointers
}
//Building matrice
void Matrix::build(int& rows, int& columns)
{
m_Rows = rows;
m_Columns = columns;
cout << endl;
cout << "You have made a matrice with the will\n";
cout << "of Mr.Grant, the God of Math.\n";
cout << "[" << m_Rows << "][" << m_Columns << "]\n";
m_pMatrice = new float*[m_Rows]; // allocate m pointers
for(int i = 0; i < m_Rows; ++i)
m_pMatrice[i] = new float[m_Columns]; // allocate n columns for each row
cout << endl;
cout << "Now, please pay attention, we will fill up" << endl;
cout << "the entries of your matrice." << endl;
for(int i = 0; i < m_Rows; ++i)
{
for(int j = 0; j < m_Columns; ++j)
{
cout << "entry[" << i << "][" << j << "] = ";
cin >> m_pMatrice[i][j];
}
}
}
//Resizing result
void Matrix::resizeResult(int rows, int columns)
{
// create the new matrix
this->m_pMatrice = new float*[rows];
for(int i = 0; i < rows; ++i)
this->m_pMatrice[i] = new float[columns];
this->m_Rows = rows;
this->m_Columns = columns;
}
//Multiplying matrices
Matrix Matrix::operator*(const Matrix& rhs)
{
Matrix result;
if (this == &rhs)
{
return *this;
}
else
{
//Checks if matrices are able to be multiplied
if (this->m_Columns == rhs.m_Rows)
{
//Do multiplication
result.resizeResult(this->m_Rows, rhs.m_Columns);
for(int i = 0; i < result.m_Rows; ++i)
for(int j = 0; j < result.m_Columns; ++j)
//Error occurs here, "Unhandled exception"
result.m_pMatrice[i][j] = (float)i*rhs.m_Columns+j;
}
else
{
cout << "Cannot perform multiplication." << endl;
cout << "Columns of first matrix don't ";
cout << "match the rows of the second matrix" << endl;
}
}
return result;
}
application code :
Code:
//Special thanks to Frank Luna who helped me :)
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include "matrix.h"
using namespace std;
int main()
{
cout << "\tMatrices Multiplication 1.0 Beta" << endl;
cout << "\tDedicated specially with gratitude to my math teacher: " << endl;
cout << "\tMr.Grant." << endl << endl;
int rows, columns;
//////////////////////////////////////////////////////
////////////////Creating Matrice1/////////////////////
Matrix mtrx1;
cout << "Enter the rows of the first matrice: "; cin >> rows;
cout << endl;
cout << "Enter the columns of the first matrice: ";cin >> columns;
mtrx1.build(rows, columns);
cout << endl;
//////////////////////////////////////////////////////
//////////////Creating Matrice2///////////////////////
Matrix mtrx2;
cout << "Enter the rows of the second matrice: "; cin >> rows;
cout << endl;
cout << "Enter the columns of the second matrice: "; cin >> columns;
mtrx2.build(rows, columns);
//////////////////////////////////////////////////////
////////////////////////////////////////////////////////
//////////////Multiplication process////////////////////
Matrix result;
result = mtrx1 * mtrx2;
////////////////////////////////////////////////////////
//Cleaning allocated memory
mtrx1.~Matrix();
mtrx2.~Matrix();
result.~Matrix();
getch();
return 0;
}