04-24-2011, 04:43 PM
Dear Student,
This program is just like a game where a Die is rolled. We know that a Die consists of 6 numbers from 1 to 6 on its each different face. Here in this program, the objective is to find the number of times each face will come up when that die is rolled 600000 times. Let us look at the logic behind the loop which you are finding dificult to understand.
In the loop declaration, you can see that loop limit is less than equal to 600000, which means that this loop will be executed 600000 times. In other words, this limits describes the number of times the Die will be rolled. Now lets come to second statement of the above code. We know that a die can only have 6 faces. when the 1+rand()%6 is executed, it always returns a value less than or equal to 6 becuase this value will represent the value of each face of the die.
This program is just like a game where a Die is rolled. We know that a Die consists of 6 numbers from 1 to 6 on its each different face. Here in this program, the objective is to find the number of times each face will come up when that die is rolled 600000 times. Let us look at the logic behind the loop which you are finding dificult to understand.
Code:
for (roll=1;roll<=600000;roll++)
frequency[1+rand()%6]++;In the loop declaration, you can see that loop limit is less than equal to 600000, which means that this loop will be executed 600000 times. In other words, this limits describes the number of times the Die will be rolled. Now lets come to second statement of the above code. We know that a die can only have 6 faces. when the 1+rand()%6 is executed, it always returns a value less than or equal to 6 becuase this value will represent the value of each face of the die.
