Directory:Derek Elder/Programs/MyMatrix
MyWikiBiz, Author Your Legacy — Wednesday November 27, 2024
< Directory:Derek Elder | Programs(Redirected from Directory:Derek Elder/Programs/MyMtarix)
Jump to navigationJump to searchMyMatrix.h
#pragma once #include <iostream> using namespace std; class MyMatrix { public: MyMatrix(); MyMatrix(const MyMatrix& m); MyMatrix(int rows, int columns); ~MyMatrix(); MyMatrix operator+(const MyMatrix& m2) const; MyMatrix operator-(const MyMatrix& m2) const; MyMatrix operator=(const MyMatrix& m); double* operator[](int i); double* operator[](int i) const; double getDeterminant(MyMatrix& m); void Read(istream& is); void Write(ostream& os) const; private: double **m_dpp; int m_rows; int m_columns; }; istream& operator>>(istream& is, MyMatrix& m); ostream& operator<<(ostream& os, const MyMatrix& m);
MyMatrix.cpp
#include <iostream> #include <cmath> #include "MyMatrix.h" using namespace std; double MyMatrix::getDeterminant(MyMatrix& 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); } } MyMatrix::MyMatrix() { m_rows = 0; m_columns = 0; m_dpp = NULL; } MyMatrix::MyMatrix(int rows, int columns) { int i, j; double *dp; this->m_rows = rows; this->m_columns = columns; dp = new double[m_rows*m_columns]; m_dpp = new double*[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; } MyMatrix::MyMatrix(const MyMatrix& m) { int i, j; double *dp; this->m_rows = m.m_rows; this->m_columns = m.m_columns; dp = new double[m_rows*m_columns]; //Real -> m_rows*m_columns != 0 m_dpp = new double*[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]; } MyMatrix::~MyMatrix() { if(m_dpp != NULL) { delete [] m_dpp[0]; delete [] m_dpp; } } MyMatrix MyMatrix::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; } double* dp; this->m_rows = m.m_rows; this->m_columns = m.m_columns; dp = new double[m_rows*m_columns]; m_dpp = new double*[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; } double* MyMatrix::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); } } double* MyMatrix::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); } } MyMatrix MyMatrix::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; } MyMatrix MyMatrix::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; } void MyMatrix::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; double *dp; dp = new double[m_rows*m_columns]; m_dpp = new double*[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; } double *dp; dp = new double[m_rows*m_columns]; m_dpp = new double*[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]; } } } 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"; } } } } istream& operator>>(istream& is, MyMatrix& m) { m.Read(is); return is; } ostream& operator<<(ostream& os, const MyMatrix& m) { m.Write(os); return os; }
Main.cpp
#include <iostream> #include <string> #include "MyMatrix.h" using namespace std; void main() { int rows,i,j; double cr; //MyMatrix m1, m2(2,3); cout<<"How many equations would you like to solve? "<<flush; cin>>rows; MyMatrix m3(rows,rows); //Input matrix MyMatrix m4(rows,1); //Answer matrix for(i = 0;i < rows;i++) { for(j = 0;j < rows;j++) { cout<<"Enter the coefficent of row "<<(i + 1) <<", column "<<(j + 1)<<": "; cin>>m3[i][j]; } cout<<"Enter what the row equates to: "; cin>>m4[i][0]; } cout<<"The determinant is: "<<m3.getDeterminant(m3)<<"\n"; MyMatrix m5(m3); //D Matrix MyMatrix m6(1,rows); //Variable matrix for(j = 0; j < rows;j++) { cr = 0; for(i = 0;i < rows;i++) { m5[i][j] = m4[i][0]; } cr = ((m5.getDeterminant(m5))/(m3.getDeterminant(m3))); m5 = m3; m6[0][j] = cr; cout<<"cr"<<(j+1)<<" = "<<cr<<endl; } cout<<"Checking phase"<<'\n'; double var; for(i = 0;i < rows;i++) { var = 0; for(j = 0;j < rows;j++) { var += (m6[0][j] * m5[i][j]); cout<<m5[i][j]<<" * "<<m6[0][j]; if(j != rows - 1) { cout<<" + "; } else { cout<<" = "; } } cout<<var<<endl; } cout<<"\nHit enter to exit the program."<<endl; /*cout<<"After:\n" <<"MyMatrix m1, m2(2,3), m3;\n" <<"m1:\n"<<m1 <<"\nm2:\n"<<m2 <<"\nm3:\n"<<m3; cout<<"\n\nEnter the data for m3:\n"<<flush; cin>>m3; cout<<"\nThe matrix you entered follows:\n"<<m3; cout<<"\nHit enter to continue: "<<flush; cin.get(); cin.get(); cout<<"\nm3[0][0] = " << m3[0][0] << '\n'; m3[0][0] = 29.999; cout<<"\nAfter:\n m3[0][0] = 29.999;\nm3 follows:\n"<<m3; cout<<"\nHit enter to continue: "<<flush; cin.get(); cout<<"\n\nEnter the data for m2:\n"<<flush; cin>>m2; cout<<"The matrix you entered follows:\n"<<m2; cout<<"\nHit enter to continue: "<<flush; cin.get(); m1 = m2 + m3; cout<<"\n\nAfter:\n m1 = m2 + m3;\nm1 follows:\n"<<m1; cout<<"\nHit enter to continue: "<<flush; cin.get(); m1 = m2 - m3; cout<<"\n\nAfter:\n m1 = m2 - m3;\nm1 follows:\n"<<m1; cout<<"\nHit enter to continue: "<<flush; cin.get(); m1 = m3; cout<<"\n\nAfter:\n m1 = m3;\nm1 follows:\n"<<m1; cout<<"\nHit enter to continue: "<<flush; cin.get(); m1[0][0] = -999.876; cout<<"\n\nAfter:\n m1[0][0] = -999.876;\nm1 follows:\n"<<m1 <<"\n\nm3 follows:\n" <<m3<<"\n\nIs this what you expected?"; cout<<"\n\nHit enter to exit the program: "<<flush; cin.get();*/ }
With Templates
MyMatrix.h
#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; }
MyMatrix.cpp
#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; }
Main.cpp
#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; }