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
|
#include <iomanip>
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int *arr;
int *createArray(int &size);
void findStats(int *arr, int size, int &min, int &max, double &average);
int *searchElement(int *arr, int size, int *element);
int main()
{
int userOption;
int ix = 0;
int size;
int min = 0;
int max = 0;
int userElement;
double average = 0;
do
{
cout << "1. Create and initialize a dynamic array" << endl;
cout << "2. Display statistics on the array" << endl;
cout << "3. Search for an element" << endl;
cout << "4. Quit" << endl << endl;
cout << "Please select option (1-4)" << endl;
cin >> userOption;
if (userOption != 1 && userOption != 4 && ix == 0)
{
cout << endl << "Error; initial choice must be 1." << endl << endl;
}
ix++;
if (userOption == 1)
{
cout << "Please enter an integer value for the size of the array." << endl;
cin >> size;
int arr = *createArray(size);
for (int ia = 0; ia < size; ia++)
cout << "Element " << ia << ":" << arr + ia << endl;
cout << endl << endl;
}
else if (userOption == 2)
{
findStats(arr, size, min, max, average);
cout << endl << "Statistics:" << endl;
cout << "Min: " << min << endl;
cout << "Max: " << max << endl;
cout << "Average: " << average << endl << endl;
}
else if (userOption == 3)
{
cout << endl << " Enter an integer to search the array for." << endl;
cin >> userElement;
int *element = &userElement;
int location = *searchElement(arr, size, element);
if (location)
cout << "Integer found in element: " << location << endl;
else
cout << "Integer not found." << endl;
cout << "Select 4 to end." << endl;
}
} while (userOption != 4);
return 0;
}
int *createArray(int &size)
{
int* userArray = new int[size]();
for (int ixx = 0; ixx < size; ixx++)
{
srand(time(NULL));
userArray[ixx] = rand() % 100 + 1;
}
arr = *&userArray;
return arr;
}
void findStats(int *arr, int size, int &min, int &max, double &average)
{
int ifs = 0,
sum = 0;
min = (arr[ifs]);
for (ifs = 0; ifs < size; ifs++)
{
if (min >(arr[ifs]))
{
min = (arr[ifs]);
}
if (max < (arr[ifs]))
{
max = (arr[ifs]);
}
sum += (arr[ifs]);
}
average = sum / size;
}
int *searchElement(int *arr, int size, int *element)
{
int *location,
index = 0,
position = -1;
bool found = false;
while (index < size && !found)
{
if (arr[index] == *element);
{
found = true;
position = index;
}
index++;
}
return location = &position;
}
|