|
CS 2213/2211 Advanced Programming Spring 2005 Recitation 1 Introduction to C, Unix, and vi Week 1: Jan 18-20 Due (on time): 2005-01-25 23:59:59 Due (late): 2005-01-30 23:59:59 |
Recitation 1 should be submitted
following directions at: submissions with deadlines
|
C Dice Program: dice.c | |
---|---|
/* dice: Gather statistics about rolling * two dice. A Monte Carlo simulation using * the random number generator rand() */ #include <stdio.h> /* for the input/output */ #include <stdlib.h> /* for rand and srand */ #include <time.h> /* for time */ /* function prototypes */ void initarray(int d[]); void printarray(int d[]); void printstar(int n); void fancyprint(int d[]); int roll(void); /* global variable */ int numrolls; int main(void) { int d[13]; /* 2-12 hold num of rolls */ int i; /* loop variable */ int starttime = time(NULL); /* clock time */ srand((long)starttime); /* initialize RNG */ printf("Enter # of rolls ---> "); scanf("%i", &numrolls); /* need & on var */ initarray(d); printf("Total rolls: %1d\n\n", numrolls); for (i = 0; i < numrolls; i++) /* simul. */ d[roll() + roll()]++; printarray(d); fancyprint(d); printf("Elapsed time: %ld seconds\n", time(NULL) - starttime); return 0; } /* initarray: initialize statistic array */ void initarray(int d[]) { int i; for (i = 2; i < 13; i++) d[i] = 0; } | /* printarray: print each num of rolls */ void printarray(int d[]) { int i; double e[] = {0, 0, /* frequencies */ 1.0/36.0, 2.0/36.0, 3.0/36.0, 4.0/36.0, 5.0/36.0, 6.0/36.0, 5.0/36.0, 4.0/36.0, 3.0/36.0, 2.0/36.0, 1.0/36.0}; printf("Sum Times Frequency"); printf(" Exact Diff\n\n"); for (i = 2; i < 13; i++) printf("%2d %7d %11.7f %10.7f %8.4f\n", i, d[i], (double)d[i]/numrolls*100.0, e[i]*100.0, ((double)(d[i]) - e[i]*numrolls)/numrolls*100.0); } /* printstar: print n stars */ void printstar(int n) { while (n > 0) { printf("*"); n--; } } /* fancyprint: print bar graph */ void fancyprint(int d[]) { int i; printf("\n"); for (i = 2; i < 13; i++) { printf("Sum:%3d |", i); printstar(300*d[i]/numrolls); printf("\n"); } printf("\n"); } /* roll: simulate rolling a die */ int roll(void) { return (int) (6.0*(rand()/(double)RAND_MAX) + 1.0); } |
double e[] = {0, 0, 0, /* array of frequencies */ 1.0/216.0, 3.0/216.0, 6.0/216.0, 10.0/216.0, 15.0/216.0, 21.0/216.0, 25.0/216.0, 27.0/216.0, 27.0/216.0, 25.0/216.0, 21.0/216.0, 15.0/216.0, 10.0/216.0, 6.0/216.0, 3.0/216.0, 1.0/216.0};
Contents of email submission
for Recitation 1: Last Name, First Name; Course Number; Recitation Number. a. Your Makefile for the program dice.c. b. Result of running lint for the same program, using the Makefile. c. Result of running the compiled program, using the Makefile. Enter a fairly large number for the number of rolls, such as 1000000. d. C source code for the rewritten program to handle the sum of the spots of three rolls, now called dice3.c. e. Result of running lint for this program, using a slightly altered Makefile. f. Result of running the compiled program, using the slightly altered Makefile. Enter a fairly large number for the number of rolls, such as 1000000.
|