Changes

MyWikiBiz, Author Your Legacy — Sunday November 24, 2024
Jump to navigationJump to search
Template program added
Line 464: Line 464:  
cout<<"\n\nHit enter to exit the program: "<<flush;
 
cout<<"\n\nHit enter to exit the program: "<<flush;
 
cin.get();*/
 
cin.get();*/
 +
}
 +
</pre>
 +
 +
=With Templates=
 +
==MyMatrix.h==
 +
<pre>
 +
#pragma once
 +
#include <iostream>
 +
using namespace std;
 +
 +
template <class T>
 +
class MyMatrix
 +
{
 +
public:
 +
MyMatrix();
 +
MyMatrix(const MyMatrix<T>& m);
 +
MyMatrix(int rows, int columns);
 +
~MyMatrix();
 +
MyMatrix operator+(const MyMatrix<T>& m2) const;
 +
MyMatrix operator-(const MyMatrix<T>& m2) const;
 +
MyMatrix operator=(const MyMatrix<T>& m);
 +
T* operator[](int i);
 +
T* operator[](int i) const;
 +
double getDeterminant(MyMatrix<T>& m);
 +
void Read(istream& is);
 +
void Write(ostream& os) const;
 +
private:
 +
T **m_dpp;
 +
int m_rows;
 +
int m_columns;
 +
};
 +
template <class T>
 +
istream& operator>>(istream& is, MyMatrix<T>& m);
 +
template <class T>
 +
ostream& operator<<(ostream& os, const MyMatrix<T>& m);
 +
 +
template <class T>
 +
double MyMatrix<T>::getDeterminant(MyMatrix<T>& m)
 +
{
 +
int r, c;
 +
r = m.m_rows;
 +
c = m.m_columns;
 +
 +
double determinant = 0.0;
 +
 +
if(r == c)
 +
{
 +
if(r < 1)
 +
{
 +
cout<<"Less than one row and/or column";
 +
exit(1);
 +
}
 +
else if(r == 1)
 +
{
 +
return m[0][0];
 +
}
 +
else if(r == 2)
 +
{
 +
//cout<<"The determinant is: "<<((m[0][0]*m[1][1]) - (m[0][1]*m[1][0]));
 +
determinant = ((m[0][0]*m[1][1]) - (m[1][0]*m[0][1]));
 +
return determinant;
 +
}
 +
else
 +
{
 +
for(int columns = 0; columns < m.m_columns; columns++)
 +
{
 +
MyMatrix m2((m.m_rows-1),(m.m_columns-1));
 +
 +
for(int subRows = 1; subRows < m.m_rows; subRows++)
 +
{
 +
int subColumns = 0;
 +
 +
for(int i = 0; i < m.m_columns; i++)
 +
{
 +
if(i != columns)
 +
{
 +
m2[(subRows-1)][subColumns] = m[subRows][i];
 +
subColumns++;
 +
}
 +
}
 +
}
 +
determinant = determinant + ((m[0][columns])*(pow(float(-1),float(columns)))*(m2.getDeterminant(m2)));
 +
}
 +
}
 +
return determinant;
 +
}
 +
else
 +
{
 +
cout<<"Error, the matrix is uneven, cannot determine the matrix, exiting program.";
 +
exit(1);
 +
}
 +
}
 +
template <class T>
 +
MyMatrix<T>::MyMatrix()
 +
{
 +
m_rows = 0;
 +
m_columns = 0;
 +
m_dpp = NULL;
 +
}
 +
template <class T>
 +
MyMatrix<T>::MyMatrix(int rows, int columns)
 +
{
 +
int i, j;
 +
T *dp;
 +
 +
this->m_rows = rows;
 +
this->m_columns = columns;
 +
dp = new T[m_rows*m_columns];
 +
m_dpp = new T*[m_rows];
 +
 +
for(i = 0;i < m_rows;i++)
 +
m_dpp[i] = dp+(i * m_columns);
 +
for(i = 0;i < m_rows;i++)
 +
for(j = 0;j < m_columns;j++)
 +
m_dpp[i][j] = 0;
 +
}
 +
template <class T>
 +
MyMatrix<T>::MyMatrix(const MyMatrix<T>& m)
 +
{
 +
int i, j;
 +
T *dp;
 +
 +
this->m_rows = m.m_rows;
 +
this->m_columns = m.m_columns;
 +
dp = new T[m_rows*m_columns];
 +
m_dpp = new T*[m_rows];
 +
 +
for(i = 0;i < m_rows;i++)
 +
m_dpp[i] = dp+(i * m_columns);
 +
for(i = 0;i < m_rows;i++)
 +
for(j = 0;j < m_columns;j++)
 +
m_dpp[i][j] = m.m_dpp[i][j];
 +
}
 +
