Unit #5

 

 

 

 

Problem #1: Player A and Player B play the game of multiplication as specified by the rules below:

 

Step 1: A random integer value, N in [2, 1000] is selected.

Step 2: An integer variable, P is assigned the value of 1.

Step 3: Player A always starts first.

Step 4: Player A randomly generates an integer value in [2, 9] and multiplies the value with P.

Step 5: If the value of P exceeds N, Player A wins. Game over. If the value of P is less than or equal to N, proceed to Step 6.

Step 6: Player B randomly generates an integer value in [2, 9] and multiplies the value with the value of P computed in Step 4.

   If the value of P exceeds N, Player B wins. Game over.

   If the value of P is less than or equal to N, continue Steps 4-6 until the game is completed.

 

Write a program to implement the multiplication game as per the above rules.

 

Program Code

 

Example run:

N = 639

A randomly selects 9

P = 9

B randomly selects 4

P = 36

A randomly selects 7

P = 252

B randomly selects 8

P = 2016

B Wins!

 

 

 

 

Problem #2: In this program, you will simulate the rolling of two dice. Use the random number generator to roll the first die and again to roll the second die. The sum of the two values should then be calculated. Your program should roll the dice 10000 times. Use an integer array of the appropriate size to tally the number of times each possible sum appears. Display the results.

 

Program Code

 

Example run:

Dice Roll Simulation

2 was rolled 263 times

3 was rolled 592 times

4 was rolled 817 times

5 was rolled 1120 times

6 was rolled 1389 times

7 was rolled 1597 times

8 was rolled 1363 times

9 was rolled 1103 times

10 was rolled 824 times

11 was rolled 619 times

12 was rolled 313 times