0

Given a task of finding the index of a minimum value in an array, simply looping over, storing the index of smallest value so far and updating it with

min_index = arr[i] < arr[min_index] ? i : min_index;

is simple.

However, we were given the same task, however, we were specified that we MUST use the min(a, b) function.

This could work:

min_index = min(arr[i], arr[min_index]) == arr[i] ? i : min_index;

However, numbers we were dealing with were floats with unknown precision.

Zobody
  • 101
  • 2
  • 1
  • Does this answer your question? [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Mohammed Deifallah Jan 22 '20 at 07:44
  • 1
    Can you clarify the question more? What is the problem with float numbers? – Zefick Jan 22 '20 at 07:49
  • 1
    _This could work_ — So, does it work or not? Have you tried? What is your question? – Daniel Langr Jan 22 '20 at 07:49
  • Partly related: you can use `std::min` on an array of indices (no explicit loop), with a dedicated comparaison function on the indices – Damien Jan 22 '20 at 09:07
  • 2
    @MohammedDeifallah: [That question](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) has no relevance here. This question does not involve any operations in which rounding errors occur. – Eric Postpischil Jan 22 '20 at 12:17
  • You should show or explain the complete context of the course assignment. Artificial constraints such as requiring the use of `min` are introduce to class exercises to force students to focus on one thing or another. We need to know whether the constraint to use `min` is intended to force you to focus on the use of the minimum value it returns, whether you may use the lvalue it returns, or whether you are intended to extend it by applying it to a custom defined type that contains data about both the value and the array index of an array element, or something else. – Eric Postpischil Jan 22 '20 at 12:48
  • *E.g.*, if your class was recently learning about custom definitions for C++ operators and standard facilities, you may be intended to apply `min` to a custom type. If your class was recently learning about lvalues and references, you may be intended to use the lvalue feature of `min`. – Eric Postpischil Jan 22 '20 at 12:49
  • If NaN are present, `min(a, b)` must be used with extra caution. – chux - Reinstate Monica Jan 22 '20 at 15:43

2 Answers2

1

I don't think there is anything wrong with your solution for an array of floating-point elements (I assume that there are no NaNs). std::min returns a reference to its argument with a lower value (which is defined for non-nan FP numbers). Thus std::min(arr[i], arr[min_index]):

  1. either returns a reference to arr[i] if it is less or equal to arr[min_index],
  2. or, returns a reference to arr[min_index], if this is greater than arr[i].

In 1., when you compare the result of std::min with arr[i], you are comparing two very same FP objects, which is guaranteed to be evaluated ot true.

In 2., you are comparing two FP objects with different values, which is guaranteed to be evaluated to false.

Therefore, I think your solution works well.

Just be aware that rounding errors outside of your this solution can seemingly break it. Consider the following example:

float arr[3] = { 1.0f / 3.0f, 0.33333334, 2.0f };
size_t min_index = 0;
for (size_t i = 1; i < 3; i++)
   min_index = std::min(arr[i], arr[min_index]) == arr[i] ? i : min_index;
std::cout << min_index;

1/3 is the smallest number, but this printed out 1 in my experiment, since what your code is comparing is a computer representation of 1.0f/3.0f with a computer representation of 0.33333334, which both are subjects of rounding. Consequently, the numbers actually stored in arr, are different.

Daniel Langr
  • 22,196
  • 3
  • 50
  • 93
0
#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    int n;
    cin>>n;
    float arr[n];
    float min_index = 0;
    float mini = 0;


    for(int i=0; i<n; i++)
    {
        cin>>arr[i];
    }

    mini = arr[0];

    for(int i=0; i<n; i++)
    {
        mini = min(arr[i], mini);
    }

    for(int i=0; i<n; i++)
    {
        if(arr[i] == mini)
            min_index = i;
    }

    cout<<min_index<<endl;

    return 0;
}

working fine with float! could u be more specific about ur precision?

Iraz Irfan
  • 10
  • 5