|
|
#include #include #include using namespace std; //Constant Declarations //--------------------------- const int MAXROWS = 5; const int MAXCOLS = 5; //Function Prototypes //------------------------- void Input(double a[][MAXCOLS], int& row, int& col, ifstream& fin); void Output(double a[][MAXCOLS], int row, int col, ofstream& fout); bool Equal(double a[][MAXCOLS], int aRow, int aCol, double b[][MAXCOLS], int bRow, int bCol); void Add(double a[][MAXCOLS], int aRow, int aCol, double b[][MAXCOLS], int bRow, int bCol, double sum[][MAXCOLS]); void Transpose(double a[][MAXCOLS], int row, int col, double trans[][MAXCOLS]); void Multiply(double a[][MAXCOLS], int aRow, int aCol, double b[][MAXCOLS], int bRow, int bCol, double product[][MAXCOLS]); char getOp(); int main() { //Variable List //---------------- char inFile1[25]; char inFile2[25]; char outFile[25]; ifstream fin1; ofstream fout; ifstream fin2; double array1[MAXROWS][MAXCOLS] = {{0}}; int a1_Rows = 0; int a1_Cols = 0; int a2_Rows = 0; int a2_Cols = 0; double array2[MAXROWS][MAXCOLS] = {{0}}; double product[MAXROWS][MAXCOLS] = {{0}}; double sum[MAXROWS][MAXCOLS] = {{0}}; double trans[MAXROWS][MAXCOLS] = {{0}}; char ans; bool t; cout << "Enter the name of the file that contains first set of data->"; cin >> inFile1; cout << endl; cout << "Enter the name of the file that contains the second set of data->"; cin >> inFile2; cout << endl; cout << "Enter the name of the output file that you would like to write to->"; cin >> outFile; cout << endl; fin1.open(inFile1); if(fin1.fail()) { cout << "Cannot open " << inFile1 << " program exiting" << endl; exit(1); } fin2.open(inFile2); if(fin2.fail()) { cout << "Cannot open " << inFile2 << " program exiting" << endl; exit(1); } fout.open(outFile); if(fout.fail()) { cout << "Cannot open " << outFile << " program exiting" << endl; exit(1); } Input(array1, a1_Rows, a1_Cols, fin1); cout << "Array 1 Input [DONE]" << endl; Input(array2, a2_Rows, a2_Cols, fin2); cout << "Array 2 Input [DONE]" << endl; cout << endl; ans = getOp(); while (ans != 'q') { switch(ans) { case '1': Multiply(array1, a1_Rows, a1_Cols, array2, a2_Rows, a2_Cols, product); Output(array1, a1_Rows, a1_Cols, fout); fout << " * "; fout << endl; Output(array2, a2_Rows, a2_Cols, fout); fout << " = "; fout << endl; Output(product, a1_Rows, a2_Cols, fout); fout << "-------------------------------------------- "; cout << "Outputted to File [DONE]" << endl; ans = getOp(); break; case '2': Add(array1, a1_Rows, a1_Cols, array2, a2_Rows, a2_Cols, sum); Output(array1, a1_Rows, a1_Cols, fout); fout << " + "; fout << endl; Output(array2, a2_Rows, a2_Cols, fout); fout << " = "; fout << endl; Output(sum, a1_Rows, a2_Cols, fout); cout << "Outputted to File [DONE]" << endl; fout << "-------------------------------------------- "; ans = getOp(); break; case '3': Transpose(array1, a1_Rows, a1_Cols, trans); Output(array1, a1_Rows, a1_Cols, fout); fout << " T' = "; fout << endl; Output(trans, a1_Rows, a1_Cols, fout); cout << "Outputted to File [DONE]" << endl; fout << "-------------------------------------------- "; ans = getOp(); break; case '4': Transpose(array2, a2_Rows, a2_Cols, trans); Output(array1, a1_Rows, a1_Cols, fout); fout << " T' = "; fout << endl; Output(trans, a1_Rows, a1_Cols, fout); cout << "Outputted to File [DONE]" << endl; fout << "-------------------------------------------- "; ans = getOp(); break; case '5': Output(array1, a1_Rows, a1_Cols, fout); fout << " Equals "; fout << endl; Output(array2, a2_Rows, a2_Cols, fout); t = Equal(array1, a1_Rows, a1_Cols, array2, a2_Rows, a2_Cols); if (t == 1) fout << "True" << endl; else fout << "False" << endl; fout << endl; cout << "Outputted to File [DONE]" << endl; fout << "-------------------------------------------- "; ans = getOp(); break; default: cout << "Incorrect input, plase try again->"; cin >> ans; cout << endl; } } //Closing All Input/Output Files //------------------------------------- fin1.close(); fin2.close(); fout.close(); return(0); } void Input(double a[][MAXCOLS], int& row, int& col, ifstream& fin) { int tempRow, tempCol; double tempVal; fin >> tempRow; fin >> tempCol; for(int i = 0; i < tempRow; i++) { for(int j = 0; j < tempCol; j++) { fin >> tempVal; a[i][j] = tempVal; } } row = tempRow; col = tempCol; } void Output(double a[][MAXCOLS], int row, int col, ofstream& fout) { int count = 0; for(int r = 0; r < row; r++) { for(int c = 0; c < col; c++) { if(a[r][c] == 0) count++; } } if (count != (row * col)) { for(int i = 0; i < row; i++) { for(int j = 0; j < col; j++) { fout << a[i][j] << " "; } fout << endl; } fout << endl; } else fout << " Could Not Be Solved" << endl; } bool Equal(double a[][MAXCOLS], int aRow, int aCol, double b[][MAXCOLS], int bRow, int bCol) { bool equalityFlag = true; if( (aRow != bRow) || (aCol != bCol) ) equalityFlag = false; else { for(int i = 0; i < aRow ; i++) { for(int j = 0; j < aCol; j++) { if(a[i][j] != b[i][j]) equalityFlag = false; } } } return(equalityFlag); } void Add(double a[][MAXCOLS], int aRow, int aCol, double b[][MAXCOLS], int bRow, int bCol, double sum[][MAXCOLS]) { if ( (aRow == bRow) && (aCol == bCol)) { for(int i = 0; i < aRow; i++) { for(int j = 0; j < aCol; j++) { sum[i][j] = a[i][j] + b[i][j]; } } } else { cout << "*************************************************************" << endl; cout << "* CANNOT ADD THESE MATRICES, DIMENSIONS DON'T MATCH *" << endl; cout << "*************************************************************" << endl; } } void Transpose(double a[][MAXCOLS], int row, int col, double trans[][MAXCOLS]) { if(row == col) { for(int i = 0; i < row; i++) { for(int j = 0; j < col; j++) { trans[i][j] = a[j][i]; } } } else { cout << "*****************************************************************" << endl; cout << "* CANNOT TRANSPOSE, INDICES NEED TO FORM A PERFECT SQUARE*" << endl; cout << "*****************************************************************" << endl; } } void Multiply(double a[][MAXCOLS], int aRow, int aCol, double b[][MAXCOLS], int bRow, int bCol, double product[][MAXCOLS]) { double sum = 0; if( (aRow == bCol) && (bRow == aCol) ) { for(int row = 0; row < aRow; row++) { for(int col = 0; col < bRow; col ++) { sum = 0; for(int i = 0; i < aCol; i++) { sum = sum + (a[row][i] * b[i][col]); } product[row][col] = sum; } } } else{ cout << "*****************************************************" << endl; cout << "* CANNOT MULTIPLY, INDICES ARE NOT CONSISTENT *" << endl; cout << "*****************************************************" << endl; } } char getOp() { char ans; cout << " MATRIX OPERATIONS" << endl; cout << " ------------------------------" << endl; cout << "1.
|