2

If you have a function int getMin(int a[], int n) then what would you say is the cleanest solution to deal with the empty array case?

3
  • 12
    I'd probably use std::min_element() instead of writing my own. I think it returns the end iterator on an empty container. Commented Sep 27, 2011 at 20:54
  • 7
    Don't use int for n, use the appropriate integer: size_t. Commented Sep 27, 2011 at 21:01
  • there is no integer that is the minimum of an empty array of integers so the given return type is wrong. If you must keep the return type then you might throw an exception for this case. Commented Sep 30, 2011 at 4:26

4 Answers 4

12

Return a pointer to the minimum element instead of the element itself. This way, a pointer value of one past the end of the array can indicate not found. (Or in this case empty)

This is the strategy taken by std::min_element, which already implements what you're doing.

You can even implement this in terms of std::min_element:

int* getMin(int a[], int n)
{
    return std::min_element(a, a+n);
}
Sign up to request clarification or add additional context in comments.

Comments

3

Assuming you're looking for the minimum value in the array how about:

if (!n)
  throw YourPreferredException();

Or:

#include <limits>
//...
if (!n)
  return std::numeric_limits<int>::max();

Or, if it should never happen:

#include <cassert>
//...
assert(n);

It depends on the application and the values you're expecting to be passing in. What makes most sense and what fits the existing code base is hard to guess.

5 Comments

Might as well make that if (!a || !n), too. @cnicutar: Because 0 is a valid return value. Why return a valid thing with invalid arguments?
0 might be a legitimate value - if the range of the int array was -10 to 10 then it would be indistinguishable.
if (n <= 0). or an assert(n > 0), if you do thorough testing.
@Pubby8 - I've not really had much trouble with it, especially for integers representing a size. Clearly it's appropriate to stick to local coding styles regardless of the path taken though. If I was going to write it explicitly I'd do (0 == n) instead of (n == 0) though.
@yi_H - good point, I'm not usually in the habit of using signed types to store lengths of arrays :)
3

Maybe instead, do it the way the standard library does: Take two iterators as parameters and return the end parameter if the sequence is empty. Better still use min_element instead of rolling your own.

If you need to do it the array/length way either throw or return std::numeric_limits<int>::max()

1 Comment

If you return min and do min(x, getMin(a,0)), the getMin call will "poison" the result. It's probably "more neutral" to return max instead.
1

There is definitely no "cleanest solution" absent an understanding of the domain. Mathematically, the infimum of any set of values from a domain is the greatest lower bound (in the domain) of all elements of the set. For the extended integers, this would be +infinity for an empty set. (See, e.g., the Wikipedia article on Empty Set) If your domain is all C++ int values, a (mathematically consistent) return value would then be INT_MAX.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.