ENGINEERING H192 DAILY ASSIGNMENT E9

advertisement
ENGINEERING H192
DAILY ASSIGNMENT E9
As you’ve seen, the switch statement can be used to implement menu-like transfers. In addition,
the switch selection structure in C/C++ can be used as a convenient way to count the occurrences
of integral data values. In this assignment you are to write a complete C/C++ program that uses a
while loop, a for loop, and a switch structure to generate and count a user specified number of
random integers between 0 and 9 inclusive. The while loop will control the overall execution of
the program, the for loop will be “nested” inside the while loop and will be used to generate and
count the random integers, while the switch structure, which will be nested inside the for loop,
will do the actual counting.
Your program should (1) execute indefinitely until the users decides to quit (2) prompt the user
for the number of random integers to generate, with “0” indicating “quit”, (3) inform the user
when an entry is invalid (less than zero) and ignore that entry, (4) keep track of each random
number occurrence, and (5) display the results of how many occurrences of each value (0 through
9) there were. It is in the fourth step that the switch selection structure will be used.
You will need to use the random number generator, rand ( ) and the modulus operator “%”, in
order to generate random integers between 0 and 9. Additionally, in order to “seed” the random
number generator you will need to execute the following statement ONCE in your program prior
to any calls to rand ( ):
srand (time (NULL));
The use of rand ( ), srand ( ), and time ( ) requires inclusion of the <stdlib.h> and <time.h>
header files.
Once the program is running correctly, submit a copy of the source code with this sheet as well as
sample output from your program. You should test your program several times by generating
large numbers of random integers and verifying an even distribution across the ten bins.
Name__________________________Instr._________Room_______Seat______Hour______
Download