The task is as follows:
Enter an integer a and output the sum of squares of all numbers from 1 to a with a step of 0.1. (1 ^ 2 + 1.1 ^ 2 + 1.2 ^ 2 + ... + a ^ 2)
The solution algorithm is as follows:
result := 0 while (a >= 1) do begin result := result + a * a; a := a - 0.1; end; At the end of the program, the result should be:
При a = 1.1 result = 2.21 При a = 3 result = 91.7 The variable a , in any case, at the end of the program must be equal to 0.9 .
The problem is as follows. When a = 1.1 everything is correct, but starting from 1.2 and further, the program produces as a result 1 less than necessary: a = 3; result = 90.7 a = 3; result = 90.7 , at the same time, at the end of the program, a = 0.9(9) .
If the algorithm is modified: the variable a is multiplied by 10 after input, and then it is not a cycle that squares the expression a / 10 , but everything starts to work correctly (the second line in the cycle will be: a := a - 1 ).
What could be the problem?