template <class T>
 +
MyMatrix<T>::~MyMatrix()
 +
{
 +
if(m_dpp != NULL)
 +
{
 +
delete [] m_dpp[0];
 +
delete [] m_dpp;
 +
}
 +
}
 +
template <class T>
 +
MyMatrix<T> MyMatrix<T>::operator=(const MyMatrix<T>& m)
 +
{
 +
int i, j;
 +
 +
if(this != &m)
 +
{
 +
if((m.m_rows == m_rows) && (m.m_columns == m_columns))
 +
{
 +
for(i = 0;i < m_rows;i++)
 +
for(j = 0;j < m_columns;j++)
 +
m_dpp[i][j] = m.m_dpp[i][j];
 +
return *this;
 +
}
 +
if(m_dpp != NULL)
 +
{
 +
delete [] m_dpp[0];
 +
delete [] m_dpp;
 +
}
 +
 +
T* dp;
 +
this->m_rows = m.m_rows;
 +
this->m_columns = m.m_columns;
 +
dp = new T[m_rows*m_columns];
 +
m_dpp = new T*[m_rows];
 +
 +
for(i = 0;i < m_rows;i++)
 +
m_dpp[i] = dp+(i * m_columns);
 +
for(i = 0;i < m_rows;i++)
 +
for(j = 0;j < m_columns;j++)
 +
m_dpp[i][j] = m.m_dpp[i][j];
 +
}
 +
return *this;
 +
}
 +
template <class T>
 +
MyMatrix<T> MyMatrix<T>::operator+(const MyMatrix<T>& m2) const
 +
{
 +
int i, j;
 +
 +
if((m2.m_rows != m_rows) || (m2.m_columns != m_columns))
 +
{
 +
cout<<"Error, different rows and/or columns.\n"
 +
<<"Press enter to exit the program."<<flush;
 +
cin.get();
 +
cin.get();
 +
exit(1);
 +
}
 +
MyMatrix sum(m2.m_rows,m2.m_columns);
 +
for(i = 0;i < sum.m_rows;i++)
 +
for(j = 0;j < sum.m_columns;j++)
 +
sum.m_dpp[i][j] = m_dpp[i][j] + m2.m_dpp[i][j];
 +
return sum;
 +
}
 +
template <class T>
 +
MyMatrix<T> MyMatrix<T>::operator-(const MyMatrix<T>& m2) const
 +
{
 +
int i, j;
 +
 +
if((m2.m_rows != m_rows) || (m2.m_columns != m_columns))
 +
{
 +
cout<<"Error, different rows and/or columns.\n"
 +
<<"Press enter to exit the program."<<flush;
 +
cin.get();
 +
cin.get();
 +
exit(1);
 +
}
 +
MyMatrix sum(m2.m_rows,m2.m_columns);
 +
for(i = 0;i < m2.m_rows;i++)
 +
for(j = 0;j < m2.m_columns;j++)
 +
sum.m_dpp[i][j] = m_dpp[i][j] - m2.m_dpp[i][j];
 +
return sum;
 +
}
 +
template <class T>
 +
T* MyMatrix<T>::operator[](int i)
 +
{
 +
if(i >= 0 && i < m_rows)
 +
return m_dpp[i];
 +
else
 +
{
 +
cout<<"Error, Out of Range. Press enter to exit the program."<<flush;
 +
cin.get();
 +
cin.get();
 +
exit(1);
 +
}
 +
}
 +
template <class T>
 +
T* MyMatrix<T>::operator[](int i) const
 +
{
 +
if(i >= 0 && i < m_rows)
 +
return m_dpp[i];
 +
else
 +
{
 +
cout<<"Error, Out of Range. Press enter to exit the program."<<flush;
 +
cin.get();
 +
cin.get();
 +
exit(1);
 +
}
 +
}
 +
template <class T>
 +
