//cst 116 //dec 7, 2021 //Project 3 (Isabella Mon, Tyler Taormina) #include using namespace std; int const SIZE = 3; void FillMat (int size, float mat[SIZE][SIZE]); void PrintMat (int size, float mat[SIZE][SIZE]); void AddMat (int size, float mat1[SIZE][SIZE], float mat2[SIZE][SIZE], float matSum[SIZE][SIZE]); void TransMat (int size, float mat[SIZE][SIZE], float tranMat[SIZE][SIZE]); void MultMat (int size, float mat1[SIZE][SIZE], float mat2[SIZE][SIZE], float matMult[SIZE][SIZE], float tranMat[SIZE][SIZE]); void ClearBuffer(); void DisplayMenu(int& menu_choice); void ProcessChoice (int& flag, int menu_choice, int size, float mat1[SIZE][SIZE], float mat2[SIZE][SIZE], float matMult[SIZE][SIZE], float tranMat[SIZE][SIZE], float matSum[SIZE][SIZE]); int main() { float mat1 [SIZE][SIZE]; float mat2 [SIZE][SIZE]; float matSum [SIZE][SIZE]; float matMult [SIZE][SIZE]; float tranMat [SIZE][SIZE]; int menu_choice; int flag = 1; FillMat(SIZE, mat1); FillMat(SIZE, mat2); while(flag != 0) { DisplayMenu(menu_choice); ProcessChoice(flag, menu_choice, SIZE, mat1, mat2, matMult, tranMat, matSum); } cout << "Thanks for checking out the program!" << endl; return 0; } void AddMat(int size, float mat1[SIZE][SIZE], float mat2[SIZE][SIZE], float matSum[SIZE][SIZE]) { int row, col; float sum; for (row = 0; row < size; row++) { for (col = 0; col < size; col++) { sum = mat1[row][col] + mat2[row][col]; matSum[row][col] = sum; } } } void FillMat(int size, float mat[SIZE][SIZE]) { int row, col; float temp; cout << endl << endl; cout << "Lets build a 3x3 matrix..." << endl; cout << "We will fill in the matrix by row..." << endl; cout << "Input prompts will stop automatically once the matrix is full..." << endl; for (row = 0; row < size; row++) { for (col = 0; col < size; col++) { cout << "Please enter a number: "; cin >> temp; mat[row][col] = temp; } } } void PrintMat(int size, float mat[SIZE][SIZE]) { int row, col; for (row = 0; row < size; row++) { for (col = 0; col < size; col++) { cout << mat[row][col] << " "; } cout << endl; } } void TransMat (int size, float mat[SIZE][SIZE], float tranMat[SIZE][SIZE]) { int row, col; for (row = 0; row < size; row++) { for (col = 0; col < size; col++) { tranMat[row][col] = mat[col][row]; } } } void MultMat (int size, float mat1[SIZE][SIZE], float mat2[SIZE][SIZE], float matMult[SIZE][SIZE], float tranMat[SIZE][SIZE]) { int row, col; int i = 0; int k; float temp = 0; float total = 0; TransMat(size, mat2, tranMat); for (k = 0; k < size; k++) { for (row = 0; row < size; row++) { for (col = 0; col < size; col++) { temp = mat1[k][col] * tranMat[i][col]; total += temp; } matMult[k][i] = total; i++; total = 0; } i = 0; } } void DisplayMenu(int& menu_choice) { //Displays the menu of functions for the user to choose from. menu_choice = 0; cout << "==================================================================\n"; cout << " MENU" << endl; cout << "==================================================================\n"; cout << "1) Get New Matrices.\n"; cout << "2) Multiply Matrices.\n"; cout << "3) Add Matrices.\n"; cout << "4) Exit.\n"; cout << "Enter: "; cin >> menu_choice; if (menu_choice > 4 || menu_choice < 1) { cout << "Invalid Entry. Please enter a number from the options list provided.\n\n\n\n" << endl; DisplayMenu(menu_choice); } } void ProcessChoice (int& flag, int menu_choice, int size, float mat1[SIZE][SIZE], float mat2[SIZE][SIZE], float matMult[SIZE][SIZE], float tranMat[SIZE][SIZE], float matSum[SIZE][SIZE]) { //Takes in user input for menu choice and calls the appropriate function. string temp; int check = 1; ClearBuffer(); switch(menu_choice) { case 1: FillMat(size, mat1); FillMat(size, mat2); cout << "MATRIX ONE..." << endl; cout << "==================" << endl; PrintMat(size, mat1); cout << "==================" << endl; cout << endl << endl; cout << "MATRIX TWO..." << endl; cout << "==================" << endl; PrintMat(size, mat2); cout << "==================" << endl; break; case 2: MultMat(size, mat1, mat2, matMult, tranMat); cout << "MULTIPLIED MATRIX..." << endl; cout << "==================" << endl; PrintMat(size, matMult); cout << "==================" << endl; break; case 3: AddMat(size, mat1, mat2, matSum); cout << "ADDED MATRIX..." << endl; cout << "==================" << endl; PrintMat(size, matSum); cout << "==================" << endl; break; case 4: cout << "Are you sure you want to exit?" << endl; check = 0; break; default: break; } if (check == 0) { cout << "Enter 0 to end program.\n" << endl; cout << "Enter any other number to continue program.\n" << endl; cin >> flag; } } void ClearBuffer() { // clears buffer after menu choice so as not to interfere with the following user inputs. char c; do { c = getchar(); } while (c != '\n' && c != EOF); }