Examples
- Here is a pseudo-code algorithm for using a computer to simulate 100 rolls of dice, and to determinine the number of times that a seven is rolled on two die objects if the dice are rolled 100 times.
- Initialize two variables,
count
andseven
to 0. - For each of 100 rolls:
- Increment the value of
count
. - Randomly assign a value from 1 to 6 to each of the dice.
- Add the values of the dice and save as
sum
. - If
sum
equals 7, increment the value ofseven
.
- Increment the value of
- Divide the final value of
seven
by the final value ofcount
.
Notice the clear single action code-like descriptions of each step in the algorithm. This language helps us when we are ready to implement the algorithm in code. Because each step is small and specific, it will be easy to implement and debug that step. This makes our final solution easier to maintain.
The words in bold-face type are words that indicate a change in the execution order of our list of steps. We will discuss how to implement if and for statements in other lessons.
- Initialize two variables,