简体   繁体   中英

Is there a clean and fast way to find the min and max of an array

Is there a clean and fast way to find the min and max of an array

int array1[] = {2,6,10,22,12};

results are

min = 2 / max = 22

// Function  
void min_max(set<int> my_set) 
{ 
    for (auto i : my_set); 
} 

// Function to find the maximum element 
int findMax(set<int> my_set) 
{ 

    // Get the maximum element 
    int max_element; 
    if (!my_set.empty()) 
        max_element = *(my_set.rbegin()); 

    // return the maximum element 
    return max_element; 
} 

// Function to find the minimum element 
int findMin(set<int> my_set) 
{ 

    // Get the minimum element 
    int min_element; 
    if (!my_set.empty()) 
        min_element = *my_set.begin(); 

    // return the minimum element 
    return min_element; 
} 

int main() 
{ 

    // Get the set 
    set<int> my_set = {2,6,10,22,12}; 
        // results
        findMin(my_set) 
        findMax(my_set) 
        
} 

Don't reinvent the wheel. The C++ Standard Library exists to make common programming tasks take very little time, and with a very small chance of error.

You want std::min_element and std::max_element . You'll need to include <algorithm> to use these.

#include <algorithm> // for min/max_element
#include <iterator>  // for begin/end

...

int array1[] = {2,6,10,22,12};

int min = std::min_element(std::begin(array1), std::end(array1));
int max = std::max_element(std::begin(array1), std::end(array1));

The documentation for these functions can be found at the following pages:

https://en.cppreference.com/w/cpp/algorithm/min_element
https://en.cppreference.com/w/cpp/algorithm/max_element
https://en.cppreference.com/w/cpp/iterator/begin

If you want to get really fancy you can use std::minmax with std::tie to unpack the std::pair returned.

#include <algorithm> // for minmax_element
#include <iterator>  // for begin/end
#include <tuple>     // for tie

...

int array1[] = {2,6,10,22,12};

int min, max;
std::tie(min, max) = std::minmax_element(std::begin(array1), std::end(array1));

https://en.cppreference.com/w/cpp/algorithm/minmax_element
https://en.cppreference.com/w/cpp/utility/tuple/tie

You included an unfinished structural code snippet but without any apparent context in your question. I'm assuming this is your homework, where you're supposed to implement the logic.

Based on that assumption I don't think using a library method such as.max() or.min() on the array/set will suffice as a solution. I would urge you to take a few minutes and,

  1. Think about the problem
  2. If you can, break the problem down into smaller problems
  3. Try and think up possible solutions, one small problem at a time
  4. Try and implement your solution, step through with the debugger, see what happens. If it fails, try other approaches, adjustments etc., maybe you'll get there!

If you manage to come up with a solution, great. If not then either way I would recommend that after putting some mental effort on it googling possible solutions and discussions and explanations and really internalizing what is happening there.

But here's pseudocode for you, because you're supposed to do your own homework.

    let maxValue = minimum value of whatever datatype you're using

    for each number in some array
      if the number is larger than maxValue then 
        maxValue = number
        
    return maxValue

You can probably figure out how to find the minimum value from here if you use your brain.

You can find the min, max and sum in one pass:

int array1[] = {2,6,10,22,12};
const int quantity = 
    sizeof(array1) / sizeof(array1[0]);
int max = array1[0];
int min = array1[0];
int sum = 0;
for (int i = 1; i < quantity; ++i)
{
  const int value = array1[i];
  if (value > max) max = value;
  if (value < min) min = value;
  sum = sum + value;
}

The above is called a running maximum as well as a running minimum . The sum is added at little bit of extra cost (not much).

Thank you Thomas

This will used for linear position of multiple devices

if one position gets to far off from another it will then run a Level function

Nice and simple

  const int quantity = 
  sizeof(adj_str) / sizeof(adj_str[0]); // analog read of multiple linear position's 
  int max = adj_str[0];
  int min = adj_str[0];
  //int sum = 0;
    for (int i = 1; i < quantity; ++i)
      {
       const int value = adj_str[i];
       if (value > max) max = value;
       if (value < min) min = value;
       //sum = sum + value; 
      }
         int dwn_max = (max);
         int up_min = (min);
         int adj_str_dif = dwn_max - up_min; // example = if adj_str_dif > 6 then run level();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM