How to Find the Highest and Lowest Element in Arrays

In this tutorial, we will do an interesting exercise, which involves arrays and searching. We are going to generate an array of 1 million elements, with random values, and we are going to have the machine scan this giant array, looking for the largest and smallest elements in the array.

Searching in Arrays

First, we define the size of our array, which will be SIZE, which will be a constant of 1 million.
Then, just declare our array, named 'numb': numb[SIZE];

We will declare an auxiliary variable, the 'count', and two others that will store the value of the highest and lowest value contained in that array.

Let's first go in search of the biggest element.
The logic is as follows: at first, let's assume that the first element of the array, the one with index 0, is the largest. So, we do:
highest = numb[0];

What we have to do is go through the entire array, starting from index 1 and compare all other elements with the value stored in 'highest'.

Let's compare if the element 'numb [1]' is greater than 'highest'.
Let's compare if the element 'numb [2]' is greater than 'highest'.
Let's compare if the element 'numb [3]' is greater than 'highest'.
...
Let's compare if the element 'numb [999999]' is greater than 'highest'.

These comparisons we will do with a simple IF conditional test, inside a FOR looping that will go through all the elements of the giant array:
if (numb[count]> highest)

Now, if any element is greater, we must change the value stored in 'highest' to this new value, do you agree? In short, just do:
highest = numb [count];

Our code looks like this:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
    const int SIZE = 1000000;
    int numb[SIZE], count, highest, lowest;
    srand( time(0) );

    for (count=0; count<SIZE; count++ )
        numb[count] = rand();

    highest=numb[0];
    for (count=1; count<SIZE; count++){
        if (numb[count] > highest)
            highest = numb[count] ;
    }

    lowest=numb[0];
    for (count=1; count<SIZE; count++){
        if (numb[count] < lowest)
            lowest = numb[count] ;
    }

    cout<<"Biggest : "<<highest<<endl;
    cout<<"Smallest: "<<lowest<<endl;
}
And to find the lowest value?
The logic is the same.

We cause the lowest value to initially store the value of the first element of the array:
lowest = numb[0];

Then we compare all other elements of the array with this 'lowest', checking if the other elements of the array are MINOR, that lowest, if they are, we update the new value of lowest.

In fact, we can even merge these two FOR loops with nested IFs, in one loop, see:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
    const int SIZE = 1000000;
    int numb[SIZE], count, highest, lowest;
    srand( time(0) );

    for (count=0; count<SIZE; count++ )
        numb[count] = rand();

    highest=numb[0];
    lowest=numb[0];

    for (count=1; count<SIZE; count++){
        if (numb[count] > highest)
            highest = numb[count];

        if (numb[count] < lowest)
            lowest = numb[count];
    }

    cout<<"Biggest : "<<highest<<endl;
    cout<<"Smallest: "<<lowest<<endl;
}
1 million elements, and we find the smallest and largest elements almost instantly. Powerful this scheme of searching with arrays and loops, don't you think? C++ is awesome.

No comments:

Post a Comment