The program now follows directly from the flowcharts. Notice that its structure parallels the flowcharts', that we have attempted to minimize side effects by passing information between components explicitly as parameters, and that information needed only by a component is kept local to that component.
#include <stdio.h>
#define TRUE 1
#define FALSE 0
main()
/*
Bowling Scores calculates and prints the game scores
for a series of bowling games. It also prints the
number of games bowled, the minimum, average, and
maximum of all game scores. The input consists, for
each game, of a sequence of roll scores. Each roll
represents the number of pins knocked down on the
corresponding roll. Its input must consist of at least
one game.
Main Variables
average - average of all game scores
gamescore - score for the current game
max - current maximum game score
min - current minimum game score
number - current number of games played
total - accumulated total of all game scores
*/
{
int gamescore;
int number = 0, max = 0, min = 300;
int newgame = TRUE;
float average;
while (newgame)
{
gamescore = gamecalc(gamescore);
update(gamescore,&number,&min,&max,&total);
printf("If you want to bowl another game enter a 1\n");
printf("If you wish to stop enter a 0\n");
scanf("%d",&newgame);
}
average = ((float)total)/number;
printf("Minimum game %d\n",min);
printf("Average game %f\n",average);
printf("Maximum game %d\n",max);
printf("Number of games %d\n",number);
}
gamecalc (gamescore)
/* calculates gamescore for the current game.
Main variables
frame - the current frame
gamescore - score for the current game
rollscore - first roll score of the current frame
r1 - succeeding roll score after rollscore
*/
int gamescore;
{
int frame, rollscore, r1;
gamescore = 0;
frame = 1;
printf("Please enter the number of pins down on this roll\n");
scanf("%d",&rollscore);
printf("Please enter the number of pins down on this roll\n");
scanf("%d",&r1);
while (frame <= 10)
framecalc(&gamescore,&frame,&rollscore,&r1);
return(gamescore);
}
framecalc(pgamescore,pframe,prollscore,pr1)
/* Calculates the framescore for the current game,
adds it to gamescore, increases frame by 1 and
updates rollscore and r1.
Main variables
pframe - pointer to the current frame
pgamescore - pointer to the score for the current game
prollscore - pointer to first rollscore of current frame
pr1 - pointer to next rollscore of current frame
framescore - score for current frame
r2 - rollscore after r1
*/
int *pgamescore,*pframe,*prollscore,*pr1;
{
int framescore, r2;
if (*prollscore == 10) /* it is a strike */
{
printf("Please enter the number of pins down on this");
printf("roll\n");
scanf("%d",&r2);
framescore = 10 + *pr1 + r2;
*prollscore = *pr1;
*pr1 = r2;
}
else if ((*prollscore + *pr1) == 10) /* it is a spare */
{
printf("Please enter the number of pins down on this");
printf(" roll\n");
scanf("%d",&r2);
framescore = 10 + r2;
*prollscore = r2;
if (*pframe < 10)
{
printf("Please enter the number of pins down");
printf("on this roll\n");
scanf("%d",pr1);
}
}
else /* it is an open frame */
{
framescore = *prollscore + *pr1;
if (*pframe < 10)
{
printf("Please enter the number of pins");
printf("down on this roll\n");
scanf("%d",prollscore);
printf("Please enter the number of pins");
printf("down on this roll\n");
scanf("%d",pr1);
}
}
*pgamescore = *pgamescore + framescore;
*pframe = *pframe + 1;
}
update(gamescore,pnumber,pmin,pmax,ptotal)
/* This prints gamescore, and updates number, min, max,
and total to reflect gamescore.
Main variables
gamescore - current game score
pnumber - pointer to current number of games
pmin - pointer to current minimum game score
pmax - pointer to current maximum game score
ptotal - pointer to total of all game scores
*/
int gamescore, *pnumber, *pmin, *pmax;
int *ptotal;
{
printf("gamescore \n");
printf("%d\n",gamescore);
*pnumber = *pnumber + 1;
*ptotal = *ptotal + gamescore;
if (gamescore < *pmin)
*pmin = gamescore;
if (gamescore > *pmax)
*pmax = gamescore;
}