home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c021 / 7.img / EXAMPLES.ZIP / GAME.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-04  |  1.4 KB  |  52 lines

  1. /* GAME.C--Example from Chapter 4 of Getting Started */
  2.  
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <conio.h>
  6. #include <time.h>
  7.  
  8. #define DODGERS 0
  9. #define GIANTS 1
  10.  
  11. void main(void)
  12. {
  13.    int scoreboard [2][9];    /* An array two rows by nine columns */
  14.    int team, inning;
  15.    int score, total;
  16.  
  17.    randomize();              /* Initialize random number generator */
  18.  
  19.    /* Generate the scores */
  20.    for (team = DODGERS; team <= GIANTS; team++) {
  21.       for (inning = 0; inning < 9; inning++) {
  22.          score = random(3);
  23.          if (score == 2)     /* 1/3 chance to score at least a run */
  24.             score = random(3) + 1;   /* 1 to 3 runs */
  25.          if (score == 3)
  26.             score = random(7) + 1;   /* Simulates chance of a big
  27.                                         inning of 1 to 7 runs */
  28.          scoreboard[team][inning] = score;
  29.       }
  30.    }
  31.  
  32.    /* Print the scores */
  33.    printf("\nInning\t1   2   3   4   5   6   7   8   9   Total\n");
  34.    printf("Dodgers\t");
  35.    total = 0;
  36.    for (inning = 0; inning <= 8; inning++) {
  37.       score = scoreboard[DODGERS][inning];
  38.       total += score;
  39.       printf("%d   ", score);
  40.    }
  41.    printf("   %d", total);
  42.  
  43.    printf("\nGiants\t");
  44.    total = 0;
  45.    for (inning = 0; inning < 9; inning++) {
  46.       score = scoreboard[GIANTS][inning];
  47.       total += score;
  48.       printf("%d   ", score);
  49.    }
  50.    printf("   %d\n", total);
  51. }
  52.