Advertisement
ivanwidyan

Smallest, Biggest, and Average of Array

Oct 24th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.88 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <ctime>
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. int findSmallestRemainingElement(int array[], int size);
  7. int findBiggestRemainingElement(int array[], int size);
  8. float findAverage(int array[], int size);
  9.  
  10. void sort(int array[], int size) {
  11.     int smallestIndex = 0;
  12.     int biggestIndex = 0;
  13.     float average = 0;
  14.     smallestIndex = findSmallestRemainingElement(array, size);
  15.     biggestIndex = findBiggestRemainingElement(array, size);
  16.     average = findAverage(array, size);
  17.     cout << "Smallest: " << array[smallestIndex] << endl;
  18.     cout << "Biggest: " << array[biggestIndex] << endl;
  19.     cout << "Average: " << average << endl;
  20. }
  21.  
  22. int findSmallestRemainingElement(int array[], int size) {
  23.     int index_of_smallest_value = 0;
  24.     for (int i = 0; i < size; i++) {
  25.         if (array[i] < array[index_of_smallest_value]) {
  26.             index_of_smallest_value = i;
  27.         }
  28.     }
  29.     return index_of_smallest_value;
  30. }
  31.  
  32. int findBiggestRemainingElement(int array[], int size) {
  33.     int index_of_biggest_value = 0;
  34.     for (int i = 0; i < size; i++) {
  35.         if (array[i] > array[index_of_biggest_value]) {
  36.             index_of_biggest_value = i;
  37.         }
  38.     }
  39.     return index_of_biggest_value;
  40. }
  41.  
  42. float findAverage(int array[], int size) {
  43.     float sumArray = 0;
  44.     for (int i = 0; i < size; i++) {
  45.         sumArray += array[i];
  46.     }
  47.     return sumArray / size;
  48. }
  49.  
  50. void displayArray(int array[], int size) {
  51.     cout << "{";
  52.     for (int i = 0; i < size; i++) {
  53.         if (i != 0) {
  54.             cout << ", ";
  55.         }
  56.         cout << array[i];
  57.     }
  58.     cout << "}";
  59. }
  60.  
  61. int main() {
  62.     int arraySize;
  63.     cout << "Put The Size of The Array: ";
  64.     cin >> arraySize;
  65.     int *array = new int[arraySize];
  66.     srand(time(NULL));
  67.     for (int i = 0; i < arraySize; i++) {
  68.         // keep the numbers small so they're easy to read
  69.         array[i] = rand() % 100;
  70.     }
  71.     cout << "Original array: ";
  72.     displayArray(array, arraySize);
  73.     cout << '\n';
  74.     sort(array, arraySize);
  75.    
  76.     return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement