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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
|
#include <iostream>
#include <cctype> // To be used with the toupper function.
// Function prototypes.
void displayMenu(int&);
void askNumberOfValues(int&);
void enterValuesInArray(int*, const unsigned int);
void readValuesInArray(const int*, const unsigned int);
void showArrayReverseOrder(const int*, const unsigned int);
void showMultiplesThrees(const int*, const unsigned int);
void sumAllTheElements(const int*, const unsigned int);
int main()
{
int sizeOfArray{};
int* arrayOfInt{ nullptr };
int choice{};
int sum{};
char deleteArrayChoice{};
bool keepLooping{ true };
// Keep looping until the user hits option 6 to close the program.
while (keepLooping)
{
displayMenu(choice);
switch (choice)
{
case 1:
if (sizeOfArray == 0)
{
askNumberOfValues(sizeOfArray);
arrayOfInt = new int[sizeOfArray];
}
else
{
std::cout << "This option will override the current array.\n";
std::cout << "Do you wish to continue (Y/N)?: ";
std::cin >> deleteArrayChoice;
if (std::toupper(deleteArrayChoice) == 'Y')
{
delete[] arrayOfInt;
arrayOfInt = nullptr;
sizeOfArray = 0;
askNumberOfValues(sizeOfArray);
arrayOfInt = new int[sizeOfArray];
}
else
{
break;
}
}
enterValuesInArray(arrayOfInt, sizeOfArray);
keepLooping = true;
break;
case 2:
readValuesInArray(arrayOfInt, sizeOfArray);
keepLooping = true;
break;
case 3:
showArrayReverseOrder(arrayOfInt, sizeOfArray);
keepLooping = true;
break;
case 4:
showMultiplesThrees(arrayOfInt, sizeOfArray);
keepLooping = true;
break;
case 5:
sumAllTheElements(arrayOfInt, sizeOfArray);
keepLooping = true;
break;
case 6:
std::cout << "Closing the program...\n";
keepLooping = false;
break;
default:
std::cout << "Please choose between 1 and 6.\n";
keepLooping = true;
}
}
delete[] arrayOfInt;
arrayOfInt = nullptr;
return 0;
}
void displayMenu(int& choice)
{
std::cout << "\n Menu Based Array\n";
std::cout << "-----------------------------------------\n";
std::cout << "1) Enter Values in Array\n";
std::cout << "2) Read Array Elements\n";
std::cout << "3) Display Array in Reverse Order\n";
std::cout << "4) Display Array Elements in Multiple of 3s.\n";
std::cout << "5) Display the Sum of the Elements in Array.\n";
std::cout << "6) Exit the program\n\n";
std::cout << "Enter your choice: ";
std::cin >> choice;
}
void askNumberOfValues(int& numOfValues)
{
std::cout << "Enter the number of values in the array: ";
std::cin >> numOfValues;
}
void enterValuesInArray(int* arrayOfInt, const unsigned int sizeOfArray)
{
for (unsigned int count = 0; count < sizeOfArray; count++)
{
std::cout << "Element number " << count + 1 << " : ";
std::cin >> *(arrayOfInt + count);
}
}
void readValuesInArray(const int* arrayOfInt, const unsigned int sizeOfArray)
{
if (sizeOfArray == 0)
{
std::cout << "Array is empty, first enter data in array.\n";
}
else
{
std::cout << "The array elements are...\n";
for (unsigned int count = 0; count < sizeOfArray; count++)
{
std::cout << "Element number " << count + 1 << " : "
<< *(arrayOfInt + count) << std::endl;
}
}
}
void showArrayReverseOrder(const int* arrayOfInt, const unsigned int sizeOfArray)
{
if (sizeOfArray == 0)
{
std::cout << "Array is empty, first enter data in array.\n";
}
else
{
std::cout << "The reversed array elements are...\n";
for (int count = sizeOfArray - 1; count > -1; count--)
{
std::cout << "Element number " << count + 1 << " : "
<< *(arrayOfInt + count) << std::endl;
}
}
}
void showMultiplesThrees(const int* arrayOfInt, const unsigned int sizeOfArray)
{
int countMulOf3s{ 0 };
if (sizeOfArray == 0)
{
std::cout << "Array is empty, first enter data in array.\n";
}
else
{
std::cout << "Displaying elements with multiple of threes...\n";
for (unsigned int count = 0; count < sizeOfArray; count++)
{
if (*(arrayOfInt + count) % 3 == 0)
{
std::cout << "Element number " << count + 1 << " : "
<< *(arrayOfInt + count) << std::endl;
countMulOf3s++;
}
}
if (countMulOf3s == 0)
{
std::cout << "No elements with multiple of threes in the array\n";
}
}
}
void sumAllTheElements(const int* arrayOfInt, const unsigned int sizeOfArray)
{
int sum{};
if (sizeOfArray == 0)
{
std::cout << "Array is empty, first enter data in array\n";
return;
}
else
{
for (unsigned int count = 0; count < sizeOfArray; count++)
{
sum += *(arrayOfInt + count);
}
}
std::cout << "The sum of all elements in the array is: " << sum << std::endl;
}
| |