void MyMatrix<T>::Read(istream& is)
 +
{
 +
int i, j;
 +
int rows, columns;
 +
 +
if(is == cin)
 +
{
 +
cout<<"Enter the number of rows: "<<flush;
 +
cin>>rows;
 +
cout<<"Enter the number of columns: "<<flush;
 +
cin>>columns;
 +
 +
if((rows == m_rows) && (columns == m_columns))
 +
{
 +
for(i = 0;i < m_rows;i++)
 +
{
 +
for(j = 0;j < m_columns;j++)
 +
{
 +
cout<<"m["<<i<<"]"<<"["<<j<<"] = "<<flush;
 +
cin>>m_dpp[i][j];
 +
}
 +
}
 +
}
 +
else
 +
{
 +
if(m_dpp != NULL)
 +
{
 +
delete [] m_dpp[0];
 +
delete [] m_dpp;
 +
}
 +
 +
m_rows = rows;
 +
m_columns = columns;
 +
 +
T *dp;
 +
dp = new T[m_rows*m_columns];
 +
m_dpp = new T*[m_rows];
 +
 +
for(i = 0;i < m_rows;i++)
 +
m_dpp[i] = dp + (i * m_columns);
 +
for(i = 0;i < m_rows;i++)
 +
{
 +
for(j = 0;j < m_columns;j++)
 +
{
 +
cout<<"m["<<i<<"]"<<"["<<j<<"] = "<<flush;
 +
cin>>m_dpp[i][j];
 +
}
 +
}
 +
}
 +
}
 +
else
 +
{
 +
is>>m_rows;
 +
is>>m_columns;
 +
 +
if(m_dpp != NULL)
 +
{
 +
delete [] m_dpp[0];
 +
delete [] m_dpp;
 +
}
 +
 +
T *dp;
 +
dp = new T[m_rows*m_columns];
 +
m_dpp = new T*[m_rows];
 +
 +
for(i = 0;i < m_rows;i++)
 +
m_dpp[i] = dp + (i * m_columns);
 +
for(i = 0;i < m_rows;i++)
 +
{
 +
for(j = 0;j < m_columns;j++)
 +
is>>m_dpp[i][j];
 +
}
 +
}
 +
}
 +
template <class T>
 +
void MyMatrix<T>::Write(ostream& os) const
 +
{
 +
int i, j;
 +
 +
if(os == cout)
 +
{
 +
if((m_rows == 0) || (m_columns == 0))
 +
cout<<"The matrix is empty.\n";
 +
 +
for(i = 0;i < m_rows;i++)
 +
{
 +
if(i > 0)
 +
cout<<"\n\n";
 +
 +
for(j = 0;j < m_columns;j++)
 +
{
 +
cout<<m_dpp[i][j]<<" ";
 +
}
 +
cout<<"\n";
 +
}
 +
}
 +
else
 +
{
 +
for(i = 0;i < m_rows;i++)
 +
{
 +
for(j = 0;j < m_columns;j++)
 +
{
 +
os<<m_dpp[i][j]<<"\n";
 +
}
 +
}
 +
}
 +
}
 +
template <class T>
 +
istream& operator>>(istream& is, MyMatrix<T>& m)
 +
{
 +
m.Read(is);
 +
return is;
 +
}
 +
template <class T>
 +
ostream& operator<<(ostream& os, const MyMatrix<T>& m)
 +
{
 +
m.Write(os);
 +
return os;
 +
}
 +
</pre>
 +
==MyMatrix.cpp==
 +
<pre>
 +
#include <iostream>
 +
#include <cmath>
 +
#include "MyMatrix.h"
 +
using namespace std;
 +
 +
template <class T>
 +
T MyMatrix<T>::getDeterminant(MyMatrix& m)
 +
{
 +
int r, c;
 +
r = m.m_rows;
 +
c = m.m_columns;
 +
 +
T determinant = 0.0;
 +
 +
if(r == c)
 +
{
 +
if(r < 1)
 +
{
 +
cout<<"Less than one row and/or column";
 +
exit(1);
 +
}
 +
else if(r == 1)
 +
{
 +
return m[0][0];
 +
}
 +
else if(r == 2)
 +
{
 +
//cout<<"The determinant is: "<<((m[0][0]*m[1][1]) - (m[0][1]*m[1][0]));
 +
determinant = ((m[0][0]*m[1][1]) - (m[1][0]*m[0][1]));
 +
return determinant;
 +
}
 +
else
 +
{
 +
for(int columns = 0; columns < m.m_columns; columns++)
 +
{
 +
MyMatrix m2((m.m_rows-1),(m.m_columns-1));
 +
 +
for(int subRows = 1; subRows < m.m_rows; subRows++)
 +
{
 +
int subColumns = 0;
 +
 +
for(int i = 0; i < m.m_columns; i++)
 +
{
 +
if(i != columns)
 +
{
 +
m2[(subRows-1)][subColumns] = m[subRows][i];
 +
subColumns++;
 +
}
 +
}
 +
}
 +
determinant = determinant + ((m[0][columns])*(pow(float(-1),float(columns)))*(m2.getDeterminant(m2)));
 +
}
 +
}
 +
return determinant;
 +
}
 +
else
 +
{
 +
cout<<"Error, the matrix is uneven, cannot determine the matrix, exiting program.";
 +
exit(1);
 +
}
 +
}
 +
