Home c++ Minimum and maximum array element in C++

Minimum and maximum array element in C++

Author

Date

Category

Datasets contain height values ​​for girls and boys. Determine who (boy or girl) is the tallest and smallest.


Answer 1, authority 100%

Find maximum element:

int m [5] = {1, -1, 0, 4, 2};
int max = m [0];
for (int i = 0; i & lt; 5; ++ i)
{
  if (m [i] & gt; max)
  {
    max = m [i];
  }
}

Find the minimum:

int m [5] = {1, -1, 0, 4, 2};
int min = m [0];
for (int i = 0; i & lt; 5; ++ i)
{
  if (m [i] & lt; min)
  {
    min = m [i];
  }
}

Here are the algorithms for you, check in a loop each element in the maximum (minimum), and if it is more (less) then we change the maximum (minimum) element. Google is full of examples, a popular starter theme.


Answer 2, authority 50%

The minmax_element algorithm from the STL will help you.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions