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
|
#include<iostream>
#include<string>
using namespace std;
bool insert(int value, int intArray[], int & numberOfValidEnties, int size);
bool erase(int value, int intArray[], int & numberOfValidEnties);
int main()
{
const int size = 10;
int value = 0;
int intArray[size];
int numberOfValidEnties = 0;
char answer;
char selection;
do {
cout << "\n\n=================== What would you like to do ===================\n\n";
cout << "A: Insert a value\n";
cout << "B: Delete a value\n";
cout << "C: Print the array\n";
cout << "Your choice: ";
cin >> selection;
if (toupper(selection) == 'A')
{
insert(value, intArray, numberOfValidEnties, size);
}
else if (toupper(selection) == 'B')
{
erase(value, intArray, numberOfValidEnties);
}
cout << "Would you like to quit?(Y/N): ";
cin >> answer;
} while (toupper(answer) == 'N');
return 0;
}
bool insert(int value, int intArray[], int & numberOfValidEnties, int size)
{
int temp;
cout << "Enter no more than 10 values to be sorted: ";
cin >> value;
while (value >= 0 && numberOfValidEnties < 10)
{
intArray[numberOfValidEnties] = value;
numberOfValidEnties++;
cout << "(Enter -1 to stop): ";
cin >> value;
}
for (int i = 0; i < numberOfValidEnties; i++) // sorting the array
{
for (int j = 0; j < numberOfValidEnties; j++)
{
if (intArray[j] > intArray[j+1])
{
temp = intArray[j];
intArray[j] = intArray[j+1];
intArray[j+1] = temp;
}
}
}
for (int i = 0; i < numberOfValidEnties; i++)
{
cout << intArray[i] << endl;
}
cout << "Enter a value to be added to the array: ";
cin >> value;
return true;
}
|