diff options
| author | BensProgramma <[email protected]> | 2021-11-30 14:51:57 -0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-11-30 14:51:57 -0800 |
| commit | 89705c43a93dacce69e22df389f5d9982a10f102 (patch) | |
| tree | 5eb2592659c2dede677ae04fb258aa4f2e9f7658 /Project3_Hernandez_Schroeder | |
| parent | Update Project_3_Hernandez_Schroeder.cpp (diff) | |
| download | project_3_hernandez_schroeder-89705c43a93dacce69e22df389f5d9982a10f102.tar.xz project_3_hernandez_schroeder-89705c43a93dacce69e22df389f5d9982a10f102.zip | |
Working Code
Diffstat (limited to 'Project3_Hernandez_Schroeder')
| -rw-r--r-- | Project3_Hernandez_Schroeder/Project_3_Hernandez_Schroeder.cpp | 139 |
1 files changed, 72 insertions, 67 deletions
diff --git a/Project3_Hernandez_Schroeder/Project_3_Hernandez_Schroeder.cpp b/Project3_Hernandez_Schroeder/Project_3_Hernandez_Schroeder.cpp index 882757b..686fcac 100644 --- a/Project3_Hernandez_Schroeder/Project_3_Hernandez_Schroeder.cpp +++ b/Project3_Hernandez_Schroeder/Project_3_Hernandez_Schroeder.cpp @@ -119,7 +119,6 @@ int main() //Ask for two matrices to do calculations upon getMatrices(); - DisplayMenu(); return 0; } @@ -139,22 +138,22 @@ void DisplayMenu() } switch (menuChoice) { - case 1: + case 1: // Get New Matrices { getMatrices(); break; } - case 2: + case 2: // Multiply Matrices { multiplyMatrices(MatrixA, MatrixB, MatrixC); break; } - case 3: + case 3: // Add Metrices { addMatrices(MatrixA, MatrixB, MatrixC); break; } - case 4: + case 4: // Exit the Program { cout << "\tThank You, Good Bye!!\n\n"; break; @@ -162,30 +161,31 @@ void DisplayMenu() } } -void getMatrices() -{ - - cout << endl << "Enter Elements for the 1st Matrix: " << endl; - for (i = 0; i < row; i++) // These two commands 'row' and 'col' are not really necessary they could be replaced with a '3' for this specific program (BAS) - for (j = 0; j < col; j++) - { - cout << "Row: " << i + 1 <<" Element "<< j + 1 << " : " << endl; - cin >> MatrixA[i][j]; - } +void getMatrices() +{ + cout << "\nEnter the elements of Matrix A (by row): \n"; + for (int i = 0; i < 3; i++) + { + cout << "row " << i + 1 << "\n"; // ask for elements in rows 1->3 + for (int j = 0; j < 3; j++) + { + cout << "element " << j + 1 << ": "; //iterate through the columns (per row) + cin >> MatrixA[i][j]; + } + } + cout << "\nEnter the Elements of Matrix B (by row):" << endl; + for (int i = 0; i < 3; i++) + { + cout << "row " << i + 1 << "\n"; // ask for elements in rows 1->3 + for (int j = 0; j < 3; j++) + { + cout << "element " << j + 1 << ": "; //iterate through the columns (per row) + cin >> MatrixB[i][j]; + } + } - cout << endl << "Enter Elements for the 2nd Matrix: " << endl; - for (i = 0; i < row; i++) - for (j = 0; j < col; j++) - { - cout << "Row " << i + 1 << " Element "<< j + 1 << " : " << endl; - cin >> MatrixB[i][j]; - } - - - - // It may also be a good thing if we were to also display the two completed matrices that the user input here...(BAS) - // Display the Matrices that were input + // Display the Matrices that were input cout << "\n\n Matrix A:\n"; for (int i = 0; i < 3; i++) { @@ -207,25 +207,28 @@ void getMatrices() cout << "\n\n"; DisplayMenu(); - } -void multiplyMatrices(float[3][3], float[3][3], float[3][3]) +void multiplyMatrices(float MatrixA[3][3], float MatrixB[3][3], float MatrixC[3][3]) { - //So ive been watching a couple videos on how to do matrix multipication and the rules for multiplication is a lot different from the rules of addition. - //Rows*Columns... when multipying the column has to be the same as the row and since we are using the 3x3 as const the rule doesnt really matter. - //The only thing im having trouble wrapping my head in terms of writing out the code how we are going to have to the Row multiply into each column. - //sum=AB - //row1 * col1 *col*2 *col3 - //row2 ..etc - //row3 ..etc + // Calculate A X B = C (9 equations (one for each element in Result Matrix)) + // Top row of C + MatrixC[0][0] = (MatrixA[0][0] * MatrixB[0][0]) + (MatrixA[0][1] * MatrixB[1][0]) + (MatrixA[0][2] * MatrixB[2][0]); + MatrixC[0][1] = (MatrixA[0][0] * MatrixB[0][1]) + (MatrixA[0][1] * MatrixB[1][1]) + (MatrixA[0][2] * MatrixB[2][1]); + MatrixC[0][2] = (MatrixA[0][0] * MatrixB[0][2]) + (MatrixA[0][1] * MatrixB[1][2]) + (MatrixA[0][2] * MatrixB[2][2]); + // 2nd row of C + MatrixC[1][0] = (MatrixA[1][0] * MatrixB[0][0]) + (MatrixA[1][1] * MatrixB[1][0]) + (MatrixA[1][2] * MatrixB[2][0]); + MatrixC[1][1] = (MatrixA[1][0] * MatrixB[0][1]) + (MatrixA[1][1] * MatrixB[1][1]) + (MatrixA[1][2] * MatrixB[2][1]); + MatrixC[1][2] = (MatrixA[1][0] * MatrixB[0][2]) + (MatrixA[1][1] * MatrixB[1][2]) + (MatrixA[1][2] * MatrixB[2][2]); + // 3rd row of C + MatrixC[2][0] = (MatrixA[2][0] * MatrixB[0][0]) + (MatrixA[2][1] * MatrixB[1][0]) + (MatrixA[2][2] * MatrixB[2][0]); + MatrixC[2][1] = (MatrixA[2][0] * MatrixB[0][1]) + (MatrixA[2][1] * MatrixB[1][1]) + (MatrixA[2][2] * MatrixB[2][1]); + MatrixC[2][2] = (MatrixA[2][0] * MatrixB[0][2]) + (MatrixA[2][1] * MatrixB[1][2]) + (MatrixA[2][2] * MatrixB[2][2]); + + - - - - // after the multiplication is done display the input matrices and the multiplication result - //print out results - // recall Matrix A + //print out results + // recall Matrix A cout << "\n\t MATRIX MULTIPLICATION\n Matrix A:\n"; for (int i = 0; i < 3; i++) { @@ -258,29 +261,30 @@ void multiplyMatrices(float[3][3], float[3][3], float[3][3]) cout << "\n\n"; DisplayMenu(); - } - - - -void addMatrices(float MatrixA[3][3], float MatrixB[3][3], float MatrixC[3][3]) +void addMatrices(float MatrixA[3][3], float MatrixB[3][3], float MatrixC[3][3]) { - for (int i = 0; i < row; i++) // Again these commands 'row' and 'col' can be replaced with '3' for this specific program (BAS) - for (int j = 0; j < col;j++) - MatrixC[i][j] = MatrixA[i][j] + MatrixB[i][j]; + for (int i = 0; i < 3; i++) + { + for (int j = 0; j < 3; j++) + { + MatrixC[i][j] = MatrixA[i][j] + MatrixB[i][j]; + } + } - - //print out results - // recall Matrix A - cout << "\n\t MATRIX ADDITION\n Matrix A:\n"; - for (int i = 0; i < 3; i++) + + + //print out results + // recall Matrix A + cout << "\n\t MATRIX ADDITION\n Matrix A:\n"; + for (int i = 0; i < 3; i++) { - for (int j = 0; j < 3; j++) + for (int j = 0; j < 3; j++) { - cout << "\t" << MatrixA[i][j] << "\t"; + cout << "\t" << MatrixA[i][j] << "\t"; } - cout << "\n"; + cout << "\n"; } //recall Matrix B cout << "\n Matrix B:\n"; @@ -292,15 +296,16 @@ void addMatrices(float MatrixA[3][3], float MatrixB[3][3], float MatrixC[3][3]) } cout << "\n"; } - cout << "The Sum: " << endl; //I was thinking is might be nice to also display the input matrices again before displaying the result (for clarity) (BAS) - cout << "\n A + B = C:\n"; - for (i = 0; i < row; i++) - for (j = 0; j < col; j++) - { - cout << MatrixC[i][j] << "\t"; - if (j == col - 1) - cout << endl; - } + //Display Result Matrix C (the sum) + cout << "\n A + B = C:\n"; + for (int i = 0; i < 3; i++) + { + for (int j = 0; j < 3; j++) + { + cout << "\t" << MatrixC[i][j] << "\t"; + } + cout << "\n"; + } cout << "\n\n"; DisplayMenu(); |