7.6 Cross Country Trip Simulation7 Assignments7.4 Character processing7.5 Bowling program

7.5 Bowling program

 

Write a program that reads a data file and uses an array to figures out the bowling scores. In bowling, 10 pins are set up for each frame and the player gets two chances to knock them down. If the player knocks down less than 10 ten pins down with the two balls his/her score for that frame is the total number of pins knocked down. If the player knocks down all of the pins with two balls, (a spare, marked with a "/") the player gets 10 points plus the number of pins knocked down with the next ball. If the player knocks down all of the pins with one ball, (a strike, marked with an "X") the player gets 10 points plus the number of pins knocked down with the next two balls.

At the beginning of your program printout the following:

  1. Your name
  2. CIS265 Louis Taber
  3. Lab: 7.3: Bowling.

Your input data will be:

  1. Players name
  2. Number of pins knocked down with a ball.
  3. -1 For end of game (next record new players name)
  4. -2 For end of data file

For each game print out all ten frames. For each frame print out the following:

  1. the number of pins knocked down for each ball.
  2. If the frame was a strike or a spare.
  3. The running score for the player.

For example: (Print error messages when you find the error.)

Name:  John Doe
Frame  1:   Ball 1: 10   Ball 2: none  Strike    Score:   20
Frame  2:   Ball 1:  5   Ball 2:  5    Spare     Score:   36
Frame  3:   Ball 1:  6   Ball 2:  0              Score:   42
Frame  4:   Ball 1: 10   Ball 2: none  Strike    Score:   54
Frame  5:   Ball 1:  0   Ball 2:  2              Score:   56
Frame  6:   Ball 1:  0   Ball 2:  1              Score:   57
Frame  7:   Ball 1:  4   Ball 2:  5              Score:   66
Frame  8:   Ball 1:  4   Ball 2:  6    Spare     Score:   83
Frame  9:   Ball 1:  7   Ball 2:  3    Spare     Score:  103
Frame 10:   Ball 1: 10   Ball 2: none  Strike    Score:  133
Extra Ball 1: 10     Extra Ball 2: 10

You will also need to print out your results using the "traditional" format for a bowling score sheet.

Name:  John Doe

  ( Error message will go here -- if there are any )

___1___2___3___4___5___6___7___8___9__10___
| |X|5|/|6|0| |X|0|2|0|1|4|5|4|/|7|/|X|X|X|
| 20| 36| 42| 54| 56| 57| 66| 83|103|  133|   
-------------------------------------------

Remember that after the last frame, up to two additional balls may be played. (Two for a strike and one for a spare.) Your program also needs to check for errors in the input data: The types of errors your program needs to look for are:

Run your program with the test data available at: Test your program with the following sample data sets. The first one has (hopefully) no errors. It is available by anonymous FTP at
ftp://lt.tucson.az.us/pub/c/bowling.dat.

The second one has lots of errors, to check your error handling code. It is available by anonymous FTP at
ftp://lt.tucson.az.us/pub/c/bwerror.dat.

An example program for reading the data file is available at:
ftp://lt.tucson.az.us/pub/c/read-bowling.c Here is the example program:

/* Louis Taber 3/26/2001
 *
 * --  Read bowling info
 *
 * Third example program for fscanf()
 * example of fscanf, errno, perror
 *                               */

#include <stdio.h>
#include <errno.h>

#define TRUE 1

int extern errno;

int main(int argc, char *argv[])
{
FILE *fp;
int score;
int balls;
char bowler[200];
char blanks[200];

if( argc == 1 )   /* No arguments -- use std input */
  {
  fp= stdin;
  }
else
  {
  if((fp = fopen( argv[1], "r")) == NULL)
    {
    printf(" Error(%d): %s\n", errno, strerror(errno)); 
    printf("%s(line %d): fopen error: %s\n",
           __FILE__, __LINE__,  argv[1]);
    return 1;
    }
  }


while( TRUE ) 
  {
  fgets(bowler, sizeof(bowler), fp);
  balls = 0;
  while( TRUE )
    {
    fscanf(fp, "%d", &score );
    if( score < 0 ) break;
    balls ++;
    printf(" %02d", score);
    }
  fgets(blanks, sizeof(blanks), fp);
  printf("\nballs: %4d  %s\n", balls, bowler);
  if( score == -2 ) break;
  }

fclose(fp);
return 0;
}

Mark your output two printouts and your listing with:

Please turn your lab in to me, Louis Taber, during class, or slide it under the door of Santa Rita Building room A-115 (my office).


Instructor: Louis Taber, louis.taber.at.pima at gmail dot com (520) 206-6850
My web site in Cleveland, OH
The Pima Community College web site

7.6 Cross Country Trip Simulation7 Assignments7.4 Character processing7.5 Bowling program