For further reference, here is the full program, sort of lengthy, but if anyone can help, I sure would apprecieated it, I've got one day:eek:
#include <stdio.h>
#include <string.h>
#define NOT_FOUND -1
/*Structure containing all data for each player*/
typedef struct {
char name[30];
int fieldgoals_att;
int fieldgoals_made;
int freethrows_att;
int freethrows_made;
} playerData;
/*Computes and displays Total Points for Each Player*/
void totalPoints(playerData playerList [],int index)
{
int fg_made;
int ft_made;
int total;
fg_made = playerList[index].fieldgoals_made;
ft_made = playerList[index].freethrows_made;
total = (fg_made * 2)+ ft_made;
printf(" %d\n\n", total);
}
/*Computes and displays the fieldgoal percentage*/
void fieldPercent(playerData playerList [], int index)
{
int field_att;
int field_made;
double field_percent;
field_att = playerList[index].fieldgoals_att;
field_made = playerList[index].fieldgoals_made;
if(field_att == 0)
{
printf("0\t");
}
else
{
field_percent = (double)field_made / field_att;
printf("%.2f\t", field_percent);
}
}
/*Computes and displays the freethrow percentage*/
void freePercent(playerData playerList [], int index)
{
int free_att;
int free_made;
double free_percent;
free_att = playerList[index].freethrows_att;
free_made = playerList[index].freethrows_made;
if(free_att == 0)
{
printf("0\t");
}
else
{
free_percent = (double)free_made / free_att;
printf("%.2f\t", free_percent);
}
}
/*Computes the average percentage of fieldgoals and freethrows for the whole team*/
void totalAveragePercent(playerData playerList [], int count)
{
int index, total_field_att, total_field_made, total_free_att, total_free_made;
double team_field_percent, team_free_percent;
total_field_att = 0;
total_field_made = 0;
total_free_att = 0;
total_free_made = 0;
for(index = 1; index <= count-1; index++)
{
total_field_att = total_field_att + playerList[index].fieldgoals_att;
total_field_made = total_field_made + playerList[index].fieldgoals_made;
total_free_att = total_free_att + playerList[index].freethrows_att;
total_free_made = total_free_made + playerList[index].freethrows_made;
}
if(total_field_att == 0)
{
team_field_percent = 0;
}
else
{
team_field_percent = (double)total_field_made / total_field_att;
}
if(total_free_att == 0)
{
team_free_percent = 0;
}
else
{
team_free_percent = (double)total_free_made / total_free_att;
}
printf(" Team Avg %.2f Team Avg %.2f", team_field_percent, team_free_percent);
}
/*Combines data of a player who has more than one entry in the data file*/
int search(playerData playerList[], /* input - array to search */
playerData currentPlayer, /* input - value searched for */
int count) /* input - number of elements to search */
{
int i = 0,
found = 0, /* whether or not target has been found */
where; /* index where target found or NOT_FOUND */
/* Compares each element to target */
while(!found && i < count)
{
if(strcmp(playerList[i].name, currentPlayer.name) == 0)
{found = 1;}
else
{i++;}
}
/* Returns index of element matching target or NOT_FOUND */
if (found)
{where = i;}
else
{where = NOT_FOUND;}
return (where);
}
/*Checks for validity of all data in the data file.*/
void validateData(playerData currentPlayer, playerData playerList[], int count, int invalid[])
{
if((currentPlayer.fieldgoals_made > currentPlayer.fieldgoals_att) /* IF statement checks the validity of all the input data, */
|| (currentPlayer.freethrows_made > currentPlayer.freethrows_att) /* if there's invalid data, reports an error and sets that */
|| (currentPlayer.fieldgoals_made <=0 && currentPlayer.fieldgoals_made >=100) /* player's stats to zero so that they won't effect any of */
|| (currentPlayer.fieldgoals_att <=0 && currentPlayer.fieldgoals_att >=100) /* the calculations. Then the invalid array is used to set */
|| (currentPlayer.freethrows_made <=0 && currentPlayer.freethrows_made >=100) /* the player's corresponding value to 1, which is accesed */
|| (currentPlayer.freethrows_att <=0 && currentPlayer.freethrows_att >=100)) /* by the showPlayerData function to tell which stats will */
{ /* be shown, depending on the values in the invalid array. */
playerList[count].fieldgoals_att = 0;
playerList[count].fieldgoals_made = 0;
playerList[count].freethrows_att = 0;
playerList[count].freethrows_made = 0;
invalid[count] = 1;
printf("Invalid data in line %d.\n\n", count);
}
else
{
strcpy(playerList[count].name, currentPlayer.name);
playerList[count].fieldgoals_att = currentPlayer.fieldgoals_att;
playerList[count].fieldgoals_made = currentPlayer.fieldgoals_made;
playerList[count].freethrows_att = currentPlayer.freethrows_att;
playerList[count].freethrows_made = currentPlayer.freethrows_made;
}
}
/*Calls the computation sub funtctions and displays the player's full data*/
void showPlayerData(playerData playerList[], int count, int invalid[])
{
int i;
printf(" Daring Dunkers Player Statistics\n\n");
printf("Player Field Goals Free Throws Total\n\n");
printf(" Tried Made %% Tried Made %% Points\n\n");
for(i = 1; i <= count-1; i++)
{
if(invalid[i] < 1)
{
printf("%s\t", playerList[i].name);
printf(" %d\t", playerList[i].fieldgoals_att);
printf("%d\t", playerList[i].fieldgoals_made);
fieldPercent(playerList, i);
printf("%d\t", playerList[i].freethrows_att);
printf("%d\t", playerList[i].freethrows_made);
freePercent(playerList, i);
totalPoints(playerList,i);
}
}
}
/*Reads the data file and fills the structure variable*/
void fillStructure(playerData *currentPlayer, playerData playerList[], int count, int invalid[])
{
FILE *inp;
int status;
inp = fopen("bbstats.txt", "r");
status = fscanf(inp, "%s", currentPlayer->name);
while(status != EOF)
{
fscanf(inp, "%d", ¤tPlayer->fieldgoals_att);
fscanf(inp, "%d", ¤tPlayer->fieldgoals_made);
fscanf(inp, "%d", ¤tPlayer->freethrows_att);
fscanf(inp, "%d", ¤tPlayer->freethrows_made);
printf("%d ", currentPlayer->freethrows_made);
validateData(*currentPlayer, playerList, count, invalid);
search(playerList, *currentPlayer, count);
status = fscanf(inp, "%s", currentPlayer->name);
count++;
}
}
/*Start of Main Function*/
int main (void)
{
FILE *starting;
int count;
int invalid[1000] = {0};
playerData playerList[1000];
playerData currentPlayer;
count = 1;
starting = fopen("bbstats.txt", "r");
if(starting == NULL)
{
printf("Could not open file");
}
else
{
fillStructure(¤tPlayer, playerList, count, invalid);
showPlayerData(playerList, count, invalid);
totalAveragePercent(playerList, count);
}
return (0);
}