📜 ⬆️ ⬇️

A little C / C ++ task to warm up.

I propose to solve the problem. As an option, you can offer to solve it to applicants during interviews ( in addition ). The task is very easy to solve, but it creates a pause for people who have not met with it before or who have not tried to analyze the properties of loop operators when studying or after.


Using only one variable of unsigned char type (8-bit by condition), exactly one loop operator and not using the conditional if statement and the conditional ternary operation (? :), create a loop of exactly 256 steps, the body of which, even if it is empty, is executed exactly 256 times. In the particular case, such a cycle can output all integers from 0 to 255 inclusive.

The solution of the problem


Hidden text
#include <iostream> int main() { unsigned int i = 0; do { std::cout << i << " "; } while (++i); return 0; } 


Interestingly, the totality of the conditions given in the problem requires the use of the do loop and does not allow to use the while or for operators. It is the application of the last two cycles that is the main error in solving this problem, leading in most cases to skipping a step (performing 255 steps instead of 256 required in conditions) or creating an infinite loop. This error is suggested by the fact that for / while loops are used much more often than do, I have somewhere met the subjective assessment of the ratio of 95%: 5%.
')
The cycle repeats the execution of two actions: checking the condition and body execution. The alternation of these actions and the cycles differ with a precondition (while) and with a postcondition (do). There is a lot in common between cycles, in particular, using the conditional if operator and one type of cycle you can always form a structure that will behave like the second type of cycle (for example, you can write do from while and if). From here and one of requirements of the task: not to use the conditional operator if. Nevertheless, the difference in the course of action gives two types of cycles a difference in properties. First, the cycle body with a precondition may not be executed even once, while the cycle body with a post-condition is always executed at least once. Secondly, in for / while loops the condition check is performed 1 time more than the body of the loop, and in do the loop body is executed as many times as the condition check or 1 time more if the body forcibly interrupts the cycle. Therefore, “exactly 256 steps” in the statement of the problem is redundant, and “the body of which, ... is executed exactly 256 times” is not. In general, the formulation “cycle of 256 steps” is vulnerable: if the condition was checked 256 times and the body was executed 255 times, is it possible to say that the cycle consists only of 255 steps? It is clear that the verification of the condition can be an operation of any complexity and perform any side effects. Previously, an intermediate restriction was formulated: a ban on the performance of the required operations of the cycle body (eg, output) when checking the condition. However, the requirement to execute the body of the cycle exactly 256 times is more stringent, specific and, in the final analysis, correct. At a more abstract level, transforming a condition into an action, then a condition sequence, where an action and a condition are connected by sequential calculation operations (,), logical operations or otherwise, actually makes the condition check not starting a loop iteration with a precondition and turns it into a post-conditional loop .

Finally, the idiom of a cycle of N steps in C / C ++ is: for (int i = 0; i <N; i ++). The property of this design is that the loop counter i takes (N + 1) different values ​​from 0 to N inclusive. Consequently, an 8-bit type that accepts exactly 256 values ​​can only be used to create such cycles from no more than 255 steps.

Source: https://habr.com/ru/post/173613/


All Articles