(C++) For loop operator. Typical tasks


Let's try to write a program to solve the following problem:
Find the sum of all integers from 100 to 500.

When solving this problem, difficulty arises in finding the amount. If we just write the result of the addition into the variable s, for example, as

s=100+101+102+103+...+500

it will take a lot of time for the recording itself, because the computer will not understand how to use the ellipsis in arithmetic terms and we will have to write all the numbers from 100 to 500 in this amount. And the value of such a program will be insignificant. Especially if we want to change our numbers and take a different range.

What do we do?

If we pay attention to the record above, then we constantly use the addition of "+".
You can try adding numbers to the variable s gradually. For example, using such a record
s=s+i;
what we did here:
1) on the right, we set the calculation of the expression s + i, that is, we take the value of the variable s, which we now have in memory, and add the value of the variable i to it
2) on the left we put the name of the variable s, that is, the entire result of the calculation on the right will be stored in this variable, so we change the value of the variable s.

Where to get numbers from our range?

The numbers from 100 to 500, which belong to our range, must alternately fall into the variable i. And this can be done using the for loop known to us.
For example, in this way
s=0;          //at the beginning it is necessary to reset the variable s, so that in the first step the number 100 is added to zero, and not to what is in memory!
for ( i = 100; i<=500; i++)  //the header of the loop in which the variable i changes its value from 100 to 500 in steps of 1
    s = s + i;   // body of the loop, in which we gradually add the value of the variable i to the variable s
                  // and save the result back to the variable s
This solution is very similar to calculating the amount in steps
 s = 0 + 100 = 100
 s = 100 + 101 = 201
 s = 201 + 102  = 303
и т.д.

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.

Task:  Given N numbers. Find the second largest maximum element of the sequence..

This task has two interpretations.
For example, if we are given a set of numbers: 10 15 20 35 14 35 10, then what should be the answer?
By "second largest maximum element" or simply "second maximum", we can understand:
1) the value that would have stood in the penultimate place if we had all the values ​​arranged (sorted) in non-decreasing order (each next one is greater than or equal to the previous one). Then for the considered set of numbers the answer will be the value 35
2) the value of the element, of which only the maximum is greater. Then the answer is 20
If there is only one maximum element in the set of numbers (all the others are smaller), then both interpretations coincide, and the answer will be the same for both cases, otherwise the answer will be different.

Consider the first case (the algorithm will be written in pseudocode).
To find the answer, we will use two variables:
1) maximum1 - maximum value (first maximum)
2) maximum2 - second maximum (our answer)
If the range of variation of the values is known, then as the initial values we take a number that is obviously less than the lower boundary of the range (for example, for a range from -1000 to 1000 - take the number -1001)
If the range of values is unknown, then the first two input numbers can be written in the initial values of the variables maximum 1 and maximum 2, and then compare these two variables
input N //amount of numbers
input a, b
maximum1 = а
maximum2 = b
if b > a then
    maximum1 = b
    maximum2 = a
Next, we consider all the other elements (the first 2 have already been viewed, so we will start from the 3rd)
for i <-- 3 to n
    input а
    if а > maximum1 //a value greater than the maximum1
      then 
        maximum2 = maximum1 //the former first high will become the second
        maximum1 = а  //the first high will be a new element
      else
       // next element no more than maximum1
       // it must be compared with a value of maximum 2
        if а > maximum2 then
            maximum2 = а  // accept it as a new value maximum 2
                          // maximum1 in this case does not change
Try to implement this algorithm yourself