hi guys i need help,
Write a program that reads a data file and uses an array to figure out bowling scores.
In bowling, 10 pins are set up for each frame and the player gets two chances to knock them down.
1. 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.
2. If the player knocks down all 10 pins with the two balls, it is called a spare and is marked with a "/"). The player gets 10 points plus the number of pins knocked down with the next ball.
3. If the player knocks down all 10 pins with one ball, it is called a strike and is marked with an "X"). The player gets 10 points plus the number of pins knocked down with the next two balls.
Frame 10 is a special case. If you get a strike on the first ball you get two extra balls to add-up their scores. If you get a spare with the first two balls then you get a third ball to throw. If you fail to knock down all pins with the first two balls, the game ends there.
Input data: The data file contains the player’s name and the number of pins knocked down with a ball. -1 indicates the end of the game (the next record is another player name).
For each player, print out the the traditional bowling sheet for that player.
Print out at the end the name of the winner and your personal and copyright information (see example).
The output report should be like the following example. This is for only one player; your report must include all players (skip 3 lines between players in the report).
Your report must use the data from the bowling.dat file but should work with any valid game file.
Here is what I got:
Code:
#include <stdio.h>
#include <time.h>
#define TRUE 1
#define FALSE 0
void print_date_and_time()
{
time_t t;
char now[20];
time(&t);
strcpy(now, ctime(&t));
printf("%s", now);
}
void calculate_and_print(FILE *fp)
{
char *first_name, *last_name;
int scores[21], points[12];
int i, frame, first_try = TRUE;
int strike = FALSE, spare = FALSE;
printf("Frame\t\t 1 2 3 4 5 6 7 8 9 10\n");
while(1)
{
/* initialize points array with 0 */
for(i = 0; i < 12; i ++)
{
points[i] = 0;
}
/* Read first name and last name */
fscanf(fp, "%s", first_name);
if(!strcmp(first_name, "End"))
break;
fscanf(fp, "%s", last_name);
printf("%s\t", last_name);
/* store scores into array & calculate score*/
i = 0;
while(1)
{
fscanf(fp, "%d", &scores[i]);
if(scores[i] == -1)
break;
i++;
}
frame = 0; /* used as index for points array (frame 1 = 0) */
i = 0; /* used as index to access scores array */
while(1)
{
if(scores[i] == -1)
break;
if(first_try)
{
if(scores[i] == 10) /* strike */
{
points[frame] = 10;
strike = TRUE;
frame++;
first_try = TRUE;
printf("X ");
}
else
{
points[frame] = scores[i];
first_try = FALSE;
printf("%d", scores[i]);
}
}
else /* second try */
{
points[frame] = points[frame] + scores[i];
if(strike) /* if strike in pervious frame */
{
points[frame - 1] = points[frame - 1] + points[frame];
strike = FALSE;
}
if(spare) /* if spare in previous frame */
{
points[frame - 1] = points[frame - 1] + scores[i - 1];
spare = FALSE;
}
if(scores[i - 1] + scores[i] == 10) /* spare */
{
spare = TRUE;
printf("/ ");
}
else
printf("-%d ", scores[i]);
frame++;
first_try = TRUE;
}
i++;
}
/* Accumulate scores */
for(i = 1; i < 10; i ++)
{
points[i] = points[i - 1] + points[i];
}
/* Handle 10th frame case */
points[8] = points[9];
points[9] = points[9] + points[10] + points[11]; /* additional scores
if there's any */
printf("\nScore:\t\t");
printf("%d ", points[0]);
for(i = 1; i < 10; i ++)
printf("%d ", points[i]);
printf("\n\n\n");
printf("Program by:bla bla and bla bla\n");
printf("We worked in the partners\n");
}
}
int main(void)
{
FILE* fp;
char *file_name;
printf("Enter data file name: ");
scanf("%s", file_name);
fp = fopen(file_name, "r");
if(fp == NULL)
{
printf("Can't open %s\n", file_name);
exit(1);
}
calculate_and_print(fp);
getch();
return 0;
}
I don't see any time. Do I have to call the funtion time in the main? Please any help is arprecaited. Thanks