1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
//#include "stdafx.h"
//#include "targetver.h"
#include <iostream>
#include <iomanip>
using namespace std;
const int ROW = 5;
const int COL = 5;
void displayMenu();
int getInput(); // added input function prototype
void displayArray(int myArray[][COL], int);
void multiArray(int myArray[][COL], int);
void swapArray(int myArray[][COL], int);
void sum(int myArray[][COL], int);
void displayExit(); // added exit message function prototype
void displayError(); //added error message function protoype
int main() {
int myArray[ROW][COL] = {
{ 1,2,3,4,5 },
{ -20,-40,-60,-80,-100 },
{ 3,3,3,3,3 },
{ 5,7,9,11,13 },
{ -15,-25,-35,-45,-55 }
};
int choice = 0;
do { //Added the DO .. While Loop
choice = getInput();
displayMenu();
switch(choice)
{
case 1:
displayArray(int myArray, ROW);
break;
case 2:
multiArray(int myArray, ROW);
break;
case 3:
swapArray(int myArray, ROW);
break;
case 4: // added case 4 and 5
sum(int myArray, ROW);
break;
case 5:
displayExit();
break;
default:
displayError();
break;
}
}while (choice !=5)
}
void displayMenu()
{
cout << "1. Display array" << endl;
cout << "2. Multiply array by 3" << endl;
cout << "3. Swap row 2 and row 3" << endl;
cout << "4. Display sum of all elements" << endl;
cout << "5. Exit" << endl;
}
int getInput() { // added getInput function to gather the input from the user
cout << "Input values 1 through 4, or 5 to exit program: " << endl;
int choice;
if (!(cin >> choise)) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
choice = 0;
}
cout << endl;
return choice;
}
void displayArray(int myArray[][COL], int rows) { // fixed display function ( i think)
cout << "Current Array: " << endl;
for (int row = 0; row < rows; row++) {
for (int col = 0; col < COLS; col++) {
cout << setw(5) << right << myArray[ROW][COL] << " ";
}
cout << endl;
}
cout << endl;
}
void multiArray(int myArray[][COL], int rows)
for (int row = 0; row < rows; row++) {
for (int &val : myArray[row]) {
val *= 3;
}
}
cout << "Array multiplied by 3" << endl;
}
void swapArray(int myArray[][COL], int rows){
}
void sum(int myArray[][COL], int rows) { // added empty sum function to get back to
}
void displayExit() {
cout << "The Program will now end" << endl;
}
void displayError() {
cout << "Sorry, that is not a valid choice." << endl;
}
|