template <class T>
 +
MyMatrix<T>::MyMatrix()
 +
{
 +
m_rows = 0;
 +
m_columns = 0;
 +
m_dpp = NULL;
 +
}
 +
template <class T>
 +
MyMatrix<T>::MyMatrix(int rows, int columns)
 +
{
 +
int i, j;
 +
T *dp;
 +
 +
this->m_rows = rows;
 +
this->m_columns = columns;
 +
dp = new T[m_rows*m_columns];
 +
m_dpp = new T*[m_rows];
 +
 +
for(i = 0;i < m_rows;i++)
 +
m_dpp[i] = dp+(i * m_columns);
 +
for(i = 0;i < m_rows;i++)
 +
for(j = 0;j < m_columns;j++)
 +
m_dpp[i][j] = 0;
 +
}
 +
template <class T>
 +
MyMatrix<T>::MyMatrix(const MyMatrix& m)
 +
{
 +
int i, j;
 +
T *dp;
 +
 +
this->m_rows = m.m_rows;
 +
this->m_columns = m.m_columns;
 +
dp = new T[m_rows*m_columns];
 +
m_dpp = new T*[m_rows];
 +
 +
for(i = 0;i < m_rows;i++)
 +
m_dpp[i] = dp+(i * m_columns);
 +
for(i = 0;i < m_rows;i++)
 +
for(j = 0;j < m_columns;j++)
 +
m_dpp[i][j] = m.m_dpp[i][j];
 +
}
 +
template <class T>
 +
MyMatrix<T>::~MyMatrix()
 +
{
 +
if(m_dpp != NULL)
 +
{
 +
delete [] m_dpp[0];
 +
delete [] m_dpp;
 +
}
 +
}
 +
template <class T>
 +
MyMatrix<T> MyMatrix<T>::operator=(const MyMatrix& m)
 +
{
 +
int i, j;
 +
 +
if(this != &m)
 +
{
 +
if((m.m_rows == m_rows) && (m.m_columns == m_columns))
 +
{
 +
for(i = 0;i < m_rows;i++)
 +
for(j = 0;j < m_columns;j++)
 +
m_dpp[i][j] = m.m_dpp[i][j];
 +
return *this;
 +
}
 +
if(m_dpp != NULL)
 +
{
 +
delete [] m_dpp[0];
 +
delete [] m_dpp;
 +
}
 +
 +
T* dp;
 +
this->m_rows = m.m_rows;
 +
this->m_columns = m.m_columns;
 +
dp = new T[m_rows*m_columns];
 +
m_dpp = new T*[m_rows];
 +
 +
for(i = 0;i < m_rows;i++)
 +
m_dpp[i] = dp+(i * m_columns);
 +
for(i = 0;i < m_rows;i++)
 +
for(j = 0;j < m_columns;j++)
 +
m_dpp[i][j] = m.m_dpp[i][j];
 +
}
 +
return *this;
 +
}
 +
template <class T>
 +
MyMatrix<T> MyMatrix<T>::operator+(const MyMatrix& m2) const
 +
{
 +
int i, j;
 +
 +
if((m2.m_rows != m_rows) || (m2.m_columns != m_columns))
 +
{
 +
cout<<"Error, different rows and/or columns.\n"
 +
<<"Press enter to exit the program."<<flush;
 +
cin.get();
 +
cin.get();
 +
exit(1);
 +
}
 +
MyMatrix sum(m2.m_rows,m2.m_columns);
 +
for(i = 0;i < sum.m_rows;i++)
 +
for(j = 0;j < sum.m_columns;j++)
 +
sum.m_dpp[i][j] = m_dpp[i][j] + m2.m_dpp[i][j];
 +
return sum;
 +
}
 +
template <class T>
 +
