Created
October 5, 2018 18:25
-
-
Save Abdulazizsayed/1e35af9e966daf7405cc904d30279c7c to your computer and use it in GitHub Desktop.
Matrix manipulation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <iomanip> | |
#include <cassert> | |
using namespace std; | |
// A structure to store a matrix | |
struct matrix | |
{ | |
int** data; // Pointer to 2-D array that will simulate matrix | |
int row, col; | |
}; | |
// Already implemented | |
void createMatrix (int row, int col, int num[], matrix& mat); | |
// ABDALLAH FUNCTIONS | |
// All these operations return a new matrix with the calculation result | |
matrix operator+ (matrix mat1, matrix mat2); // Add if same dimensions | |
matrix operator- (matrix mat1, matrix mat2); // Sub if same dimensions | |
matrix operator* (matrix mat1, matrix mat2); // Multi if col1 == row2 | |
matrix operator+ (matrix mat1, int scalar); // Add a scalar | |
matrix operator- (matrix mat1, int scalar); // Subtract a scalar | |
matrix operator* (matrix mat1, int scalar); // Multiple by scalar | |
// ABDULAZIZ FUNCTIONS | |
// The last operator >> takes an istream and a matrix and return the | |
// the same istream so it is possible to cascade input like | |
// cin >> matrix1 >> matrix2 << endl; | |
matrix operator+= (matrix& mat1, matrix mat2); // mat1 changes & return | |
// new matrix with the sum | |
matrix operator-= (matrix& mat1, matrix mat2); // mat1 changes + return new | |
// matrix with difference | |
matrix operator+= (matrix& mat, int scalar); // change mat & return new matrix | |
matrix operator-= (matrix& mat, int scalar); // change mat & return new matrix | |
void operator++ (matrix& mat); // Add 1 to each element ++mat | |
void operator-- (matrix& mat); // Sub 1 from each element --mat | |
istream& operator>> (istream& in, matrix& mat); | |
// Input matrix like this (dim 2 x 3) cin >> 2 3 4 6 8 9 12 123 | |
// and return istream to allow cascading input | |
// TAHA FUNCTIONS | |
ostream& operator<< (ostream& out, matrix& mat); | |
// Print matrix as follows (2 x 3) 4 6 8 | |
// and return ostream to cascade printing 9 12 123 | |
bool operator== (matrix mat1, matrix mat2); // True if identical | |
bool operator!= (matrix mat1, matrix mat2); // True if not same | |
bool isSquare (matrix mat); // True if square matrix | |
bool isSymetric (matrix mat); // True if square and symmetric | |
bool isIdentity (matrix mat); // True if square and identity | |
matrix transpose (matrix& mat); // Return new matrix with the transpose | |
//__________________________________________ | |
int main(){ | |
matrix mat1, mat2, mat3; | |
int scalar, num; | |
cout << "Enter the number of the process you want to do on matrices.\n"; | |
cout << "1- Add tow matrices in a new matrix.\n" | |
<< "2- Subtract tow matrices in a new matrix.\n" | |
<< "3- Multiply tow matrices in a new matrix.\n" | |
<< "4- Add a scalar.\n" | |
<< "5- Subtract a scalar.\n" | |
<< "6- Multiply a scalar.\n" | |
<< "7- Add tow matrices in a matrix1.\n" | |
<< "8- Subtract tow matrices in matrix1.\n" | |
<< "9- Add a scalar to matrix1.\n" | |
<< "10-Subtract a scalar form matrix1.\n" | |
<< "11- ++mat.\n" | |
<< "12- --mat.\n" | |
<< "13- Check if matrix1 = matrix2 or not.\n" | |
<< "14- Check if matrix1 is square.\n" | |
<< "15- Check if matrix1 is symmetric.\n" | |
<< "16- Check if matrix1 is identical.\n" | |
<< "17- Transpose matrix1.\n"; | |
cout << "Please, Choose:"; | |
cin >> num; | |
cout << "Please enter matrix1.\n"; | |
cin >> mat1; | |
if(num == 1){ | |
cout << "Please enter matrix2.\n"; | |
cin >> mat2; | |
mat3 = mat1 + mat2; | |
cout << mat3 << endl; | |
} | |
else if(num == 2){ | |
cout << "Please enter matrix2.\n"; | |
cin >> mat2; | |
mat3 = mat1 - mat2; | |
cout << mat3 << endl; | |
} | |
else if(num == 3){ | |
cout << "Please enter matrix2.\n"; | |
cin >> mat2; | |
mat3 = mat1 * mat2; | |
cout << mat3 << endl; | |
} | |
else if(num == 4){ | |
cout << "Please enter scalar.\n"; | |
cin >> scalar; | |
mat3 = mat1 + scalar; | |
cout << mat3 << endl; | |
} | |
else if(num == 5){ | |
cout << "Please enter scalar.\n"; | |
cin >> scalar; | |
mat3 = mat1 - scalar; | |
cout << mat3 << endl; | |
} | |
else if(num == 6){ | |
cout << "Please enter scalar.\n"; | |
cin >> scalar; | |
mat3 = mat1 * scalar; | |
cout << mat3 << endl; | |
} | |
else if(num == 7){ | |
cout << "Please enter matrix2.\n"; | |
cin >> mat2; | |
mat1 += mat2; | |
cout << mat1 << endl; | |
} | |
else if(num == 8){ | |
cout << "Please enter matrix2.\n"; | |
cin >> mat2; | |
mat1 -= mat2; | |
cout << mat1 << endl; | |
} | |
else if(num == 9){ | |
cout << "Please enter scalar.\n"; | |
cin >> scalar; | |
mat1 += scalar; | |
cout << mat1 << endl; | |
} | |
else if(num == 10){ | |
cout << "Please enter scalar.\n"; | |
cin >> scalar; | |
mat1 -= scalar; | |
cout << mat1 << endl; | |
} | |
else if(num == 11){ | |
++mat1; | |
cout << mat1 << endl; | |
} | |
else if(num == 12){ | |
--mat1; | |
cout << mat1 << endl; | |
} | |
else if(num == 13){ | |
cout << "Please enter matrix2.\n"; | |
cin >> mat2; | |
if(mat1 == mat2) | |
cout << "matrix1 = matrix2\n"; | |
else | |
cout << "matrix1 != matrix2\n"; | |
} | |
else if(num == 14){ | |
if(isSquare(mat1)) | |
cout << "matrix1 is square.\n"; | |
else | |
cout << "matrix1 isn't square.\n"; | |
} | |
else if(num == 15){ | |
if(isSymetric(mat1)) | |
cout << "matrix1 is symmetric.\n"; | |
else | |
cout << "matrix1 isn't symmetric.\n"; | |
} | |
else if(num == 16){ | |
if(isIdentity(mat1)) | |
cout << "matrix1 is identical.\n"; | |
else | |
cout << "matrix1 isn't identical.\n"; | |
} | |
else if(num == 17){ | |
mat3 = transpose(mat1); | |
cout << mat3; | |
} | |
return 0; | |
} | |
//__________________________________________ | |
// Takes an array of data and stores in matrix according | |
// to rows and columns | |
void createMatrix (int row, int col, int num[], matrix& mat) { | |
mat.row = row; | |
mat.col = col; | |
mat.data = new int* [row]; | |
for (int i = 0; i < row; i++) | |
mat.data[i] = new int [col]; | |
for (int i = 0; i < row; i++) | |
for (int j = 0; j < col; j++) | |
mat.data[i][j] = num[i * col + j]; | |
} | |
//************************************ | |
//NAGUIB'S TASK: | |
matrix operator+ (matrix mat1, matrix mat2){ | |
matrix resMat; | |
resMat.row=mat1.row; | |
resMat.col=mat1.col; | |
resMat.data=new int*[resMat.row]; | |
for (int i = 0; i < resMat.row; i++){ | |
resMat.data[i] = new int [resMat.col]; | |
} | |
for(int i=0 ; i<resMat.row ; i++){ | |
for(int j=0 ; j<resMat.col ; j++){ | |
resMat.data[i][j]=mat1.data[i][j]+mat2.data[i][j]; | |
} | |
} | |
return resMat; | |
} | |
matrix operator- (matrix mat1, matrix mat2){ | |
matrix resMat; | |
resMat.row=mat1.row; | |
resMat.col=mat1.col; | |
resMat.data=new int*[resMat.row]; | |
for (int i = 0; i < resMat.row; i++){ | |
resMat.data[i] = new int [resMat.col]; | |
} | |
for(int i=0 ; i<resMat.row ; i++){ | |
for(int j=0 ; j<resMat.col ; j++){ | |
resMat.data[i][j]=mat1.data[i][j]-mat2.data[i][j]; | |
} | |
} | |
return resMat; | |
} | |
matrix operator* (matrix mat1, matrix mat2){ | |
matrix resMat; | |
resMat.row=mat1.row; | |
resMat.col=mat2.col; | |
for(int i=0 ; i<resMat.row ; i++){ | |
for(int j=0 ; j<resMat.col ; j++){ | |
int sum=0; | |
int x=0; | |
/* | |
we need to multiply each row by each col and take their sum | |
so initialize var x to walk through all the cols with row (i) in mat1 | |
ato walk through all the rows in col (j) in mat2 | |
*/ | |
for(; x<mat1.col ; x++){//we assume that mat1.col = mat2.row | |
sum+=mat1.data[i][x] * mat2.data[x][j]; | |
} | |
resMat.data[i][j]=sum; | |
} | |
} | |
return resMat; | |
} | |
matrix operator+ (matrix mat1, int scalar){ | |
matrix resMat; | |
resMat.row=mat1.row; | |
resMat.col=mat1.col; | |
resMat.data = new int* [resMat.row]; | |
for (int i = 0; i < resMat.row; i++){ | |
resMat.data[i] = new int [resMat.col]; | |
} | |
for(int i=0 ; i<resMat.row ; i++){ | |
for(int j=0 ; j<resMat.col ; j++){ | |
resMat.data[i][j]=mat1.data[i][j]+scalar; | |
} | |
} | |
return resMat; | |
} | |
matrix operator- (matrix mat1, int scalar){ | |
matrix resMat; | |
resMat.row=mat1.row; | |
resMat.col=mat1.col; | |
resMat.data = new int* [resMat.row]; | |
for (int i = 0; i < resMat.row; i++){ | |
resMat.data[i] = new int [resMat.col]; | |
} | |
for(int i=0 ; i<resMat.row ; i++){ | |
for(int j=0 ; j<resMat.col ; j++){ | |
resMat.data[i][j]=mat1.data[i][j]-scalar; | |
} | |
} | |
return resMat; | |
} | |
matrix operator* (matrix mat1, int scalar){ | |
matrix resMat; | |
resMat.row=mat1.row; | |
resMat.col=mat1.col; | |
resMat.data = new int* [resMat.row]; | |
for (int i = 0; i < resMat.row; i++){ | |
resMat.data[i] = new int [resMat.col]; | |
} | |
for(int i=0 ; i<resMat.row ; i++){ | |
for(int j=0 ; j<resMat.col ; j++){ | |
resMat.data[i][j]=mat1.data[i][j]*scalar; | |
} | |
} | |
return resMat; | |
} | |
//*********************** | |
//ABDULAZIZ'S TASK: | |
void operator++ (matrix& mat){ | |
for(int i = 0 ; i < mat.row ; ++i){ | |
for(int j = 0 ; j < mat.col ; j++){ | |
mat.data[i][j] = mat.data[i][j] + 1; | |
} | |
} | |
} | |
void operator-- (matrix& mat){ | |
int row, col; | |
row = mat.row; | |
col = mat.col; | |
for(int i = 0 ; i < row ; ++i){ | |
for(int j = 0 ; j < col ; j++){ | |
mat.data[i][j] = mat.data[i][j] - 1; | |
} | |
} | |
} | |
matrix operator+= (matrix& mat, int scalar){ | |
int row, col; | |
row = mat.row; | |
col = mat.col; | |
for(int i = 0 ; i < row ; ++i){ | |
for(int j = 0 ; j < col ; j++){ | |
mat.data[i][j] = mat.data[i][j] + scalar; | |
} | |
} | |
} | |
matrix operator-= (matrix& mat, int scalar){ | |
int row, col; | |
row = mat.row; | |
col = mat.col; | |
for(int i = 0 ; i < row ; ++i){ | |
for(int j = 0 ; j < col ; j++){ | |
mat.data[i][j] = mat.data[i][j] - scalar; | |
} | |
} | |
} | |
matrix operator+= (matrix& mat1, matrix mat2){ | |
int row, col; | |
row = mat1.row; | |
col = mat1.col; | |
for(int i = 0 ; i < row ; ++i){ | |
for(int j = 0 ; j < col ; j++){ | |
mat1.data[i][j] = mat1.data[i][j] + mat2.data[i][j]; | |
} | |
} | |
return mat1; | |
} | |
matrix operator-= (matrix& mat1, matrix mat2){ | |
int row, col; | |
row = mat1.row; | |
col = mat1.col; | |
for(int i = 0 ; i < row ; ++i){ | |
for(int j = 0 ; j < col ; j++){ | |
mat1.data[i][j] = mat1.data[i][j] - mat2.data[i][j]; | |
} | |
} | |
return mat1; | |
} | |
istream& operator>> (istream& in, matrix& mat){ | |
int row,col; | |
cout << "Enter rows and cols" << endl; | |
in >> row >> col; | |
cout << "Enter the matrix "<<endl; | |
mat.row = row; | |
mat.col = col; | |
mat.data = new int*[row]; | |
for (int i = 0; i < row; i++) | |
mat.data[i] = new int [col]; | |
for(int i=0 ; i<row ; i++){ | |
for(int j=0 ; j<col ; j++){ | |
in >> mat.data[i][j]; | |
} | |
} | |
return in; | |
} | |
/*ostream& operator<< (ostream& out, matrix& mat){ | |
int row ,col; | |
row = mat.row; | |
col = mat.col; | |
for(int i=0 ; i<row ; i++){ | |
for(int j=0 ; j<col ; j++){ | |
out << mat.data[i][j] << " "; | |
} | |
out << endl; | |
} | |
return out; | |
}*/ | |
//********************************* | |
//TAHA'S TASK: | |
ostream& operator<< (ostream& out, matrix& mat){ | |
for(int i=0 ; i<mat.row ; i++){ | |
for(int j=0 ; j<mat.col ; j++){ | |
out << mat.data[i][j] << " "; | |
} | |
out << endl; | |
} | |
return out; | |
} | |
bool operator== (matrix mat1, matrix mat2){ | |
for ( int i=0; i<mat1.row ;i++){ | |
for(int j=0 ; j<mat1.col ; j++){ | |
if (mat1.data[i][j]!=mat2.data[i][j] ){ | |
return false; | |
} | |
} | |
} | |
return true; | |
} | |
bool operator!= (matrix mat1, matrix mat2){ | |
for ( int i=0; i<mat1.row ;i++){ | |
for(int j=0 ; j<mat1.col ; j++){ | |
if (mat1.data[i][j]!=mat2.data[i][j]) | |
return true; | |
} | |
} | |
return false; | |
} | |
bool isSquare (matrix mat){ | |
if (mat.row==mat.col) | |
return true; | |
else | |
return false; | |
} | |
bool isSymetric (matrix mat){ | |
int check=0; | |
for(int i=0 ; i<mat.row ; i++){ | |
for(int j=0 ; j<mat.col ; j++){ | |
if(i==j)continue; | |
else | |
if(mat.data[i][j]!=mat.data[j][i]) check+=1; | |
} | |
} | |
if(check==0) return true; | |
else return false; | |
} | |
bool isIdentity (matrix mat){ | |
int checkZero=0 , checkone=0; | |
for(int i=0 ; i<mat.row ; i++){ | |
for(int j=0 ; j<mat.col ; j++){ | |
if (i==j){ | |
if (mat.data[i][j]!=1) checkone+=1; | |
} | |
else | |
if(mat.data[i][j]!=0) checkZero+=1; | |
} | |
} | |
if(checkone==0 && checkZero==0) return true; | |
else return false; | |
} | |
matrix transpose(matrix& mat){ | |
matrix resMat; | |
resMat.row=mat.col; | |
resMat.col=mat.row; | |
resMat.data = new int* [resMat.row]; | |
for (int i = 0; i < resMat.row; i++){ | |
resMat.data[i] = new int [resMat.col]; | |
} | |
for(int i=0 ; i<resMat.row ; i++){ | |
for(int j=0 ; j<resMat.col ; j++){ | |
resMat.data[i][j]=mat.data[j][i]; | |
} | |
} | |
return resMat; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment