for loop operator: search for the minimum number among keyboard inputs


When studying the conditional operator, we touched on the topic of finding the maximum number from several entered numbers. In the "Maximum of four numbers" problem, we used the following algorithm:
1. Assign the value of the variable Max to the first of four variables
2. If the value of the second variable is greater than the value in the maximum variable, then replace the value of the maximum variable with the value of the second variable
3. If the value of the third variable is greater than the value in the maximum variable, then replace the value of the maximum variable with the value of the third variable
4. If the value of the fourth variable is greater than the value in the maximum variable, then replace the value of the maximum variable with the value of the fourth variable

It can be seen that we compared each number (denoted by X) with the variable maximum, as follows
pseudocode:
input Х
if (maximum < X), then
  maximum = X
The main thing in this code is to determine what initial value the variable maximum will have.
Usually, when solving the problem of finding the maximum or minimum, the initial value of the variable maximum is assigned equal to the first number.
Thus, the above code must be executed 1 times less than the number of numbers (since the first number must be entered and stored as the initial value of the variable maximum).
If we have the number of numbers set from the keyboard (for example, into the variable n), then we can organize a cycle (from 2 to n), using the same variable to store the number.
Try to write the whole program yourself

If we need to find the maximum (minimum) not among all elements of the array, but only among numbers that satisfy a certain condition, then we must take into account the fact that the first element that we take as the initial value of the maximum (minimum) will not always satisfy our requirements.

For example, if we look for the maximum negative number, then having the data set: {5, -2, 4, 2, -1, -3} we will get the number 5 in the initial value of the maximum, and it is positive and more than any negative. And therefore, the condition X> maximum will always be false.

Therefore, it is not enough to add only a check for a negative number in the algorithm from the previous task, it is also necessary to take into account the fact that the first number may not satisfy the required condition (in this case, be negative)
You can fix this by adding the following condition inside the loop:
pseudocode:
if X is negative, then
   if maximum> = 0 or maximum <X, then
     maximum = X
In the indicated code, the condition maximum> = 0 allows the action maximum = A to be performed even if initially a value that obviously exceeds the others gets into the variable maximum (in the example considered by us, the value is 5).

We also note that if the range of variation of numbers is known, then as the initial value of the maximum (minimum), you can take the minimum (maximum) number from the specified range.