MyMatrix<T> MyMatrix<T>::operator-(const MyMatrix& m2) const
 +
{
 +
int i, j;
 +
 +
if((m2.m_rows != m_rows) || (m2.m_columns != m_columns))
 +
{
 +
cout<<"Error, different rows and/or columns.\n"
 +
<<"Press enter to exit the program."<<flush;
 +
cin.get();
 +
cin.get();
 +
exit(1);
 +
}
 +
MyMatrix sum(m2.m_rows,m2.m_columns);
 +
for(i = 0;i < m2.m_rows;i++)
 +
for(j = 0;j < m2.m_columns;j++)
 +
sum.m_dpp[i][j] = m_dpp[i][j] - m2.m_dpp[i][j];
 +
return sum;
 +
}
 +
template <class T>
 +
T* MyMatrix<T>::operator[](int i)
 +
{
 +
if(i >= 0 && i < m_rows)
 +
return m_dpp[i];
 +
else
 +
{
 +
cout<<"Error, Out of Range. Press enter to exit the program."<<flush;
 +
cin.get();
 +
cin.get();
 +
exit(1);
 +
}
 +
}
 +
template <class T>
 +
T* MyMatrix<T>::operator[](int i) const
 +
{
 +
if(i >= 0 && i < m_rows)
 +
return m_dpp[i];
 +
else
 +
{
 +
cout<<"Error, Out of Range. Press enter to exit the program."<<flush;
 +
cin.get();
 +
cin.get();
 +
exit(1);
 +
}
 +
}
 +
template <class T>
 +
void MyMatrix<T>::Read(istream& is)
 +
{
 +
int i, j;
 +
int rows, columns;
 +
 +
if(is == cin)
 +
{
 +
cout<<"Enter the number of rows: "<<flush;
 +
cin>>rows;
 +
cout<<"Enter the number of columns: "<<flush;
 +
cin>>columns;
 +
 +
if((rows == m_rows) && (columns == m_columns))
 +
{
 +
for(i = 0;i < m_rows;i++)
 +
{
 +
for(j = 0;j < m_columns;j++)
 +
{
 +
cout<<"m["<<i<<"]"<<"["<<j<<"] = "<<flush;
 +
cin>>m_dpp[i][j];
 +
}
 +
}
 +
}
 +
else
 +
{
 +
if(m_dpp != NULL)
 +
{
 +
delete [] m_dpp[0];
 +
delete [] m_dpp;
 +
}
 +
 +
m_rows = rows;
 +
m_columns = columns;
 +
 +
T *dp;
 +
dp = new T[m_rows*m_columns];
 +
m_dpp = new T*[m_rows];
 +
 +
for(i = 0;i < m_rows;i++)
 +
m_dpp[i] = dp + (i * m_columns);
 +
for(i = 0;i < m_rows;i++)
 +
{
 +
for(j = 0;j < m_columns;j++)
 +
{
 +
cout<<"m["<<i<<"]"<<"["<<j<<"] = "<<flush;
 +
cin>>m_dpp[i][j];
 +
}
 +
}
 +
}
 +
}
 +
else
 +
{
 +
is>>m_rows;
 +
is>>m_columns;
 +
 +
if(m_dpp != NULL)
 +
{
 +
delete [] m_dpp[0];
 +
delete [] m_dpp;
 +
}
 +
 +
T *dp;
 +
dp = new T[m_rows*m_columns];
 +
m_dpp = new T*[m_rows];
 +
 +
for(i = 0;i < m_rows;i++)
 +
m_dpp[i] = dp + (i * m_columns);
 +
for(i = 0;i < m_rows;i++)
 +
{
 +
for(j = 0;j < m_columns;j++)
 +
is>>m_dpp[i][j];
 +
}
 +
}
 +
}
 +
template <class T>
 +
void MyMatrix::Write(ostream& os) const
 +
{
 +
int i, j;
 +
 +
if(os == cout)
 +
{
 +
if((m_rows == 0) || (m_columns == 0))
 +
cout<<"The matrix is empty.\n";
 +
 +
for(i = 0;i < m_rows;i++)
 +
{
 +
if(i > 0)
 +
cout<<"\n\n";
 +
 +
for(j = 0;j < m_columns;j++)
 +
{
 +
cout<<m_dpp[i][j]<<" ";
 +
}
 +
}
 +
}
 +
else
 +
{
 +
for(i = 0;i < m_rows;i++)
 +
{
 +
for(j = 0;j < m_columns;j++)
 +
{
 +
os<<m_dpp[i][j]<<"\n";
 +
}
 +
}
 +
}
 +
}
 +
