11. Man is not angry - the solution


First, a random number between 1 and 6 is generated, which simulates the roll of the dice. The rand () function from the stdlib.h file is used for generation.
The maximum number is defined by the srand (time (0)) function from the same header, and the function obtained by the time function, which is associated with the system time, is passed to that function, and it is a function from the time.h header.
See also the following articlewww.cplusplus.com/reference/cstdlib/srand/The values ​​obtained by this number are then examined using the if-else if-else command, as shown in the solution.
Note: The task can also be solved using the switch command

​Solution:
​#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
int rolling;

/* initialize random seed: */
    srand (time(0));


rolling=1+ rand()%6;     // rolling is in the range of 1 to 6
    printf("Rolling the dice %d\n",rolling);
if(rolling==2 || rolling==4)
        printf("It's in the house\n");
else if(rolling==1)
        printf("It is close to the house\n");
else
        printf("He didn't move\n");

return 0;
}
​To see more examples from the field of branching in the program click on the image next to (below)​ Brunching in the programm link