Let’s talk about the WSQ06, maybe the hardest in this partial (actually it is not hard, but you have to search a lot to understand it).

Here the image with the program and the compiler running it:

wsq06

Gaze at the program, stare at it as they are no other thing to observe.

I am going to explain it quickly, but if you still having doubts, search for yourself, and if it is not clear yet, ask me and then I am going to help you.

We include <stdlib> and <time> (for this example is cstdlib and ctime, but it depends on the compiler), because we need to use the functions rand(), srand() (from <stdlib>) and time() (from <time>).

Rand() is supposed to create a random number, but it is always the same value, and that is why we have to include sradn() (refers to seed random), because from that function the rand() takes like a reference for create a number. In this case, between the parenthesis of the srand() function, we put the time() function, and in this way, the random number will be generated based on a value given by the current time of the computer.

It is important to put the “%” after the rand(), because in this way you are telling the function to generate a number into a range. In this case we want to set the range between the number 1 and 100, after the “%” sign, we put the number of spaces that we are going to use, plus the initial number. For the first value, took the initial number too, that means that the final value that you want to put is the upper bound plus one, and then “+” before the first value.

There is an error in the program of the image, try to  find it.

That’s all for this WSQ, here is the code (the correct one): https://github.com/hrglez/TC1017/blob/master/WSQ06%20-%20Random%20Number

CC BY 4.0 WSQ06, Guess them damn number by hrglez is licensed under a Creative Commons Attribution 4.0 International License.