template <class T>
 +
istream& operator>>(istream& is, MyMatrix<T>& m)
 +
{
 +
m.Read(is);
 +
return is;
 +
}
 +
template <class T>
 +
ostream& operator<<(ostream& os, const MyMatrix<T>& m)
 +
{
 +
m.Write(os);
 +
return os;
 +
}
 +
 +
</pre>
 +
==Main.cpp==
 +
<pre>
 +
#include <iostream>
 +
#include <string>
 +
#include <cmath>
 +
using namespace std;
 +
 +
template <class T>
 +
class MyMatrix
 +
{
 +
public:
 +
MyMatrix();
 +
MyMatrix(const MyMatrix<T>& m);
 +
MyMatrix(int rows, int columns);
 +
~MyMatrix();
 +
MyMatrix operator+(const MyMatrix<T>& m2) const;
 +
MyMatrix operator-(const MyMatrix<T>& m2) const;
 +
MyMatrix operator=(const MyMatrix<T>& m);
 +
T* operator[](int i);
 +
T* operator[](int i) const;
 +
T getDeterminant(MyMatrix<T>& m);
 +
void Read(istream& is);
 +
void Write(ostream& os) const;
 +
private:
 +
T **m_dpp;
 +
int m_rows;
 +
int m_columns;
 +
};
 +
template <class T>
 +
istream& operator>>(istream& is, MyMatrix<T>& m);
 +
template <class T>
 +
ostream& operator<<(ostream& os, const MyMatrix<T>& m);
 +
void main()
 +
{
 +
 +
MyMatrix<int>m1;
 +
MyMatrix<double>m2;
 +
MyMatrix<float>m3;
 +
m1.getDeterminant(m1);
 +
 +
cout<<"Before entering data in to MyMatrix<int>m1.\n"<<flush;
 +
cin>>m1;
 +
cout<<"\nPrinting out m1.\n"<<flush;
 +
cout<<m1;
 +
MyMatrix<int>m4(m1);
 +
cout<<"\nAfter m4(m1).\n"<<flush;
 +
cout<<m4;
 +
cout<<"\nBefore entering data in to MyMatrix<double>m2.\n"<<flush;
 +
cin>>m2;
 +
cout<<"\nPrinting out m2.\n"<<flush;
 +
cout<<m2;
 +
cout<<"\nBefore entering data in to MyMatrix<float>m3.\n"<<flush;
 +
cin>>m3;
 +
cout<<"\nPrinting out m3.\n"<<flush;
 +
cout<<m3;
 +
MyMatrix<float>m6 = m3;
 +
cout<<"\nAfter m6 = m3.\n"<<flush;
 +
cout<<m6;
 +
cout<<"\nEnd of program reached.\n"<<flush;
 +
cin.get();
 +
}
 +
 +
template <class T>
 +
T MyMatrix<T>::getDeterminant(MyMatrix<T>& m)
 +
{
 +
int r, c;
 +
r = m.m_rows;
 +
c = m.m_columns;
 +
 +
T determinant/* = 0.0*/;
 +
 +
if(r == c)
 +
{
 +
if(r < 1)
 +
{
 +
cout<<"Less than one row and/or column";
 +
exit(1);
 +
}
 +
else if(r == 1)
 +
{
 +
return m[0][0];
 +
}
 +
else if(r == 2)
 +
{
 +
//cout<<"The determinant is: "<<((m[0][0]*m[1][1]) - (m[0][1]*m[1][0]));
 +
determinant = ((m[0][0]*m[1][1]) - (m[1][0]*m[0][1]));
 +
return determinant;
 +
}
 +
else
 +
{
 +
for(int columns = 0; columns < m.m_columns; columns++)
 +
{
 +
MyMatrix m2((m.m_rows-1),(m.m_columns-1));
 +
 +
for(int subRows = 1; subRows < m.m_rows; subRows++)
 +
{
 +
int subColumns = 0;
 +
 +
for(int i = 0; i < m.m_columns; i++)
 +
{
 +
if(i != columns)
 +
{
 +
m2[(subRows-1)][subColumns] = m[subRows][i];
 +
subColumns++;
 +
}
 +
}
 +
}
 +
determinant = determinant + ((m[0][columns])*(pow(float(-1),float(columns)))*(m2.getDeterminant(m2)));
 +
}
 +
}
 +
return determinant;
 +
}
 +
else
 +
{
 +
cout<<"Error, the matrix is uneven, cannot determine the matrix, exiting program.";
 +
exit(1);
 +
}
 +
}
 +
