Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Random numbers and Nested Loops Write a program which will flip a coin 1,000 tim

ID: 649022 • Letter: R

Question

Random numbers and Nested Loops Write a program which will flip a coin 1,000 times - obtaining a heads or tails result. Count how many heads and how many tails you get out of 1,000 coin tosses. In order to do this, you should generate a random number (1 or 2). Let 1 represent heads and let 2 represent tails. Count and display the number of heads and tails. Then, ask the user if they want to flip the coin 1,000 more times. For this assignment, please submit EITHER the pseudo code OR the flowchart (Visio file). Also, submit the QBasic (.bas) file. Put your files into a compressed folder and submit in Blackboard. Sample output:

Explanation / Answer

PSEUDOCODE

1) Let head=0,tail=0

2) n=Rand(1,2)

3) IF n=1 THEN head=head+1

4) ELSE tail=tail+1

5) Repeat steps 2 to 4, 999 times

6) Display total no. of heads

7) Display total no. of tails

8) Ask whether to continue flipping 1000 more times

9) If choice is yes repeat steps 2 to 8

QBASIC PROGRAM

CLS
PRINT "This program flips a coin 1,000 times."
PRINT " "
head=0
tail=0
DO
FOR I=1 TO 1000
   n=Rand(1,2)
   IF N=1 THEN
       head=head+1
   ELSE
       tail=tail+1
   END IF
NEXT I
PRINT "Heads =";head
PRINT "Tails =";tail
PRINT " "
PRINT "Do you want to flip 1,000 more times? (Y/N)? ", Choice$
LOOP WHILE Choice$="y" OR Choice$="Y"