#include using namespace std; // This assignment is worth 5 points int main() { int x0, y0, x1, y1; int ** mat0 = NULL; // Set up all three 2D arrays int ** mat1 = NULL; int ** result = NULL; do // Loop to input and check the input matrix dimensions { cout << "Enter rows and columns for first matrix: "; cin >> x0 >> y0; cout << "Enter rows and columns for second matrix: "; cin >> x1 >> y1; if(y0 != x1) cout << "Incompatible matrices, use " << "different dimensions. " << endl; } while(y0 != x1); // Column count of first matrix must match row count of second matrix mat0 = new int * [x0]; // Establish all matrices using Method 3 mat1 = new int * [x1]; result = new int * [x0]; for(int i = 0; i < x0; i++) { mat0[i] = new int [y0]; result[i] = new int [y1]; } for(int i = 0; i < x1; i++) mat1[i] = new int [y1]; cout << "Enter 1st array values, press Enter to end each row:" << endl; int i = 0, j = 0; char ch; string temp; do // Input values to first input array { j = 0; do { cin >> mat0[i][j]; j++; ch = cin.get(); } while(j < y0 && ch != '\n'); if(ch != '\n' && j == y0) getline(cin, temp); else if(ch == '\n' && j < y0 - 1) for(j; j < y0; j++) // Post-populate with 0s mat0[i][j] = 0; i++; } while(i < x0); cout << "Enter 2nd array values, press Enter to end each row:" << endl; i = 0; do // Input values to second input array { j = 0; do { cin >> mat1[i][j]; j++; ch = cin.get(); } while(j < y1 && ch != '\n'); if(ch != '\n' && j == y1) getline(cin, temp); else if(ch == '\n' && j < y1) for(j; j < y1; j++) // Post-populate with 0s mat1[i][j] = 0; i++; } while(i < x1); cout << "Multiplication Output:" << endl; for(int i = 0; i < x0; i++) // Calculate result and print { for(int j = 0; j < y1; j++) { result[i][j] = 0; for(int k = 0; k < y0; k++) result[i][j] += mat0[i][k] * mat1[k][j]; cout << result[i][j] << ' '; } cout << endl; } return 0; }