template <class T>
 +
MyMatrix<T>::MyMatrix()
 +
{
 +
m_rows = 0;
 +
m_columns = 0;
 +
m_dpp = NULL;
 +
}
 +
template <class T>
 +
MyMatrix<T>::MyMatrix(int rows, int columns)
 +
{
 +
int i, j;
 +
T *dp;
 +
 +
this->m_rows = rows;
 +
this->m_columns = columns;
 +
dp = new T[m_rows*m_columns];
 +
m_dpp = new T*[m_rows];
 +
 +
for(i = 0;i < m_rows;i++)
 +
m_dpp[i] = dp+(i * m_columns);
 +
for(i = 0;i < m_rows;i++)
 +
for(j = 0;j < m_columns;j++)
 +
m_dpp[i][j] = 0;
 +
}
 +
template <class T>
 +
MyMatrix<T>::MyMatrix(const MyMatrix<T>& m)
 +
{
 +
int i, j;
 +
T *dp;
 +
 +
this->m_rows = m.m_rows;
 +
this->m_columns = m.m_columns;
 +
dp = new T[m_rows*m_columns];
 +
m_dpp = new T*[m_rows];
 +
 +
for(i = 0;i < m_rows;i++)
 +
m_dpp[i] = dp+(i * m_columns);
 +
for(i = 0;i < m_rows;i++)
 +
for(j = 0;j < m_columns;j++)
 +
m_dpp[i][j] = m.m_dpp[i][j];
 +
}
 +
template <class T>
 +
MyMatrix<T>::~MyMatrix()
 +
{
 +
if(m_dpp != NULL)
 +
{
 +
delete [] m_dpp[0];
 +
delete [] m_dpp;
 +
}
 +
}
 +
template <class T>
 +
MyMatrix<T> MyMatrix<T>::operator=(const MyMatrix<T>& m)
 +
{
 +
int i, j;
 +
 +
if(this != &m)
 +
{
 +
if((m.m_rows == m_rows) && (m.m_columns == m_columns))
 +
{
 +
for(i = 0;i < m_rows;i++)
 +
for(j = 0;j < m_columns;j++)
 +
m_dpp[i][j] = m.m_dpp[i][j];
 +
return *this;
 +
}
 +
if(m_dpp != NULL)
 +
{
 +
delete [] m_dpp[0];
 +
delete [] m_dpp;
 +
}
 +
 +
T* dp;
 +
this->m_rows = m.m_rows;
 +
this->m_columns = m.m_columns;
 +
dp = new T[m_rows*m_columns];
 +
m_dpp = new T*[m_rows];
 +
 +
for(i = 0;i < m_rows;i++)
 +
m_dpp[i] = dp+(i * m_columns);
 +
for(i = 0;i < m_rows;i++)
 +
for(j = 0;j < m_columns;j++)
 +
m_dpp[i][j] = m.m_dpp[i][j];
 +
}
 +
return *this;
 +
}
 +
template <class T>
 +
MyMatrix<T> MyMatrix<T>::operator+(const MyMatrix<T>& m2) const
 +
{
 +
int i, j;
 +
 +
if((m2.m_rows != m_rows) || (m2.m_columns != m_columns))
 +
{
 +
cout<<"Error, different rows and/or columns.\n"
 +
<<"Press enter to exit the program."<<flush;
 +
cin.get();
 +
cin.get();
 +
exit(1);
 +
}
 +
MyMatrix sum(m2.m_rows,m2.m_columns);
 +
for(i = 0;i < sum.m_rows;i++)
 +
for(j = 0;j < sum.m_columns;j++)
 +
sum.m_dpp[i][j] = m_dpp[i][j] + m2.m_dpp[i][j];
 +
return sum;
 +
}
 +
template <class T>
 +
MyMatrix<T> MyMatrix<T>::operator-(const MyMatrix<T>& m2) const
 +
{
 +
int i, j;
 +
 +
if((m2.m_rows != m_rows) || (m2.m_columns != m_columns))
 +
{
 +
cout<<"Error, different rows and/or columns.\n"
 +
<<"Press enter to exit the program."<<flush;
 +
cin.get();
 +
cin.get();
 +
exit(1);
 +
}
 +
MyMatrix difference(m2.m_rows,m2.m_columns);
 +
for(i = 0;i < m2.m_rows;i++)
 +
for(j = 0;j < m2.m_columns;j++)
 +
difference.m_dpp[i][j] = m_dpp[i][j] - m2.m_dpp[i][j];
 +
return difference;
 +
}
 +
