## C++ || Snippet – How To Find The Highest & Lowest Numbers Contained In An Integer Array

This page will consist of a simple demonstration for finding the highest and lowest numbers contained in an integer array.

REQUIRED KNOWLEDGE FOR THIS SNIPPET

```Integer Arrays For Loops Setw```

Finding the highest/lowest values in an array can be found in one or two ways. The first way would be via a sort, which would render the highest/lowest numbers contained in the array because the values would be sorted in order from highest to lowest. But a sort may not always be practical, especially when you want to keep the array values in the same order that they originally came in.

The second method of finding the highest/lowest values is by traversing through the array, checking each value it contains one by one to determine if the current number which is being compared truly is a target value or not. That method will be displayed below.

``` Find The Highest/Lowest Numbers In An Array C++ // ============================================================================ // Author: Kenneth Perkins // Date: Feb 12, 2012 // Taken From: http://programmingnotes.org/ // File: arrayMinMax.cpp // Description: Determines the highest and lowest value in an array // ============================================================================ #include <iostream> #include <iomanip> #include <ctime> using namespace std; const int NUM_INTS = 14; int main() { // declare variables int arry[NUM_INTS]; // array is initialized using a const variable srand(time(NULL)); // place random numbers into the array for (int x = 0; x < NUM_INTS; ++x) { arry[x] = rand() % 100 + 1; } cout << "Original array values" << endl; // Output the original array values for (int x = 0; x < NUM_INTS; ++x) { cout << setw(4) << arry[x]; } // creates a line separator cout << "\n--------------------------------------------------------"; // use a for loop to go thru the array checking to see the highest/lowest element cout << "\nThese are the highest and lowest array values: "; // set the lowest/highest to the first array item (if any) int lowestScore = NUM_INTS > 0 ? arry[0] : 0; int highestScore = lowestScore; for (int index = 1; index < NUM_INTS; ++index) { // if current score in the array is bigger than the current 'highestScore' // element, then set 'highestScore' equal to the current array element if (arry[index] > highestScore) { highestScore = arry[index]; } // if current score in the array is smaller than the current 'lowestScore' // element, then set 'lowestScore' equal to the current array element if (arry[index] < lowestScore) { lowestScore = arry[index]; } }// end for loop // display the results to the user cout << "\n\tHighest: " << highestScore; cout << "\n\tLowest: " << lowestScore; cout << endl; cin.get(); return 0; }// http://programmingnotes.org/ 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 // ============================================================================//    Author: Kenneth Perkins//    Date:   Feb 12, 2012//    Taken From: http://programmingnotes.org///    File:  arrayMinMax.cpp//    Description: Determines the highest and lowest value in an array// ============================================================================#include <iostream>#include <iomanip>#include <ctime>using namespace std; const int NUM_INTS = 14; int main(){    // declare variables    int arry[NUM_INTS]; // array is initialized using a const variable    srand(time(NULL));     // place random numbers into the array    for (int x = 0; x < NUM_INTS; ++x)    {        arry[x] = rand() % 100 + 1;    }     cout << "Original array values" << endl;    // Output the original array values    for (int x = 0; x < NUM_INTS; ++x)    {        cout << setw(4) << arry[x];    }     // creates a line separator    cout << "\n--------------------------------------------------------";     // use a for loop to go thru the array checking to see the highest/lowest element    cout << "\nThese are the highest and lowest array values: ";     // set the lowest/highest to the first array item (if any)    int lowestScore = NUM_INTS > 0 ? arry[0] : 0;    int highestScore = lowestScore;    for (int index = 1; index < NUM_INTS; ++index)    {        // if current score in the array is bigger than the current 'highestScore'        // element, then set 'highestScore' equal to the current array element        if (arry[index] > highestScore)        {            highestScore = arry[index];        }         // if current score in the array is smaller than the current 'lowestScore'        // element, then set 'lowestScore' equal to the current array element        if (arry[index] < lowestScore)        {            lowestScore = arry[index];        }    }// end for loop     // display the results to the user    cout << "\n\tHighest: " << highestScore;    cout << "\n\tLowest: " << lowestScore;    cout << endl;     cin.get();    return 0;}// http://programmingnotes.org/ ```

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output

```Original array values 10 14 1 94 29 25 7 95 11 17 6 71 100 59 -------------------------------------------------------- These are the highest and lowest array values: Highest: 100 Lowest: 1 ```