// Author: Keith Shomper // Date: 10/3/05 // Purpose: To demonstrate multidimensional arrays #include #include #include using namespace std; int main() { const int ROWS = 4; const int COLUMNS = 2; string s; int a[ROWS][COLUMNS], b[ROWS][COLUMNS], sum[ROWS][COLUMNS]; // Initialize the random number generator srand(time(0)); // Build two arbirary ROW x COLUMN matrices for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLUMNS; j++) { a[i][j] = rand() % 10 + 1; b[i][j] = rand() % 10 + 1; } } // Print out the contents of the matrices cout << "The matrix 'a' has the values: " << endl; for (int i = 0; i < ROWS; ++i) { cout << '\t'; for (int j = 0; j < COLUMNS; j++) { cout << setw(4) << a[i][j]; } cout << endl; } cout << "The matrix 'b' has the values: " << endl; for (int i = 0; i < ROWS; ++i) { cout << '\t'; for (int j = 0; j < COLUMNS; j++) { cout << setw(4) << b[i][j]; } cout << endl; } cout << endl << endl << "Press enter to continue .... "; getline(cin, s, '\n'); // Calculate the sum of the two ROW x COLUMN matrices for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLUMNS; j++) { sum[i][j] = a[i][j] + b[i][j]; } } cout << "The sum of the matrices is: " << endl; for (int i = 0; i < ROWS; ++i) { cout << '\t'; for (int j = 0; j < COLUMNS; j++) { cout << setw(4) << sum[i][j]; } cout << endl; } cout << endl << endl << "Press enter to continue .... "; getline(cin, s, '\n'); return 0; }