template <class T>
 +
T* MyMatrix<T>::operator[](int i)
 +
{
 +
if(i >= 0 && i < m_rows)
 +
return m_dpp[i];
 +
else
 +
{
 +
cout<<"Error, Out of Range. Press enter to exit the program."<<flush;
 +
cin.get();
 +
cin.get();
 +
exit(1);
 +
}
 +
}
 +
template <class T>
 +
T* MyMatrix<T>::operator[](int i) const
 +
{
 +
if(i >= 0 && i < m_rows)
 +
return m_dpp[i];
 +
else
 +
{
 +
cout<<"Error, Out of Range. Press enter to exit the program."<<flush;
 +
cin.get();
 +
cin.get();
 +
exit(1);
 +
}
 +
}
 +
template <class T>
 +
void MyMatrix<T>::Read(istream& is)
 +
{
 +
int i, j;
 +
int rows, columns;
 +
 +
if(is == cin)
 +
{
 +
cout<<"Enter the number of rows: "<<flush;
 +
cin>>rows;
 +
cout<<"Enter the number of columns: "<<flush;
 +
cin>>columns;
 +
 +
if((rows == m_rows) && (columns == m_columns))
 +
{
 +
for(i = 0;i < m_rows;i++)
 +
{
 +
for(j = 0;j < m_columns;j++)
 +
{
 +
cout<<"m["<<i<<"]"<<"["<<j<<"] = "<<flush;
 +
cin>>m_dpp[i][j];
 +
}
 +
}
 +
}
 +
else
 +
{
 +
if(m_dpp != NULL)
 +
{
 +
delete [] m_dpp[0];
 +
delete [] m_dpp;
 +
}
 +
 +
m_rows = rows;
 +
m_columns = columns;
 +
 +
T *dp;
 +
dp = new T[m_rows*m_columns];
 +
m_dpp = new T*[m_rows];
 +
 +
for(i = 0;i < m_rows;i++)
 +
m_dpp[i] = dp + (i * m_columns);
 +
for(i = 0;i < m_rows;i++)
 +
{
 +
for(j = 0;j < m_columns;j++)
 +
{
 +
cout<<"m["<<i<<"]"<<"["<<j<<"] = "<<flush;
 +
cin>>m_dpp[i][j];
 +
}
 +
}
 +
}
 +
}
 +
else
 +
{
 +
is>>m_rows;
 +
is>>m_columns;
 +
 +
if(m_dpp != NULL)
 +
{
 +
delete [] m_dpp[0];
 +
delete [] m_dpp;
 +
}
 +
 +
T *dp;
 +
dp = new T[m_rows*m_columns];
 +
m_dpp = new T*[m_rows];
 +
 +
for(i = 0;i < m_rows;i++)
 +
m_dpp[i] = dp + (i * m_columns);
 +
for(i = 0;i < m_rows;i++)
 +
{
 +
for(j = 0;j < m_columns;j++)
 +
is>>m_dpp[i][j];
 +
}
 +
}
 +
}
 +
template <class T>
 +
void MyMatrix<T>::Write(ostream& os) const
 +
{
 +
int i, j;
 +
 +
if(os == cout)
 +
{
 +
if((m_rows == 0) || (m_columns == 0))
 +
cout<<"The matrix is empty.\n";
 +
 +
for(i = 0;i < m_rows;i++)
 +
{
 +
if(i > 0)
 +
cout<<"\n\n";
 +
 +
for(j = 0;j < m_columns;j++)
 +
{
 +
cout<<m_dpp[i][j]<<" ";
 +
}
 +
cout<<"\n";
 +
}
 +
}
 +
else
 +
{
 +
for(i = 0;i < m_rows;i++)
 +
{
 +
for(j = 0;j < m_columns;j++)
 +
{
 +
os<<m_dpp[i][j]<<"\n";
 +
}
 +
}
 +
}
 +
}
 +
template <class T>
 +
istream& operator>>(istream& is, MyMatrix<T>& m)
 +
{
 +
m.Read(is);
 +
return is;
 +
}
 +
template <class T>
 +
ostream& operator<<(ostream& os, const MyMatrix<T>& m)
 +
{
 +
m.Write(os);
 +
return os;
 
}
 
}
 
</pre>
 
</pre>
123

edits

Navigation menu