C++ - Matrix Addition, Subtraction and Comparison
I have given the program for matrix addition, subtraction and comparison. The class CMatrix is using operator overloading +, -, =, ==, >> and << along with copy constructor.
Click here to access the simple version of the same program with out using classes
Source Code
#include <stdio.h>
#include <iostream>
#include <tchar.h>
#include <math.h>
class CMatrix
{
private:
int m_rows;
int m_cols;
int **m_pData;
char m_name[128];
CMatrix();
public:
CMatrix(const char *name, int rows, int cols) : m_rows(rows), m_cols(cols)
{
strcpy(m_name, name);
m_pData = new int*[m_rows];
for(int i = 0; i < m_rows; i++)
m_pData[i] = new int[m_cols];
for(int i = 0; i < m_rows; i++)
{
for(int j = 0; j < m_cols; j++)
{
m_pData[i][j] = 0;
}
}
}
CMatrix(const CMatrix &other)
{
strcpy(m_name, other.m_name);
m_rows = other.m_rows;
m_cols = other.m_cols;
m_pData = new int*[m_rows];
for(int i = 0; i < m_rows; i++)
m_pData[i] = new int[m_cols];
for(int i = 0; i < m_rows; i++)
{
for(int j = 0; j < m_cols; j++)
{
m_pData[i][j] = other.m_pData[i][j];
}
}
}
~CMatrix()
{
for(int i = 0; i < m_rows; i++)
delete [] m_pData[i];
delete [] m_pData;
m_rows = m_cols = 0;
}
void SetName(const char *name) { strcpy(m_name, name); }
const char* GetName() const { return m_name; }
void GetInput()
{
std::cin >> *this;
}
void FillSimulatedInput()
{
static int factor1 = 10, factor2 = 5;
std::cout << "\n\nEnter Input For Matrix : " << m_name << " Rows: " << m_rows << " Cols: " << m_cols << "\n";
for(int i = 0; i < m_rows; i++)
{
for(int j = 0; j < m_cols; j++)
{
std::cout << "Input For Row: " << i + 1 << " Col: " << j + 1 << " = ";
int data = ((i + 1) * factor1) + (j + 1) * factor2;
m_pData[i][j] = data;
std::cout << m_pData[i][j] << "\n";
}
std::cout << "\n";
}
factor1 += 5;
factor2 += 5;
std::cout << "\n";
}
CMatrix& operator = (const CMatrix &other)
{
if( this->m_rows != other.m_rows ||
this->m_cols != other.m_cols)
{
std::cout << "WARNING: Assignment is taking place with by changing the number of rows and columns of the matrix";
}
for(int i = 0; i < m_rows; i++)
delete [] m_pData[i];
delete [] m_pData;
m_rows = m_cols = 0;
strcpy(m_name, other.m_name);
m_rows = other.m_rows;
m_cols = other.m_cols;
m_pData = new int*[m_rows];
for(int i = 0; i < m_rows; i++)
m_pData[i] = new int[m_cols];
for(int i = 0; i < m_rows; i++)
{
for(int j = 0; j < m_cols; j++)
{
m_pData[i][j] = other.m_pData[i][j];
}
}
return *this;
}
CMatrix operator + (const CMatrix &other)
{
if( this->m_rows != other.m_rows ||
this->m_cols != other.m_cols)
{
std::cout << "Addition could not take place because number of rows and columns are different between the two matrices";
return *this;
}
CMatrix result("", m_rows, m_cols);
for(int i = 0; i < m_rows; i++)
{
for(int j = 0; j < m_cols; j++)
{
result.m_pData[i][j] = this->m_pData[i][j] + other.m_pData[i][j];
}
}
return result;
}
CMatrix operator - (const CMatrix &other)
{
if( this->m_rows != other.m_rows ||
this->m_cols != other.m_cols)
{
std::cout << "Subtraction could not take place because number of rows and columns are different between the two matrices";
return *this;
}
CMatrix result("", m_rows, m_cols);
for(int i = 0; i < m_rows; i++)
{
for(int j = 0; j < m_cols; j++)
{
result.m_pData[i][j] = this->m_pData[i][j] - other.m_pData[i][j];
}
}
return result;
}
CMatrix operator * (const CMatrix &other)
{
if( this->m_cols != other.m_rows)
{
std::cout << "Multiplication could not take place because number of columns of 1st Matrix and number of rows in 2nd Matrix are different";
return *this;
}
CMatrix result("", this->m_rows, other.m_cols);
for(int i = 0; i < this->m_rows; i++)
{
for(int j = 0; j < other.m_cols; j++)
{
for(int k = 0; k < this->m_cols; k++)
{
result.m_pData[i][j] += this->m_pData[i][k] * other.m_pData[k][j];
}
}
}
return result;
}
bool operator == (const CMatrix &other)
{
if( this->m_rows != other.m_rows ||
this->m_cols != other.m_cols)
{
std::cout << "Comparision could not take place because number of rows and columns are different between the two matrices";
return false;
}
CMatrix result("", m_rows, m_cols);
bool bEqual = true;
for(int i = 0; i < m_rows; i++)
{
for(int j = 0; j < m_cols; j++)
{
if(this->m_pData[i][j] != other.m_pData[i][j])
bEqual = false;
}
}
return bEqual;
}
void DisplayMatrix()
{
std::cout << *this;
}
friend std::istream& operator >> (std::istream &is, CMatrix &m);
friend std::ostream& operator << (std::ostream &os, const CMatrix &m);
};
std::istream& operator >> (std::istream &is, CMatrix &m)
{
std::cout << "\n\nEnter Input For Matrix : " << m.m_name << " Rows: " << m.m_rows << " Cols: " << m.m_cols << "\n";
for(int i = 0; i < m.m_rows; i++)
{
for(int j = 0; j < m.m_cols; j++)
{
std::cout << "Input For Row: " << i + 1 << " Col: " << j + 1 << " = ";
is >> m.m_pData[i][j];
}
std::cout << "\n";
}
std::cout << "\n";
return is;
}
std::ostream& operator << (std::ostream &os,const CMatrix &m)
{
os << "\n\nMatrix : " << m.m_name << " Rows: " << m.m_rows << " Cols: " << m.m_cols << "\n\n";
for(int i = 0; i < m.m_rows; i++)
{
os << " | ";
for(int j = 0; j < m.m_cols; j++)
{
os.width(4);
os << m.m_pData[i][j] << " ";
}
os << "|\n";
}
os << "\n\n";
return os;
}
void main()
{
const int ROWS = 4, COLS = 3;
CMatrix a("A", ROWS,COLS);
CMatrix b("B", ROWS,COLS);
a.FillSimulatedInput();
b.FillSimulatedInput();
//std::cin >> a;
//std::cin >> b;
CMatrix c = a + b;
c.SetName("C");
CMatrix d = c - a; // so d should be equal to b
d.SetName("D");
std::cout << a;
std::cout << b;
std::cout << c;
std::cout << d;
if( d == b)
{
std::cout << "Matrix " << d.GetName() << " and " << b.GetName() << " are EQUAL\n\n";
}
}
Output
Enter Input For Matrix : A Rows: 4 Cols: 3
Input For Row: 1 Col: 1 = 15
Input For Row: 1 Col: 2 = 20
Input For Row: 1 Col: 3 = 25
Input For Row: 2 Col: 1 = 25
Input For Row: 2 Col: 2 = 30
Input For Row: 2 Col: 3 = 35
Input For Row: 3 Col: 1 = 35
Input For Row: 3 Col: 2 = 40
Input For Row: 3 Col: 3 = 45
Input For Row: 4 Col: 1 = 45
Input For Row: 4 Col: 2 = 50
Input For Row: 4 Col: 3 = 55
Enter Input For Matrix : B Rows: 4 Cols: 3
Input For Row: 1 Col: 1 = 25
Input For Row: 1 Col: 2 = 35
Input For Row: 1 Col: 3 = 45
Input For Row: 2 Col: 1 = 40
Input For Row: 2 Col: 2 = 50
Input For Row: 2 Col: 3 = 60
Input For Row: 3 Col: 1 = 55
Input For Row: 3 Col: 2 = 65
Input For Row: 3 Col: 3 = 75
Input For Row: 4 Col: 1 = 70
Input For Row: 4 Col: 2 = 80
Input For Row: 4 Col: 3 = 90
Matrix : A Rows: 4 Cols: 3
| 15 20 25 |
| 25 30 35 |
| 35 40 45 |
| 45 50 55 |
Matrix : B Rows: 4 Cols: 3
| 25 35 45 |
| 40 50 60 |
| 55 65 75 |
| 70 80 90 |
Matrix : C Rows: 4 Cols: 3
| 40 55 70 |
| 65 80 95 |
| 90 105 120 |
| 115 130 145 |
Matrix : D Rows: 4 Cols: 3
| 25 35 45 |
| 40 50 60 |
| 55 65 75 |
| 70 80 90 |
Matrix D and B are EQUAL
Press any key to continue . . .
|