Search code examples
c++arraysalgorithmiteratormin

Find min value in array


I have two arrays:

int playerSums[9] = { };
string playerNames[9] = { };

I am trying to get the smallest value in the array playerSums and also the array index of this value.

Here's what I've tried so far:

if (playerNames[index] == "End" || playerNames[index] == "end") {
    int lowestValue = playerSums[0];
    for (i = 1; i < sizeof(playerSums) / sizeof(playerSums[0]); i++) {
        if (playerSums[i] < lowestValue || lowestValue != 0)
            lowestValue = playerSums[i]; 
    }
    cout << index[playerNames] << " had the lowest values and got the sum ";
    cout << lowestValue << endl;
}

How do I find and display the smallest value in the array playerSums if for example only 3 players are playing, i.e. only 3 elements of the array are populated (and the rest of the elements are equal to zero)?

I need the index to display the name of the player who got the smallest value.


Solution

  • You can use standard algorithm std::min_element declared in header <algorithm> that to find the element witn minimum sum. For example

    #include <algorithm>
    
    int *min = std::min_element( playerSums, playerSums + 3 );
    
    std::cout <<  playerNames[min - playerSums] 
              << " had the lowest values and got the sum " << *min
              << std::endl;
    

    The same can be written using standard functions std::begin, std::end and std::distance declared in header <iterator>

    #include <algorithm>
    #include <iterator>
    
    int *min = std::min_element( std::begin( playerSums ), std::end( playerSums ) );
    
    std::cout <<  playerNames[ std::distance( playerSums, min )] 
              << " had the lowest values and got the sum " << *min
              << std::endl;
    

    Instead of using the algorithm you could write your own function similar to the algorithm. For example

    size_t min_sum( const int playerSums[], size_t n )
    {
       size_t min = 0;
    
       for ( size_t i = 1; i < n; i++ )
       {
          if ( playerSums[min] < playerSums[i] ) min = i;
       }
    
       return min;
    }
    
    size_t min = min_sum( playerSums, sizeof( playerSums ) / sizeof( *playerSums )  );
    
    std::cout <<  playerNames[min] 
              << " had the lowest values and got the sum " << playerSums[min]
              << std::endl;
    

    If you need to skip elements of the array that equal to zero then the function will look like

    size_t min_sum( const int playerSums[], size_t n )
    {
       size_t min = 0;
    
       while ( min < n && playerSums[i] == 0 ) ++min;
    
       for ( size_t i = min; i < n; i++ )
       {
          if ( playerSums[min] < playerSums[i] ) min = i;
       }
    
       return min;
    }
    
    size_t min = min_sum( playerSums, sizeof( playerSums ) / sizeof( *playerSums )  );
    
    if ( min != sizeof( playerSums ) / sizeof( *playerSums ) )
    {   
        std::cout <<  playerNames[min] 
                  << " had the lowest values and got the sum " << playerSums[min]
                  << std::endl;
    }