CIS105 Fall 2003 Lab10

Lab 10, CIS105, Fall 2003

Lab 10, CIS105, Fall 2003

  1. Goals
  2. In this lab, you will become familiar with

  3. Reference Materials
  4. Section 5.9 of Deitel, about generating random numbers for simulating rolling of dice (this section starts on p. 156. You should read at least up to the end of section 5.9 on p. 161. Section 5.10 might be helpful also.

    Chapter 6 in Deitel, for arrays, especially p. 211, a program that is similar to the one you are writing in this lab..

  5. Getting Started
  6. In this section, you are to read through some code (available in your text, or online), and try to understand it. You might want to download the code, compile it and run it to understand what it is doing, but there is nothing to turn in for this section.

    Read over section 5.9 in Deitel, and look through the programs in figures 5.7, 5.8 and 5.9.
    If you don't have your textbook with you, these programs are available online, and you can down load them and read through them, paying attention to the comments to try to understand what the programs are doing and how they work.

    Program 5.7 illustrates how we can simulate the rolling of a six sided die (one half of a pair of dice.) There are six possible outcomes: 1, 2, 3, 4, 5, and 6. Try to understand how the rand() function works with the mod operator to generate a random number between 1 and 6. The rand() function returns a random integer. We then apply "mod 6" to this random integer to return a value between 0 and 5. We then add 1 to get a value between 1 and 6.

     

    Program 5.8 (p 157-158) simulates 6000 rolls of the die. If our simulation if fair, you'd expect about 1000 rolls to come out with each side of the die, over the long term. If the process is truly random, there won't necessarily be exactly 1000 rolls of each die, but pretty close to it.

    Now, program 5.8 (p. 157-158)uses 6 different variables, and a 6-way switch statement to count up how many times each roll occurs. This is ok, but it is not a solution that would scale up easily. Compare this with program 6.9 (p. 211), which solves the same problem using an array. Notice how much easier it would be to extend this program to more numbers than just 1-6. That's exactly what we are going to do.

  7. lab10_userid.c
  8. For lab10_userid.c, you are to write a program that will simulate the roll of two dice, and count the number of times each roll of the dice occurs. For two dice, there are 11 different possibilities, from 2 up to 12. For convenience, you can use an array with thirteen elements [0] through [12], and just ignore elements [0] and [1]. That way, the index of the array represents the roll. Each time you roll a 7, for example, you will increment element [7] of the array. In general, when you roll j you want to increment element [j] of the array.

    You'll use a for loop to roll the dice 36000 times. When you are all finished, report your results in a table like the following:

    roll       frequency
    ----       ---------
    2             1002
    3             1997
    4             3007
    5             3990
    . . . 
    11            2003
    12             990
    

     

    The number 36000 should be defined in your program as a #define, e.g.

    #define NUMROLLS 36000

    My suggestion is that you initially make NUMROLLS be 36 while you are testing and then increase it later after you have confidence your program is working. That way, if you want to include debug output in your program to test it, the size of the output will be manageable.

    To turn in: a script (lab10.txt) showing a listing, compile and run of your program. Also submit your .c file. (10 point deduction if you don't). Remember that the program name should be lab10_userid.c, where userid is your strauss login name (e.g. lab10_pconrad.c, or lab10_johndoe.c).

     

  9. For an A grade: lab10a_userid.c
  10.  

    For an A grade: instead of rolling each die with a line of code such as this one from Deitel's program 6.9:

    face = 1 + rand() % 6;

    instead, write a function matching the prototype:

    int randIntBetween(int start, int end);

    This function should be capable of returning a random integer between the the start integer, and the end integer. Then, your line to roll the die becomes a call to this function, as:

    face = randIntBetween(1,6);

    To get the A grade, you must do two things with this function:

    x = randIntBetween(-10,10);

    y = randIntBetween(50,59);

    Note that it is not sufficient just to call your randIntBetween function "once" just to test it. Coming up with a reasonable test procedure (on your own) is the other half of the extra 10 points.


    Phillip T Conrad
    Last modified: Fri Dec 12 14:26:26 EST 2003