diff options
| -rw-r--r-- | CST116F2021-Proj3/Project_3_Functions.cpp | 251 |
1 files changed, 251 insertions, 0 deletions
diff --git a/CST116F2021-Proj3/Project_3_Functions.cpp b/CST116F2021-Proj3/Project_3_Functions.cpp new file mode 100644 index 0000000..64f749c --- /dev/null +++ b/CST116F2021-Proj3/Project_3_Functions.cpp @@ -0,0 +1,251 @@ +#include "Project_3_Voids.h" +#include <iostream> + +using namespace std; + +//This funtion will process the choice inputted from the user +void ProcessChoice(int& choice) +{ + cout << "\nIn This program, you are given the choice to either:\n\n1. Add a matrix\n\n2. Multiply a matrix\n\n3. Exit the program\n\nInput the corresponding number to make a choice\n\n"; + cin >> choice; + +} + +//This function is only used to record a matrix, but won't be used because the individual functions will ask for a matrix anyways. This function is only to record and display back, and not to use +void GetMatrix(int& rowM1, int& columnM1, int& rowM2, int& columnM2, int& iM, int& jM) +{ + int arrayM1[10][10]; + int arrayM2[10][10]; + int Matrixarray[10][10]; + + //Asking for how many rows and columns for the matrix. I know it's supposed to be a 3 x 3 but I made it to fit however many the user wants + cout << "Enter rows and columns for first matrix: "; + cin >> rowM1 >> columnM1; + cout << "\nEnter rows and columns for second matrix: "; + cin >> rowM2 >>columnM2; + + // If column of first matrix in not equal to row of second matrix, + // ask the user to enter the size of matrix again. + while (columnM1 != rowM2) + { + cout << "Error! column of first matrix not equal to row of second."; + + cout << "Enter rows and columns for first matrix: "; + cin >> rowM1 >> columnM1; + + cout << "Enter rows and columns for second matrix: "; + cin >> rowM2 >> columnM2; + } + + // Storing elements of first matrix. + cout << endl << "Enter elements of matrix 1:" << endl; + for (iM = 0; iM < rowM1; ++iM) + for (jM = 0; jM < columnM1; ++jM) + { + cout << "Enter element a" << iM + 1 << jM + 1 << " : "; + cin >> arrayM1[iM][jM]; + } + + // Storing elements of second matrix. + cout << endl << "Enter elements of matrix 2:" << endl; + for (iM = 0; iM < rowM2; ++iM) + for (jM = 0; jM < columnM2; ++jM) + { + cout << "Enter element b" << iM + 1 << jM + 1 << " : "; + cin >> arrayM2[iM][jM]; + } + + //Displaying the numbers of matrix a inputted by user + cout << endl << "Input numbers from array 1: \n" << endl; + for (iM = 0; iM < rowM1; ++iM) + for (jM = 0; jM < columnM2; ++jM) + { + cout << " " << arrayM1[iM][jM]; + if (jM == columnM2 - 1) + cout << endl; + } + + //Displaying the numbers of matrix b inputted by user + cout << endl << "Input numbers from array 2: \n" << endl; + for (iM = 0; iM < rowM1; ++iM) + for (jM = 0; jM < columnM2; ++jM) + { + cout << " " << arrayM2[iM][jM]; + if (jM == columnM2 - 1) + cout << endl; + } +} + +int AddMatrix(int& rowa1, int& columna1, int& rowa2, int& columna2, int& ia, int& ja, int& ka) +{ + int arraya1[10][10]; + int arraya2[10][10]; + int addedarray[10][10]; + + //Asking for how many rows and columns for the matrix. I know it's supposed to be a 3 x 3 but I made it to fit however many the user wants + cout << "Enter rows and columns for first matrix: "; + cin >> rowa1 >> columna1; + cout << "\nEnter rows and columns for second matrix: "; + cin >> rowa2 >> columna2; + + // If column of first matrix in not equal to row of second matrix, + // ask the user to enter the size of matrix again. + while (columna1 != rowa2) + { + cout << "Error! column of first matrix not equal to row of second."; + + cout << "Enter rows and columns for first matrix: "; + cin >> rowa1 >> columna1; + + cout << "Enter rows and columns for second matrix: "; + cin >> rowa2 >> columna2; + } + + // Storing elements of first matrix. + cout << endl << "Enter elements of matrix 1:" << endl; + for (ia = 0; ia < rowa1; ++ia) + for (ja = 0; ja < columna1; ++ja) + { + cout << "Enter element a" << ia + 1 << ja + 1 << " : "; + cin >> arraya1[ia][ja]; + } + + // Storing elements of second matrix. + cout << endl << "Enter elements of matrix 2:" << endl; + for (ia = 0; ia < rowa2; ++ia) + for (ja = 0; ja < columna2; ++ja) + { + cout << "Enter element b" << ia + 1 << ja + 1 << " : "; + cin >> arraya2[ia][ja]; + } + + // Adding matrix a and b and storing in array added. + for (ia = 0; ia < rowa1; ++ia) + for (ja = 0; ja < columna2; ++ja) + for (ka = 0; ka < columna1; ++ka) + { + addedarray[ia][ja] = arraya1[ia][ja] + arraya2[ia][ja]; + } + + //Displaying the numbers of matrix a inputted by user + cout << endl << "Input numbers from array 1: \n" << endl; + for (ia = 0; ia < rowa1; ++ia) + for (ja = 0; ja < columna2; ++ja) + { + cout << " " << arraya1[ia][ja]; + if (ja == columna2 - 1) + cout << endl; + } + + //Displaying the numbers of matrix b inputted by user + cout << endl << "Input numbers from array 2: \n" << endl; + for (ia = 0; ia < rowa1; ++ia) + for (ja = 0; ja < columna2; ++ja) + { + cout << " " << arraya2[ia][ja]; + if (ja == columna2 - 1) + cout << endl; + } + + // Displaying the addition of two matrix. + cout << endl << "Output Matrix: " << endl; + for (ia = 0; ia < rowa1; ++ia) + for (ja = 0; ja < columna2; ++ja) + { + cout << " " << addedarray[ia][ja]; + if (ja == columna2 - 1) + cout << endl; + } + + return 0; +} + +int MultiplyMatrix(int& row1,int& column1,int& row2,int& column2,int& i,int& j,int& k) +{ + int array1[10][10]; + int array2[10][10]; + int multipliedarray[10][10]; + + //Asking for how many rows and columns for the matrix. I know it's supposed to be a 3 x 3 but I made it to fit however many the user wants + cout << "Enter rows and columns for first matrix: "; + cin >> row1 >> column1; + cout << "\nEnter rows and columns for second matrix: "; + cin >> row2 >> column2; + + // If column of first matrix in not equal to row of second matrix, + // ask the user to enter the size of matrix again. + while (column1 != row2) + { + cout << "Error! column of first matrix not equal to row of second."; + + cout << "Enter rows and columns for first matrix: "; + cin >> row1 >> column1; + + cout << "Enter rows and columns for second matrix: "; + cin >> row2 >> column2; + } + + // Storing elements of first matrix. + cout << endl << "Enter elements of matrix 1:" << endl; + for (i = 0; i < row1; ++i) + for (j = 0; j < column1; ++j) + { + cout << "Enter element a" << i + 1 << j + 1 << " : "; + cin >> array1[i][j]; + } + + // Storing elements of second matrix. + cout << endl << "Enter elements of matrix 2:" << endl; + for (i = 0; i < row2; ++i) + for (j = 0; j < column2; ++j) + { + cout << "Enter element b" << i + 1 << j + 1 << " : "; + cin >> array2[i][j]; + } + + // Initializing elements of matrix mult to 0. + for (i = 0; i < row1; ++i) + for (j = 0; j < column2; ++j) + { + multipliedarray[i][j] = 0; + } + + // Multiplying matrix a and b and storing in array mult. + for (i = 0; i < row1; ++i) + for (j = 0; j < column2; ++j) + for (k = 0; k < column1; ++k) + { + multipliedarray[i][j] += array1[i][k] * array2[k][j]; + } + //Displaying the numbers of matrix a inputted by user + cout << endl << "Input numbers from array 1: \n" << endl; + for (i = 0; i < row1; ++i) + for (j = 0; j < column2; ++j) + { + cout << " " << array1[i][j]; + if (j == column2 - 1) + cout << endl; + } + + //Displaying the numbers of matrix b inputted by user + cout << endl << "Input numbers from array 2: \n" << endl; + for (i = 0; i < row1; ++i) + for (j = 0; j < column2; ++j) + { + cout << " " << array2[i][j]; + if (j == column2 - 1) + cout << endl; + } + + // Displaying the multiplication of two matrix. + cout << endl << "Output Matrix: " << endl; + for (i = 0; i < row1; ++i) + for (j = 0; j < column2; ++j) + { + cout << " " << multipliedarray[i][j]; + if (j == column2 - 1) + cout << endl; + } + + return 0; +} |