The program describes a one-dimensional integer array with indices from 0 to 10. Below is a fragment
The program describes a one-dimensional integer array with indices from 0 to 10. Below is a fragment of the program that processes this array: n: = 10; s: = 0; for i: = 1 to n do begin if A [i] -A [i-1] & lt; i then s: = s + i; end; At the beginning of this fragment execution, the array contained Fibonacci numbers: 1,1,2,3,5,8,13,21,34,55,89, i.e. A [0] = 1, A [1] = 1, A [2] = 2, etc. What will be the value of the variable s after the execution of this program?
The for i: = 1… loop will be executed 10 times because n: = 10.
The loop checks the difference between the values of two adjacent array elements. If this difference is less than the value of i, then the variable s is increased by the value of i.
First cycle:
i = 1; A [i] = A [1] = 1, A [i-1] = A [0] = 1;
A [1] – A [0] = 1 – 1 = 0;
A [1] – A [0] <1 (the condition is met);
s = 0 + i = 0 + 1 = 1.
Up to i = 6 inclusive A [i] – A [i – 1] <i and the variable s has increased by the sum of the first six values of i: 1 + 2 + … + 6 = 21. After the sixth cycle, the condition is not met and s is not growing.
Answer: s = 21.