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
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.
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 |
// ============================================================================ // 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
Leave a Reply