K
K
Katya2019-02-13 15:41:59
C++ / C#
Katya, 2019-02-13 15:41:59

How can you calculate the maximum and minimum values ​​of an array?

I'm currently learning about arrays and came across such a problem as the minimum and maximum value of an array. As a result, based on various examples, I came up with the following code (part, c[i] is an array):

int max = 0;
int min = 0;

int max_index = 0; 
int min_index = 0;

for(int i = 0; i < 20; ++i)
{
   if(max < c[i])
   {
      max       = c[i];
      max_index = i;
   }

   if(min > c[i])
   {
      min       = c[i];
      min_index = i;
   }
}

As a result, the maximum value is displayed correctly, and the minimum element is 0, which is not in the array itself. Not only is the minimum value wrong, but I still can't understand the essence of the above part of the code. How is the maximum and minimum value obtained? I can’t understand it at all, but I just don’t want to memorize it. I would be grateful for an answer.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeny Shatunov, 2019-02-13
@HatunaLM

In short, you don't need to store the minimum and maximum values ​​separately, because they are always accessible by index in the array. Another thing is that you don't have these indexes before you start iterating through the array, you simply have nowhere to get them from. This moment determines the correct initial state in solving your problem. The absence of this moment also leads to the fact that your code is now working incorrectly.
You only need the indexes of those elements that you consider to be the minimum and maximum.

size_t min_value_index = 0;
size_t max_value_index = 0;

I choose initialization in 0order to initially designate the first element of the array as both the minimum and the maximum at the same time. This is how I define the initial state of the algorithm.
The next step is to pass through the array.
for( size_t index = 1; index < stream_length; ++index )
{
   // ...
}

I don't need to compare the element at index zero with itself, because I initially already selected it, so you can start traversing the array from the next element.
Now we need to write down the condition for choosing the minimum and maximum values.
for( size_t index = 1; index < stream_length; ++index )
{
   if( stream[ index ] < stream[ min_value_index ] )
   {
      min_value_index = index;
   }
   
   if( stream[ index ] > stream[ max_value_index ] )
   {
      max_value_index = index;
   }
}

When writing conditions, it is very important to follow the logic. It is necessary to write in such a way that the expression is read as easily as possible, using words in the names and the order in which the operations are written.
Here you can see that if the value of the current index is less than the memorized minimum, then you need to remember the current index as the index of the minimum value. And for the maximum value exactly the same.
At the end of the loop, the min_value_indexindex of the minimum value of the array will be guaranteed to lie in, and the max_value_indexindex of the maximum value will lie in the index.
How does this code work.

V
Vasily Melnikov, 2019-02-13
@BacCM

The most correct is:

cout << *min_element(begin(c), end(c)) << endl;
cout << *max_element(begin(c), end(c)) << endl;

Can it be like this
std::partial_sort(begin(с), begin(с) + 1, end(с));
auto m = с.front();
std::partial_sort(begin(с), begin(с) + 1, end(с), greater<long>());
auto M = с.front();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question