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.

Find maximum element:

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

Find the minimum:

``````int m  = {1, -1, 0, 4, 2};
int min = m ;
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.

The `minmax_element `algorithm from the STL will help you.

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.