0

So I'm having some trouble with something. I have to create a function that will find the smallest number in an array. I know of one way to do it, using an overkill amount of if/else if, which won't do any good if the array size changes. I know using a for loop should do the trick but I can't figure out how to write it. Any push in the right direction would be greatly appreciated.

#include <iostream>
using namespace std;

int findLowest(int[]);

int main()
{
    int AR[5] = {4, 87, 1, -3, 78};
    cout << findLowest(AR);

    return 0;
}

int findLowest(int AR[])
{

     return lowest;
}
2
  • 1
    Unless you want/need to do this on your own, perhaps look up std::min_element. Commented Oct 19, 2015 at 23:45
  • "won't do any good if the array size changes" Does it change while you are performing the task? If not, just store temp element and check against it. Commented Oct 19, 2015 at 23:47

4 Answers 4

3

If you can change the function signature and include a header file that specifies general limits, you could do the following, which reads through the array in one pass:

#include <climits>
...

/* assumes AR_size > 0 */
int findLowest(int AR[], int AR_size)
{
     int lowest = INT_MAX;
     for (i = 0; i < AR_size; ++i) {
         lowest = (AR[i] < lowest) ? AR[i] : lowest;
     }
     return lowest;
}
Sign up to request clarification or add additional context in comments.

13 Comments

lowest = (AR[i] < lowest) ? AR[i] : lowest; is clean but inefficient as it causes an assignment to itself. The optimizer may remove this, but if (AR[i] < lowest) lowest = AR[i]; should be faster.
I like ternary statements for some reason. More readable, maybe. Point taken but the performance hit will likely be very minimal.
Is there more efficient way then this one without using if(ar[i] > lower)?
@AlexReynolds I also like ternary statements and use them quite often, but I do embedded development and if this function was used a lot the single assembly call could turn out to be a big deal. If living in the desktop world than no need to worry about the little stuff.
@CroCo There can be no number greater than INT_MAX stored into an int. The value of INT_MAX is compiler dependent, however assuming your compiler defines int as a 32 bit number, then INT_MAX is 2^31 (since int is signed). The whole point is to initialize it to the largest possible number it can be. INT_MAX is defined in limits.h, look at the documentation.
|
3
template<size_t N>
int findLowest(int (&ar)[N])
{
    return *std::min_element(std::begin(ar), std::end(ar));
}

Note the usage of a template to make sure we get the size information from the caller.

1 Comment

Definitely more C++-ic
0
#include <iostream>
#include <cassert>
using namespace std;

int findLowest(int ar[], const int& SIZE)
{
    assert(("Error: the size of the array is zero. Make sure that ", SIZE > 0));

    int lowest = ar[0];

    for(int i=0; i<SIZE;i++)
    {
        if(lowest > ar[i])
            lowest = ar[i];
    }
    return lowest;
}

int main()
{
    const int SIZE(5);
    int AR[5] = {11, 12, 10, 14, 15};
    cout << findLowest(AR,SIZE);

    return 0;
}

Comments

0

Instead of defining your own function to find the smallest number in your array, why do you not use the standard std::min_element function to do it for you? Create a std::vector object from the array, and let the min_element function do the job for you.

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <vector>

#define ARRAY_SIZE 5

int main ( int argc, char **argv )
{
        int ar [ ARRAY_SIZE ] = {4, 87, 1, -3, 78};

        std::vector<int> arVector ( ar, ar + ARRAY_SIZE );

        std::cout << *std::min_element ( arVector.begin ( ), arVector.end ( )) << std::endl;

        return EXIT_SUCCESS;
}

Output:

-3

3 Comments

mega overkill. The standard library already has a min_element function.
Sorting to find a minimum (or maximum) value is relatively expensive and unnecessary when you can find bounds in linear time (one pass).
My bad, I was a little quick to read the question. Thought he wanted a sorting function. I have corrected it now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.