Examples

  1. 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.
    1. Initialize two variables, count and seven to 0.
    2. For each of 100 rolls:
      1. Increment the value of count.
      2. Randomly assign a value from 1 to 6 to each of the dice.
      3. Add the values of the dice and save as sum.
      4. If sum equals 7, increment the value of seven.
    3. Divide the final value of seven by the final value of count.

    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.