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 211 212 213 214 215 216 217 218 219
|
#include <iostream>
#include <iomanip>
using namespace std;
const int ARRAY_SIZE = 50;
/*----------------------------------------------------------------------------*/
/* Populate the array with 50 randomly generated integer values
* in the range 1-50. */
void populateArray(int ar[], const int n);
/* Find the index of the smallest element in array range. */
int indexOfSmallestElement(const int startAt, const int ar[], const int n);
/* Find the index of the largest element in array range. */
int indexOfLargestElement(const int startAt, const int ar[], const int n);
/* Perform a selection sort of the array. */
void selectionSort(int ar[], const int n);
/* Swap the elements indicated by the two indices. */
void swap(int ar[], const int index1, const int index2);
/* Compute the average of all the elements. */
double average(const int ar[], const int n);
/* Compute the sum of all the elements. */
int sum(const int ar[], const int n);
/* Find all the elements that are greater than the threshold and
* store in the foundElements arrray. Update the count to reflect
* how many elements were found and stored in the array. */
void findElementsGreaterThan(const double threshold,
const int ar[], const int n,
int foundElements[], int& count);
/* Display the array. */
void displayArray(const int ar[], const int n);
/*----------------------------------------------------------------------------*/
int main() {
int ar[ARRAY_SIZE];
/* Populate the array with random numbers. */
populateArray(ar, ARRAY_SIZE);
/* Display the array. */
cout << "Original array:" << endl;
displayArray(ar, ARRAY_SIZE);
/* Sort the array. */
cout << endl << "Sorted array:" << endl;
selectionSort(ar, ARRAY_SIZE);
displayArray(ar, ARRAY_SIZE);
/* Find the smallest value and its index. */
int index = indexOfSmallestElement(0, ar, ARRAY_SIZE);
cout << endl
<< "Smallest element: " << ar[index]
<< ", at index [" << index << "]";
/* Find the largest value and its index. */
index = indexOfLargestElement(0, ar, ARRAY_SIZE);
cout << endl
<< "Largest element: " << ar[index]
<< ", at index [" << index << "]"
<< endl;
/* Find the sum and average. */
int sumOfElements = sum(ar, ARRAY_SIZE);
double ave = average(ar, ARRAY_SIZE);
cout << endl
<< "Sum of elements: " << sumOfElements
<< endl
<< "Average of elements: " << ave
<< endl;
/* Find all the elements largest than the average. */
int foundElements[ARRAY_SIZE];
int count;
cout << endl
<< "Elements larger than the average: ";
findElementsGreaterThan(ave, ar, ARRAY_SIZE, foundElements, count);
cout << endl;
displayArray(foundElements, count);
cout << endl << endl;
return 0;
}// end main()
/*----------------------------------------------------------------------------*/
/* Populate the array with 50 randomly generated integer values
* in the range 1-50. */
void populateArray(int ar[], const int n){
int randomNumber;
for (int a = 0; a <= ARRAY_SIZE; a++) {
randomNumber = rand() % 50 + 1;
ar[a]=randomNumber;
}
}
/* Find the index of the smallest element in array range. */
int indexOfSmallestElement(const int startAt, const int ar[], const int n){
int minIndex = startAt;
for (int i = startAt; i <= n - 1; i++) {
minIndex = (ar[i] < ar[minIndex])? i : minIndex;
}
return minIndex;
}
/* Find the index of the largest element in array range. */
int indexOfLargestElement(const int startAt, const int ar[], const int n){
int maxIndex = startAt;
for (int i = startAt; i <= n - 1; i++) {
maxIndex = (ar[i] > ar[maxIndex])? i : maxIndex;
}
return maxIndex;
}
/* Perform a selection sort of the array. */
void selectionSort(int ar[], const int n){
for (int top = 0; top <= n - 2; top++) {
swap(ar, top, indexOfSmallestElement(top, ar, n));
}
}
/* Swap the elements indicated by the two indices. */
void swap(int ar[], const int index1, const int index2){
int tmp = ar[index1];
ar[index1] = ar[index2];
ar[index2] = tmp;
}
/* Compute the average of all the elements. */
double average(const int ar[], const int n){
int sum;
double ave = sum /(double) n;
return ave;
}
/* Compute the sum of all the elements. */
int sum(const int ar[], const int n){
int sum = 0.0;
for (int count = 0; count <= n - 1; count++) {
sum += ar[count];
}
return sum;
}
/* Find all the elements that are greater than the threshold and
* store in the foundElements arrray. Update the count to reflect
* how many elements were found and stored in the array. */
void findElementsGreaterThan(const double threshold,const int ar[], const int n,int foundElements[], int& count){
cout<< "1"<<endl;
}
/* Display the array. */
void displayArray(const int ar[], const int n){
for(int i = 0; i < ARRAY_SIZE; i++)
{
if((i+1) % 10 == 0) { cout << endl; }
cout << ar[i] << ' ';
}
}
|