// project3.cpp : This file contains the 'main' function. Program execution begins and ends there. // //Written by Jacob Knox: Jacob.Knox@oit.edu #include #include #include #include "project3.h" using namespace std; int main() { float m1[MATRIX_SIZE][MATRIX_SIZE], m2[MATRIX_SIZE][MATRIX_SIZE]; int choice = 0; //make sure you have matricies before giving options getMatricies(m1, m2); menu(choice, m1, m2); } void menu(int choice, float m1[MATRIX_SIZE][MATRIX_SIZE], float m2[MATRIX_SIZE][MATRIX_SIZE]) { string menu = "\t\t--Matirx Prorgam--\n\ 1. Input new matricies\n\ 2. Multiply matricies\n\ 3. Add matricies\n\ 4. Exit Program\n\n\ Input your choice: "; //choice == 4 means quit and retturn to main //main program loop while (choice != 4) { choice = readInt(menu, "Error please input an integer 1-4.\n"); switch (choice) { case 1: getMatricies(m1, m2); break; case 2: multiplyMatrix(m1, m2); break; case 3: addMatrix(m1, m2); break; case 4: cout << "Exiting program..."; break; default: cout << "Error unknown choice " << choice << ". Please try again."; break; } } } //reads two matricies from user void getMatricies(float m1[MATRIX_SIZE][MATRIX_SIZE], float m2[MATRIX_SIZE][MATRIX_SIZE]) { cout << "Input the values for the first matrix:\n"; getMatrix(m1); cout << "Input the values for the second matrix:\n"; getMatrix(m2); } // reads all the values for a single matrix void getMatrix(float mat[MATRIX_SIZE][MATRIX_SIZE]) { for (int x = 0; x < MATRIX_SIZE; x++) { for (int y = 0; y < MATRIX_SIZE; y++) { mat[x][y] = readFloat("Input the matrix value at " + to_string(x+1) + ", " + to_string(y+1) + ": ", "Error invalid value. Please input a float.\n"); } } } // adds matrix 1 and 2 and shows result void addMatrix(float m1[MATRIX_SIZE][MATRIX_SIZE], float m2[MATRIX_SIZE][MATRIX_SIZE]) { float result[MATRIX_SIZE][MATRIX_SIZE]; for (int x = 0; x < MATRIX_SIZE; x++) { for (int y = 0; y < MATRIX_SIZE; y++) { //adding matrix is adding each spot to corresponding spot in result result[x][y] = m1[x][y] + m2[x][y]; } } cout << "The result of adding\n"; printMatrix(m1); cout << "with\n"; printMatrix(m2); cout << "is:\n"; printMatrix(result); } //multiplies thw two matrices void multiplyMatrix(float m1[MATRIX_SIZE][MATRIX_SIZE], float m2[MATRIX_SIZE][MATRIX_SIZE]) { float result[MATRIX_SIZE][MATRIX_SIZE]; for (int x = 0; x < MATRIX_SIZE; x++) { for (int y = 0; y < MATRIX_SIZE; y++) { //result for a specific spot is the dot product of that spot in the two matrices result[x][y] = calulateDotProduct(x, y, m1, m2); } } cout << "The result of multiplying\n"; printMatrix(m1); cout << "with\n"; printMatrix(m2); cout << "is:\n"; printMatrix(result); } //dot product is multiplying the corresponding values in one matrix collunm/ row and adding the results of that float calulateDotProduct(int x, int y, float m1[MATRIX_SIZE][MATRIX_SIZE], float m2[MATRIX_SIZE][MATRIX_SIZE]) { float product = 0; for (int i = 0; i < MATRIX_SIZE; i++) { product += m1[x][i]*m2[i][y]; } return product; } //reads an int from the user, will output error if it fails and repeat asking until it gets a good answer int readInt(const std::string& question, const std::string& error) { int value; cout << question; while (!(cin >> value)) { cin.clear(); while (cin.get() != '\n') continue; cout << error; cout << question; } return value; } //reads an float from the user, will output error if it fails and repeat asking until it gets a good answer float readFloat(const std::string& question, const std::string& error) { float value; cout << question; while (!(cin >> value)) { cin.clear(); while (cin.get() != '\n') continue; cout << error; cout << question; } return value; } //prints a matrix by looping over it and formatting appropiately void printMatrix(float mat[MATRIX_SIZE][MATRIX_SIZE]) { for (int x = 0; x < MATRIX_SIZE; x++) { for (int y = 0; y < MATRIX_SIZE; y++) { cout << setprecision(3) << fixed << "[" << mat[x][y] << "]"; } cout << endl; } }