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
  • 2005-01-25  23:59:59 (that's Tuesday, 25 January 2005, 11:59:59 pm) for full credit.
  • 2005-01-30  23:59:59 (that's Sunday, 30 January 2005, 11:59:59 pm) for 75% credit.


Introduction: This recitation asks you to start with a non-trivial C program. Then download it, use a Makefile to compile and lint the program and to execute it. Next you should use vi to modify the program, and repeat the compile, lint, execute steps.


The "dice" program, dice.c: Here is the program for you to work on:

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);
}


What to do:
  1. Study the lecture notes about Unix: Unix Basics.
  2. Using the command line in a terminal window, create a new directory for this assignment, say rec1.
  3. Put the program above into your own file dice.c. (Either just copy and paste from the listing above, or use this copy: dice.c.)
  4. Study the writeup: lint and make. Create a Makefile for the program dice.c.
  5. Use this Makefile and the make utility to check the program using the lint utility. (You will need to run lint on the Sun system.)
  6. Use the same Makefile to compile and execute this program, using either cc or gcc as the compiler. When you execute, you should enter a fairly large number for the number of rolls, such as 1000000.
  7. Study the dice program and how it works by looking at the presentation of a line-by-line comparison between C and Java for this program: Dice Program in C and Java.
  8. Study the writeup about the vi editor: vi Editor. Use vi to make the following changes to the program:

    1. Change the program so that it records the results of rolling three dice instead of two.
    2. You will need to make various modifications to the program, calling the new program: dice3.c. In particular, the possible sums will range from 3 to 18, so you need to change arrays and for loops. The actual frequencies with which the sums from 3 to 18 come up are given in the following table:
        
           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};
        

    3. Repeat the make, lint, compile, and run as with the original program.

  9. Make up a single text file for submission, as described in the next section.


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. 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.


Key idea: You have been studying the process of program creation, checking with lint, compiling, and execution, all using C and a command line Unix environment


Revision date: 2005-01-06. (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.