|
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
|
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 |
Output for your program |
---|
craps Enter number of games ---> 36000000 Roll Wins Losses 2 0 1000414 3 0 2001126 4 1001687 1999108 5 1600349 2398699 6 2273472 2727597 7 5997732 0 8 2272140 2727101 9 1600434 2396853 10 1000055 2003161 11 1999775 0 12 0 999991 Games: 36000000, wins: 17745644, losses: 18254050 % wins: 49.2935, % losses: 50.7057 |
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.)
|