Code:
/*************************************************************************************************
This file takes an input file on the Auburn football season and scans all of its data into arrays. It then puts the
data into a printable format and calculates the records and averages and prints it to the screen.
***************************************CONSTANTS**********************************************/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define FILENAME "auResults2013.txt" //input text file
#define MAXNUMGAMES 15 //max numer of games in a season
#define TITLELEN 32 //max title lenght
#define CONFLEN 4 //max length of conference
#define DATELEN 10 //length of date
#define NAMELEN 20 //max lenght of opponent names
#define NUMCOLS 4 //max number of numeric collums
#define TIMECOL 2 //column index of time
#define LINE_MAX 61 //max num of characters on a line
/**************************************INPUT****************************************************/
int readDatafile( char title[], char conf[][CONFLEN], char dates[][DATELEN], char oppNames[][NAMELEN], int results[][NUMCOLS]);
void printReport( char title[], char conf[][CONFLEN], char dates[][DATELEN], char oppNames[][NAMELEN], int results[][NUMCOLS], int numGames);
void printHeader( char title[], char dates[][DATELEN], int numGames);
//my created functions for calculations
int arrayAverage(int numGames, int results[][NUMCOLS] );
double auaverageScore(int numGames, int results[][NUMCOLS] );
double oppAverageScore(int numGames, int results[][NUMCOLS] );
void compareGameLenghts(char dates[][DATELEN], int results[][NUMCOLS], int numGames);
int main()
{
char title[LINE_MAX],
conf[MAXNUMGAMES][CONFLEN],
dates[MAXNUMGAMES][DATELEN],
oppNames[MAXNUMGAMES][NAMELEN];
int results[MAXNUMGAMES][NUMCOLS],
numGames;
numGames = readDatafile(title, conf, dates, oppNames, results);
printReport(title, conf, dates, oppNames, results, numGames);
return 0;
}
//---------------------------------------------------Read Data-----------------------------------------
int readDatafile(char title[], char conf[][CONFLEN], char dates[][DATELEN], char oppNames[][NAMELEN], int results[][NUMCOLS])
{
char lineNumber[LINE_MAX];
int timeScanner[MAXNUMGAMES][TIMECOL],
l, //line number counter
c, //collumn number
t; //time scanner counter
FILE *inp = fopen(FILENAME, "r");
if(inp == NULL) {printf("There is no input file");}
else
{
fgets(title,TITLELEN,inp);
//scan Conference, Date of Game, and Opponent's Names in
while(fscanf(inp,"%s%s%s", conf[l], dates[l], oppNames[l]) !=EOF)
{
//Scan score in and store in array
fscanf(inp, "%d %d", &results[l][0], &results[l][1]);
//Scan hours and minutes in
fscanf(inp, "%d:%d", &timeScanner[l][0], &timeScanner[l][1]);
//Compute hours into minues, add, and store into returned array
results[l][2] = (timeScanner[l][0]*60 + timeScanner[l][1]);
//Scans attendance
fscanf(inp," %d", &results[l][3]);
l++;
}
fclose(inp);
}
return (l); //returns number of games in counter
}
/*****************************************************OUTPUT************************************************************/
//-------------------------------------------------PRINT REPORT--------------------------------------------------------//
void printReport( char title[], char conf[][CONFLEN], char dates[][DATELEN], char oppNames[][NAMELEN], int results[][NUMCOLS], int numGames)
{
int c, //line counter
w=0, // win counter
l=0, //loss counter
attdAvg;
double auScoreAvg,
oppScoreAvg,
dAU,
dOPP;
char compare[CONFLEN] = "SEC";
//print Top of Chart
printHeader(title, dates, numGames);
//calculates average attendance for use later
attdAvg = arrayAverage(numGames, results);
//averages auburns scores, returns the averagre as an int
auScoreAvg = auaverageScore(numGames, results);
//averages Opponent scores, returns the averagre as an int
oppScoreAvg = oppAverageScore(numGames, results);
//prints each line in the chart with game data
for(c=0; c<numGames; c++)
{
if(strcmp (conf[c],compare) == 0) {printf("* ");}
else {printf(" ");}
printf( "%s %-20s ",dates[c], oppNames[c]); //prints the date and opponents name
dAU = results[c][0];
printf("%2.0lf-", dAU); //prints the Auburn score
dOPP = results[c][1];
printf("%2.0lf", dOPP); //and the opponents score per line
if(results[c][0] > results[c][1]) //determines if AU win
{
printf(" W ");
w++;
}
if(results[c][1] > results[c][0]) //determines if AU loss
{
printf(" L ");
l++;
}
printf(" %6d ", results[c][3]); //prints attendence
if(attdAvg < results[c][3]) {printf(" +\n");} //determine if attendace is above the avg
else{printf("\n");}
}
printf("\n");
printf("* SEC conference game\n");
printf("+ Above Average Attendance\n");
printf("\n");
printf("Auburn Season Record: %d-%d\n", w, l);
printf("Auburn Average Score: %3.1lf\n", auScoreAvg);
printf("Opponent Average Score: %3.1lf\n", oppScoreAvg);
compareGameLenghts(dates, results, numGames); //finds and prints the longest game and date
}
//-------------------------------------------------Print Headers---------------------------------------------------
void printHeader( char title[], char dates[][DATELEN], int numGames)
{
int x = numGames - 1;
//printf("%d", x);
printf("\n");
printf(" %s as of %s \n\n", title, dates[x]);
printf(" SCORE W\n");
printf(" DATE OPPONENT AU-OP L ATTEND\n");
printf("--------------------------------------------------\n");
}
//----------------------------------------------Average Attendence------------------------------------------------
int arrayAverage(int numGames, int results[][NUMCOLS] )
{int num, //counter
total, //total num ppl
average; //total average for all games
for(num=0 ; num<numGames; num++)
{
total += results[num][3];//sums attendence
}
average = total/numGames; //calculates average attendence
return average;
}
//-----------------------------------------------Calculate AU Average Score--------------------------------------------
double auaverageScore(int numGames, int results[][NUMCOLS] )
{
double average,
total; //sum of all scores
int c; //counter
for(c=0; c<numGames; c++)
{
total += results[c][0];
}
average = total/numGames;
return average;
}
//--------------------------------------------------Calculate Opp Avg Score--------------------------------------------------
double oppAverageScore(int numGames, int results[][NUMCOLS] )
{
double average,
total=0; //keeps track of total of opp scores
int c; //counter
for(c=0; c<numGames; c++)
{
total += results[c][1];
}
average = total/numGames; //averages opponets socores/ game
return average;
}
//--------------------------------------------------Finds longest Game-----------------------------------------------------
void compareGameLenghts(char dates[][DATELEN], int results[][NUMCOLS], int numGames )
{
int k, maxK; //counter, counter of max time
double maxD; //numeric value of time of longest date
maxD = results[0][2];
for(k=0; k<numGames; k++ )
{
if(results[k][2] > results[0][2]) //compares game time lenghts
{
maxD = results[k][2];
maxK = k;
}
}
printf("Longest Games was %3.0lf mins on %s\n", maxD, dates[maxK]);
}