CS 2213/2211
 Advanced Programming
 Spring 2005

 Recitation 2
 C I/O and Random Numbers
    Week 2: Jan 25-27
 Due (on time): 2005-02-01  23:59:59
 Due (late):        2005-02-06  23:59:59

Recitation 2 should be submitted following directions at: submissions with deadlines
  • 2005-02-01  23:59:59 (that's Tuesday, 1 February 2005, 11:59:59 pm) for full credit.
  • 2005-02-06  23:59:59 (that's Sunday, 6 February 2005, 11:59:59 pm) for 75% credit.


Introduction: This recitation works on four areas:


The "craps" program, craps.c: You will be modifying and adding to the following program:

C Craps Program: craps.c Result of run

/* craps: simulate play of several
 *   games of craps. Report results.
 */
#include &stdio.h>
#include &stdlib.h> /* for rand */
#include &time.h>   /* for srand */
#define WIN 1
#define LOSE 0

/* prototypes */
int roll(void);
int craps(int* dice);

int main() {
   int dice; /* variable return value */
   int result; /* WIN/LOSE from craps */
   int i; /* loop variable */
   srand(time(NULL)); /* initialize RNG */
   printf("The \"dice value\" below\n"
          "is either:\n" 
          " o immediate win/loss value\n" 
          " o Point value otherwise\n\n");
   for (i = 0; i < 40; i++) {
      int result = craps(&dice);
      if (result == WIN) printf("WIN, ");
      else printf("LOSE,");
      printf(" dice value: %i\n", dice);
   }
}

int roll(void) {
   return 
     (int)(6.0*(rand()/(double)RAND_MAX) + 1.0);
}

int craps(int* dice) {
   int dice2;
   *dice = roll() + roll();
   if (*dice == 2 || *dice == 3 || *dice == 12)
      return LOSE; /* Immediate loss */
   else if (*dice == 7 || *dice == 11)
      return WIN; /* Immediate win */
   else {   /* point: 4, 5, 6, 8, 9, or 10 */
      while (1) {  /* keep rolling */
         dice2 = roll() + roll();
         if (*dice == dice2)
            return WIN;; /* made point, won */
         if (dice2 == 7)
            return LOSE; /* lost with a 7 */
      }
   }
}
% cc -o craps craps.c
% craps
The "dice value" below
is either:
 o immediate win/loss value
 o Point value otherwise

LOSE, dice value: 9
WIN,  dice value: 7
WIN,  dice value: 4
LOSE, dice value: 10
WIN,  dice value: 7
LOSE, dice value: 5
LOSE, dice value: 6
LOSE, dice value: 3
LOSE, dice value: 4
WIN,  dice value: 9
WIN,  dice value: 5
WIN,  dice value: 11
LOSE, dice value: 3
WIN,  dice value: 6
LOSE, dice value: 9
WIN,  dice value: 7
LOSE, dice value: 2
LOSE, dice value: 5
LOSE, dice value: 5
LOSE, dice value: 3
WIN,  dice value: 7
LOSE, dice value: 3
LOSE, dice value: 8
WIN,  dice value: 7
LOSE, dice value: 9
LOSE, dice value: 2
LOSE, dice value: 12
WIN,  dice value: 10
LOSE, dice value: 5
WIN,  dice value: 10
WIN,  dice value: 7
WIN,  dice value: 11
LOSE, dice value: 5
WIN,  dice value: 8
WIN,  dice value: 7
WIN,  dice value: 6
LOSE, dice value: 10
LOSE, dice value: 3
LOSE, dice value: 8
LOSE, dice value: 8


What to do: The expanded and modified program will be recording and printing statistics about craps games, in a way somewhat similar to the earlier program: dice program.

  1. Use scanf to read an integer value for the total number of craps games, for example: 36000000.
  2. Replace the function roll above with a function that also returns a random integer from 1 to 6 inclusive, but that uses a hand-written RNG, rather than the library one. You can use one of the RNGs presented in the Random Numbers write-up. Initialize this RNG using time(NULL) just as above.
  3. Make use of the function craps above without changing it at all. The objective is for you to practice using a reference parameter, such as the one used by this function. (The scanf function also has a reference parameter.) As the main function above illustrates, each call to craps returns either a 1 or a 0 indicating either a win or a loss of the game. In addition, the reference parameter returns a sum of dice which is either the sum that gave an immediate win, or the sum that was a point, either made (win) or not made (loss).
  4. Add the following to the main function above:


What you should submit: Refer to the submissions directions and to deadlines at the top of this page. The text file that you submit should first have Your Name, the Course Number, and the Recitation Number. The rest of the file should have the following in it, in the order below, and clearly labeled, including at the beginning the appropriate item letters: a, b, c, etc.

 Contents of email submission for Recitation 1:

Last Name, First Name; Course Number; Recitation Number.

a. Listing of your C program file, with the required changes and additions.

b. Result of running this program.

c. Answer the question: Consider the second program listing in the write-up about Random Numbers (the listing titled "C RNG Program"). Why does the C++ compiler on the Sun machines produce a different answer than the results using other compilers, even though the program is exactly the same? (This is tricky. Please don't ask anyone for the answer, neither students nor faculty.)


Key ideas: C has a non-type-safe method of I/O that is nevertheless useful and powerful. Reference parameters (not available in Java) allow one to return more than one value from a function. Random number generation is not dependent on any particular programming language.


Revision date: 2005-01-18. (Please use ISO 8601, the International Standard.)
Copyright © 2011, Neal R. Wagner. Permission is granted to access, download, share, and distribute, as long as this notice remains.