Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdlib>
- #include <ctime>
- #include <iostream>
- using namespace std;
- int findSmallestRemainingElement(int array[], int size);
- int findBiggestRemainingElement(int array[], int size);
- float findAverage(int array[], int size);
- void sort(int array[], int size) {
- int smallestIndex = 0;
- int biggestIndex = 0;
- float average = 0;
- smallestIndex = findSmallestRemainingElement(array, size);
- biggestIndex = findBiggestRemainingElement(array, size);
- average = findAverage(array, size);
- cout << "Smallest: " << array[smallestIndex] << endl;
- cout << "Biggest: " << array[biggestIndex] << endl;
- cout << "Average: " << average << endl;
- }
- int findSmallestRemainingElement(int array[], int size) {
- int index_of_smallest_value = 0;
- for (int i = 0; i < size; i++) {
- if (array[i] < array[index_of_smallest_value]) {
- index_of_smallest_value = i;
- }
- }
- return index_of_smallest_value;
- }
- int findBiggestRemainingElement(int array[], int size) {
- int index_of_biggest_value = 0;
- for (int i = 0; i < size; i++) {
- if (array[i] > array[index_of_biggest_value]) {
- index_of_biggest_value = i;
- }
- }
- return index_of_biggest_value;
- }
- float findAverage(int array[], int size) {
- float sumArray = 0;
- for (int i = 0; i < size; i++) {
- sumArray += array[i];
- }
- return sumArray / size;
- }
- void displayArray(int array[], int size) {
- cout << "{";
- for (int i = 0; i < size; i++) {
- if (i != 0) {
- cout << ", ";
- }
- cout << array[i];
- }
- cout << "}";
- }
- int main() {
- int arraySize;
- cout << "Put The Size of The Array: ";
- cin >> arraySize;
- int *array = new int[arraySize];
- srand(time(NULL));
- for (int i = 0; i < arraySize; i++) {
- // keep the numbers small so they're easy to read
- array[i] = rand() % 100;
- }
- cout << "Original array: ";
- displayArray(array, arraySize);
- cout << '\n';
- sort(array, arraySize);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement