SlideShare a Scribd company logo
I have the shell for this program already, I just need the two sub-functions to to get the driver
stats and get the median value.
THIS IS A SUB-FUNCTION THAT YOU HAVE TO WRITE
*/
void get_stats( driver_t driver_list[NRACERS], stats_t *race_stats )
Zero out the best_average (HINT: use the -> notation)
Set the slow_time to the first driver’s first try.
Set the fast_time to the first driver’s first try.
loop from d=zero to < NRACERS increment by one
{
zero out the driver_list[d].deviation
set the driver's best time to the driver's first time
loop from t=zero to t< TRIES increment by one
{
figure the driver's best time
find the fastest and slowest track time
}
add the driver's best time into the running total of best times
}
compute the average of the best times
loop from d=zero to < NRACERS increment by one
{
figure the driver's deviation from the class average
(deviation is fast time minus driver's best time)
}
return
/*
-------------------------------------------------------------
*/
/*
THIS IS A SUB-FUNCTION THAT YOU HAVE TO WRITE*/
void get_median(driver_t driver_list[NRACERS],stats_t *race_stats )
zero out the median.
calculate the mid point (divide NRACERS by two)
if the number of racers is odd then set the median to the mid average
else
set the median to the average of the two numbers(averages) on each side of the median. [mid] &
[mid
-1].NO integer division.
So that's all I need to ADD to the program, and then this is the actual program.
#include
#include
#define IN_FILENAME "lab5.dat"
#define OUT_FILENAME "lab5.txt"
#define NRACERS 10
#define TRIES 3
struct
{
char name []
double tries [TRIES]
double best_time
double deviation;
} driver_t;
struct
{
double best_average
double fast_time
double slow_time
double median;
} stats_t;
/* function prototypes */
/* get_data is a function to get and read data */
void get_data (char *filename, /* input */
driver_t driver_list[NRACERS] ); /* output */
/* open_out_file is a function to open the output file */
FILE * open_out_file (void);
/* get_stats is a function to do figure the best time for */
/* each racer, compute the all-over average of the best */
/* times, and find the longest time and the shortest time */
/* on the track */
void get_stats( driver_t driver_list[NRACERS], stats_t *race_stats )
{
}
;
/* do_sort is a function to sort the drivers based on */
/* their best times */
void do_sort( driver_t driver_list[NRACERS] );
/* get_median determines the median or mid value of */
/* the best times. */
void get_median(driver_t driver_list[NRACERS] , stats_t *race_stats )
{
};
/* print_all is a function to print things out. */
/* all of its arguments are input */
void print_all(FILE * out_file,
driver_t driver_list[NRACERS] ,
stats_t *race_stats );
/*-----------------------------------------------------------*/
int main(void)
{
driver_t driver_list[NRACERS];
stats_t race_stats = {0.0, 0.0, 0.0, 0.0};
FILE * out_file; /* file pointer for the output file */
/* Start the action. */
out_file = open_out_file ();
get_data(IN_FILENAME, driver_list);
get_stats(driver_list, &race_stats);
do_sort(driver_list);
get_median(driver_list, &race_stats);
print_all(out_file, driver_list, &race_stats);
return EXIT_SUCCESS;
}
/*----------------------------------------------------------*/
/* This function will open the output file and return the */
/* file pointer name to the main program. */
FILE * open_out_file (void)
{
FILE * outfile;
outfile = fopen(OUT_FILENAME, "w");
if (outfile == NULL)
{
printf("Error on fopen of %s  ", OUT_FILENAME);
exit(EXIT_FAILURE);
}
fprintf(outfile, " Your Name. Program 5 output.  ");
return outfile;
}
/*-----------------------------------------------------------*/
/* This function will open and read data into an array. */
void get_data (char *filename, /* input */
driver_t driver_list[NRACERS]) /* output */
{
int d;
FILE *in_file;
/* Open the data file and read in the array */
in_file = fopen(filename, "r");
if (in_file == NULL)
{
printf("Error on fopen of %s  ", filename);
exit(EXIT_FAILURE);
}
/* Use an "d" for Driver. */
for(d = 0; d < NRACERS; d++)
{
fscanf(in_file, "%20c%lf%lf%lf ",
&(driver_list[d].name),
&(driver_list[d].tries[0]),
&(driver_list[d].tries[1]),
&(driver_list[d].tries[2]) );
driver_list[d].name[20] = '0';
}
fclose(in_file);
return;
}
/*----------------------------------------------------------------*/
/* get_stats is a function to do figure the best time for each */
/* racer, compute the all-over average of the best times, and */
/* find the longest time and the shortest time on the track */
/*--------------------------------------------------------*/
/* This function will print everything out. */
void print_all(FILE * out_file,
driver_t driver_list[NRACERS] ,
stats_t *race_stats )
{
int d,t;
fprintf(out_file, " Track Results");
fprintf(out_file,"  Drivers Try 1 "
" Try 2 Try 3 Best Time Deviation");
fprintf(out_file, " -------------------- ---------"
" --------- --------- ---------- --------- ");
for (d = 0; d< NRACERS; d++)
{
fprintf(out_file, "%s", driver_list[d].name);
for (t = 0; t < TRIES; t++)
fprintf(out_file, " %8.3f ", driver_list[d].tries[t] );
fprintf(out_file, " %8.3f", driver_list[d].best_time );
fprintf(out_file, " %8.3f ", driver_list[d].deviation);
}
fprintf(out_file, "  The average of best times = %8.3f ", race_stats->best_average);
fprintf(out_file, "  The track fast time = %8.3f ", race_stats->fast_time);
fprintf(out_file, "  The track slow time = %8.3f ", race_stats->slow_time);
fprintf(out_file, "  The median of best times = %8.3f ", race_stats->median);
return;
}
/*--------------------------------------------------------*/
/* do_sort is a function to sort the drivers based on */
/* their best times */
void do_sort(driver_t driver_list[NRACERS])
{
int k, j, m;
driver_t hold;
/* start the selection sort algorithm */
for (k=0; k < NRACERS-1; k++)
{
/* exchange min with next array value */
m = k;
for (j = k+1; j < NRACERS; j++)
{
if ( driver_list[j].best_time < driver_list[m].best_time )
m = j;
}
hold = driver_list[m];
driver_list[m] = driver_list[k];
driver_list[k] = hold;
}
return;
}
Solution
#include
#include
#define IN_FILENAME "lab5.dat"
#define OUT_FILENAME "prog5.txt"
#define NRACERS 11 /* 10 this changes to amount of racers in input file */
#define TRIES 3
typedef struct{
char name[21];
double tries[TRIES];
double best_time;
double deviation;
} driver_t;
typedef struct{
double best_average;
double fast_time;
double slow_time;
double median;
} stats_t;
/* for loop i is used for each driver, must reset after use */
void get_stats(driver_t driver_list[NRACERS], stats_t *race_stats){
/*gets each drivers best time, overall average, overall fast and slow*/
race_stats -> best_average = 0;
race_stats -> slow_time = race_stats -> fast_time = driver_list[0].tries[0];
int i;
for(i=0; i slow_time = driver_list[i].tries[j] > race_stats -> slow_time ? driver_list[i].tries[j] :
race_stats -> slow_time;
race_stats -> fast_time = driver_list[i].tries[j] < race_stats -> fast_time ?
driver_list[i].tries[j] : race_stats -> fast_time;
}
race_stats -> best_average += driver_list[i].best_time;
}
race_stats -> best_average /= NRACERS;
for(i=0; i fast_time - driver_list[i].best_time;
}
}
void get_median(driver_t driver_list[NRACERS], stats_t *race_stats){
race_stats -> median = 0;
double best_each[NRACERS];
int i;
/* puts driver bests in sorted list */
for(i=0; i< NRACERS; ++i){
best_each[i] = driver_list[i].best_time;
}
race_stats -> median = NRACERS%2 == 1 ? best_each[NRACERS/2] :
(best_each[NRACERS/2-1]+best_each[NRACERS/2])/2;
}
/* Put your two structures here */
/* function prototypes */
/* get_data is a function to get and read data */
void get_data (char *filename, /* input */
driver_t driver_list[NRACERS] ); /* output */
/* open_out_file is a function to open the output file */
FILE * open_out_file (void);
/* get_stats is a function to do figure the best time for */
/* each racer, compute the all-over average of the best */
/* times, and find the longest time and the shortest time */
/* on the track */
void get_stats( driver_t driver_list[NRACERS], /* in & out */
stats_t *ace_stats ); /* in & out */
/* do_sort is a function to sort the drivers based on */
/* their best times */
void do_sort( driver_t driver_list[NRACERS] );
/* get_median determines the median or mid value of */
/* the best times. */
void get_median(driver_t driver_list[NRACERS] ,
stats_t *race_stats );
/* print_all is a function to print things out. */
/* all of its arguments are input */
void print_all(FILE * out_file,
driver_t driver_list[NRACERS] ,
stats_t *race_stats );
/*-----------------------------------------------------------*/
int main(void)
{
driver_t driver_list[NRACERS];
stats_t race_stats = {0.0, 0.0, 0.0, 0.0};
FILE * out_file; /* file pointer for the output file */
/* Start the action. */
out_file = open_out_file ();
get_data(IN_FILENAME, driver_list);
get_stats(driver_list, &race_stats);
do_sort(driver_list);
get_median(driver_list, &race_stats);
print_all(out_file, driver_list, &race_stats);
return EXIT_SUCCESS;
}
/*----------------------------------------------------------*/
/* This function will open the output file and return the */
/* file pointer name to the main program. */
FILE * open_out_file (void)
{
FILE * outfile;
outfile = fopen(OUT_FILENAME, "w");
if (outfile == NULL)
{
printf("Error on fopen of %s  ", OUT_FILENAME);
exit(EXIT_FAILURE);
}
fprintf(outfile, " Treewolf. Program 5 output.  ");
return outfile;
}
/*-----------------------------------------------------------*/
/* This function will open and read data into an array. */
void get_data (char *filename, /* input */
driver_t driver_list[NRACERS]) /* output */
{
int d;
FILE *in_file;
/* Open the data file and read in the array */
in_file = fopen(filename, "r");
if (in_file == NULL)
{
printf("Error on fopen of %s  ", filename);
exit(EXIT_FAILURE);
}
/* Use an "d" for Driver. */
for(d = 0; d < NRACERS; d++)
{
fscanf(in_file, "%20c%lf%lf%lf ",
&(driver_list[d].name),
&(driver_list[d].tries[0]),
&(driver_list[d].tries[1]),
&(driver_list[d].tries[2]) );
driver_list[d].name[20] = '0';
}
fclose(in_file);
return;
}
/*----------------------------------------------------------------*/
/* get_stats is a function to do figure the best time for each */
/* racer, compute the all-over average of the best times, and */
/* find the longest time and the shortest time on the track */
/*--------------------------------------------------------*/
/* This function will print everything out. */
void print_all(FILE * out_file,
driver_t driver_list[NRACERS] ,
stats_t *race_stats )
{
int d,t;
fprintf(out_file, " Track Results");
fprintf(out_file,"  Drivers Try 1 "
" Try 2 Try 3 Best Time Deviation");
fprintf(out_file, " -------------------- ---------"
" --------- --------- ---------- --------- ");
for (d = 0; d< NRACERS; d++)
{
fprintf(out_file, "%s", driver_list[d].name);
for (t = 0; t < TRIES; t++)
fprintf(out_file, " %8.3f ", driver_list[d].tries[t] );
fprintf(out_file, " %8.3f", driver_list[d].best_time );
fprintf(out_file, " %8.3f ", driver_list[d].deviation);
}
fprintf(out_file, "  The average of best times = %8.3f ", race_stats->best_average);
fprintf(out_file, "  The track fast time = %8.3f ", race_stats->fast_time);
fprintf(out_file, "  The track slow time = %8.3f ", race_stats->slow_time);
fprintf(out_file, "  The median of best times = %8.3f  ", race_stats->median);
return;
}
/*--------------------------------------------------------*/
/* do_sort is a function to sort the drivers based on */
/* their best times */
void do_sort(driver_t driver_list[NRACERS])
{
int k, j, m;
driver_t hold;
/* start the selection sort algorithm */
for (k=0; k < NRACERS-1; k++)
{
/* exchange min with next array value */
m = k;
for (j = k+1; j < NRACERS; j++)
{
if ( driver_list[j].best_time < driver_list[m].best_time )
m = j;
}
hold = driver_list[m];
driver_list[m] = driver_list[k];
driver_list[k] = hold;
}
return;
}
/*--------------------------------------------------------*/
/* This function determines the median or mid value of */
/* the best times. */
/*--------------------------------------------------------*/
lab5.dat
Jay Johnson 4.0 5.0 6.0
Lenny Loop 2.0 3.0 4.0
Missy Monroe 1.0 2.0 3.0
Ned Niner 3.0 7.0 5.0

More Related Content

PDF
Merge Sort implementation in C++ The implementation for Mergesort gi.pdf
DOCX
Instruction1. Please read the two articles. (Kincheloe part 1 &.docx
DOCX
RightTrianglerightTriangle.cppRightTrianglerightTriangle.cpp.docx
PDF
Notes for SQLite3 Usage
PPT
Gift-VT Tools Development Overview
PDF
Bare metal performance in Elixir
PPT
Unit 8
Merge Sort implementation in C++ The implementation for Mergesort gi.pdf
Instruction1. Please read the two articles. (Kincheloe part 1 &.docx
RightTrianglerightTriangle.cppRightTrianglerightTriangle.cpp.docx
Notes for SQLite3 Usage
Gift-VT Tools Development Overview
Bare metal performance in Elixir
Unit 8

Similar to I have the shell for this program already, I just need the two sub-f.pdf (20)

PDF
Hacking parse.y (RubyKansai38)
PDF
pg_proctab: Accessing System Stats in PostgreSQL
PDF
pg_proctab: Accessing System Stats in PostgreSQL
PPT
Php Reusing Code And Writing Functions
PDF
Non-Relational Postgres / Bruce Momjian (EnterpriseDB)
PDF
LLVM Backend の紹介
DOCX
__MACOSX._assign3assign3.DS_Store__MACOSXassign3._.D.docx
PDF
Memory Manglement in Raku
DOCX
Lab 2 Histrogram generation Author Naga Kandasamy .docx
DOCX
Lab Assignment 4 CSE330 Spring 2014 Skeleton Code for ex.docx
PDF
Hacking Parse.y with ujihisa
PDF
in c languageTo determine the maximum string length, we need to .pdf
PPT
SQL Tuning Overview
PPT
r,rstats,r language,r packages
ODP
Writing MySQL UDFs
DOCX
finalprojtemplatev5finalprojtemplate.gitignore# Ignore the b
PDF
pg_proctab: Accessing System Stats in PostgreSQL
PDF
pg_proctab: Accessing System Stats in PostgreSQL
PPT
Unit 4
PDF
pg_proctab: Accessing System Stats in PostgreSQL
Hacking parse.y (RubyKansai38)
pg_proctab: Accessing System Stats in PostgreSQL
pg_proctab: Accessing System Stats in PostgreSQL
Php Reusing Code And Writing Functions
Non-Relational Postgres / Bruce Momjian (EnterpriseDB)
LLVM Backend の紹介
__MACOSX._assign3assign3.DS_Store__MACOSXassign3._.D.docx
Memory Manglement in Raku
Lab 2 Histrogram generation Author Naga Kandasamy .docx
Lab Assignment 4 CSE330 Spring 2014 Skeleton Code for ex.docx
Hacking Parse.y with ujihisa
in c languageTo determine the maximum string length, we need to .pdf
SQL Tuning Overview
r,rstats,r language,r packages
Writing MySQL UDFs
finalprojtemplatev5finalprojtemplate.gitignore# Ignore the b
pg_proctab: Accessing System Stats in PostgreSQL
pg_proctab: Accessing System Stats in PostgreSQL
Unit 4
pg_proctab: Accessing System Stats in PostgreSQL

More from arhamnighty (20)

PDF
Besides using the certificates, suggest alternate method to avoid th.pdf
PDF
X mun( oomiB9@@e SolutionAnswerThe image represents the.pdf
PDF
Which of these is not a way for a cell to obtain nutrients and other.pdf
PDF
Which of the following receptors is most likely to exhibit tonic adap.pdf
PDF
Which of the following is not a contribution of Robert Koch and his .pdf
PDF
When RNA from a specific strain of TMV is mixed with coat protein fro.pdf
PDF
What are the sources of Ductility for structures What is more du.pdf
PDF
What are some examples of self love in Pride and PrejudiceSolut.pdf
PDF
Using the phylogenetic tree shown, state the basal taxon Using the p.pdf
PDF
Use attached Table A-2 for Normal Distribution to find the critical .pdf
PDF
Based on the data provided through the U.S. Department of the Treasu.pdf
PDF
Trace the major historical developments of hospitals in the United S.pdf
PDF
The role of Ca2+ in the control of muscle contraction is to1. caus.pdf
PDF
The following data. recorded in days, represent the length of time to.pdf
PDF
Suppose that A and B are in DSPACE(n). Prove that the following lang.pdf
PDF
Some new protocols such as Internet Protocol (IP) version 6 are not .pdf
PDF
Selection of favorite fruits and responses of orange, grape, apple, .pdf
PDF
RNA and DNA differRNA and DNA differE-All of these are correct..pdf
PDF
Propose a pathway(s) to explain how the ectoderm and the notochord b.pdf
PDF
Potassium has one electron in its outer shell. Which of the followin.pdf
Besides using the certificates, suggest alternate method to avoid th.pdf
X mun( oomiB9@@e SolutionAnswerThe image represents the.pdf
Which of these is not a way for a cell to obtain nutrients and other.pdf
Which of the following receptors is most likely to exhibit tonic adap.pdf
Which of the following is not a contribution of Robert Koch and his .pdf
When RNA from a specific strain of TMV is mixed with coat protein fro.pdf
What are the sources of Ductility for structures What is more du.pdf
What are some examples of self love in Pride and PrejudiceSolut.pdf
Using the phylogenetic tree shown, state the basal taxon Using the p.pdf
Use attached Table A-2 for Normal Distribution to find the critical .pdf
Based on the data provided through the U.S. Department of the Treasu.pdf
Trace the major historical developments of hospitals in the United S.pdf
The role of Ca2+ in the control of muscle contraction is to1. caus.pdf
The following data. recorded in days, represent the length of time to.pdf
Suppose that A and B are in DSPACE(n). Prove that the following lang.pdf
Some new protocols such as Internet Protocol (IP) version 6 are not .pdf
Selection of favorite fruits and responses of orange, grape, apple, .pdf
RNA and DNA differRNA and DNA differE-All of these are correct..pdf
Propose a pathway(s) to explain how the ectoderm and the notochord b.pdf
Potassium has one electron in its outer shell. Which of the followin.pdf

Recently uploaded (20)

PPTX
Lesson notes of climatology university.
PPTX
master seminar digital applications in india
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
01-Introduction-to-Information-Management.pdf
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Classroom Observation Tools for Teachers
Lesson notes of climatology university.
master seminar digital applications in india
Abdominal Access Techniques with Prof. Dr. R K Mishra
human mycosis Human fungal infections are called human mycosis..pptx
Final Presentation General Medicine 03-08-2024.pptx
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
FourierSeries-QuestionsWithAnswers(Part-A).pdf
01-Introduction-to-Information-Management.pdf
Chinmaya Tiranga quiz Grand Finale.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
STATICS OF THE RIGID BODIES Hibbelers.pdf
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Classroom Observation Tools for Teachers

I have the shell for this program already, I just need the two sub-f.pdf

  • 1. I have the shell for this program already, I just need the two sub-functions to to get the driver stats and get the median value. THIS IS A SUB-FUNCTION THAT YOU HAVE TO WRITE */ void get_stats( driver_t driver_list[NRACERS], stats_t *race_stats ) Zero out the best_average (HINT: use the -> notation) Set the slow_time to the first driver’s first try. Set the fast_time to the first driver’s first try. loop from d=zero to < NRACERS increment by one { zero out the driver_list[d].deviation set the driver's best time to the driver's first time loop from t=zero to t< TRIES increment by one { figure the driver's best time find the fastest and slowest track time } add the driver's best time into the running total of best times } compute the average of the best times loop from d=zero to < NRACERS increment by one { figure the driver's deviation from the class average (deviation is fast time minus driver's best time) } return /* ------------------------------------------------------------- */ /* THIS IS A SUB-FUNCTION THAT YOU HAVE TO WRITE*/ void get_median(driver_t driver_list[NRACERS],stats_t *race_stats ) zero out the median. calculate the mid point (divide NRACERS by two) if the number of racers is odd then set the median to the mid average
  • 2. else set the median to the average of the two numbers(averages) on each side of the median. [mid] & [mid -1].NO integer division. So that's all I need to ADD to the program, and then this is the actual program. #include #include #define IN_FILENAME "lab5.dat" #define OUT_FILENAME "lab5.txt" #define NRACERS 10 #define TRIES 3 struct { char name [] double tries [TRIES] double best_time double deviation; } driver_t; struct { double best_average double fast_time double slow_time double median; } stats_t; /* function prototypes */ /* get_data is a function to get and read data */ void get_data (char *filename, /* input */ driver_t driver_list[NRACERS] ); /* output */ /* open_out_file is a function to open the output file */ FILE * open_out_file (void); /* get_stats is a function to do figure the best time for */ /* each racer, compute the all-over average of the best */ /* times, and find the longest time and the shortest time */ /* on the track */
  • 3. void get_stats( driver_t driver_list[NRACERS], stats_t *race_stats ) { } ; /* do_sort is a function to sort the drivers based on */ /* their best times */ void do_sort( driver_t driver_list[NRACERS] ); /* get_median determines the median or mid value of */ /* the best times. */ void get_median(driver_t driver_list[NRACERS] , stats_t *race_stats ) { }; /* print_all is a function to print things out. */ /* all of its arguments are input */ void print_all(FILE * out_file, driver_t driver_list[NRACERS] , stats_t *race_stats ); /*-----------------------------------------------------------*/ int main(void) { driver_t driver_list[NRACERS]; stats_t race_stats = {0.0, 0.0, 0.0, 0.0}; FILE * out_file; /* file pointer for the output file */ /* Start the action. */ out_file = open_out_file (); get_data(IN_FILENAME, driver_list); get_stats(driver_list, &race_stats); do_sort(driver_list); get_median(driver_list, &race_stats); print_all(out_file, driver_list, &race_stats); return EXIT_SUCCESS; }
  • 4. /*----------------------------------------------------------*/ /* This function will open the output file and return the */ /* file pointer name to the main program. */ FILE * open_out_file (void) { FILE * outfile; outfile = fopen(OUT_FILENAME, "w"); if (outfile == NULL) { printf("Error on fopen of %s ", OUT_FILENAME); exit(EXIT_FAILURE); } fprintf(outfile, " Your Name. Program 5 output. "); return outfile; } /*-----------------------------------------------------------*/ /* This function will open and read data into an array. */ void get_data (char *filename, /* input */ driver_t driver_list[NRACERS]) /* output */ { int d; FILE *in_file; /* Open the data file and read in the array */ in_file = fopen(filename, "r"); if (in_file == NULL) { printf("Error on fopen of %s ", filename); exit(EXIT_FAILURE); } /* Use an "d" for Driver. */ for(d = 0; d < NRACERS; d++) { fscanf(in_file, "%20c%lf%lf%lf ", &(driver_list[d].name), &(driver_list[d].tries[0]), &(driver_list[d].tries[1]),
  • 5. &(driver_list[d].tries[2]) ); driver_list[d].name[20] = '0'; } fclose(in_file); return; } /*----------------------------------------------------------------*/ /* get_stats is a function to do figure the best time for each */ /* racer, compute the all-over average of the best times, and */ /* find the longest time and the shortest time on the track */ /*--------------------------------------------------------*/ /* This function will print everything out. */ void print_all(FILE * out_file, driver_t driver_list[NRACERS] , stats_t *race_stats ) { int d,t; fprintf(out_file, " Track Results"); fprintf(out_file," Drivers Try 1 " " Try 2 Try 3 Best Time Deviation"); fprintf(out_file, " -------------------- ---------" " --------- --------- ---------- --------- "); for (d = 0; d< NRACERS; d++) { fprintf(out_file, "%s", driver_list[d].name); for (t = 0; t < TRIES; t++) fprintf(out_file, " %8.3f ", driver_list[d].tries[t] ); fprintf(out_file, " %8.3f", driver_list[d].best_time ); fprintf(out_file, " %8.3f ", driver_list[d].deviation); } fprintf(out_file, " The average of best times = %8.3f ", race_stats->best_average); fprintf(out_file, " The track fast time = %8.3f ", race_stats->fast_time); fprintf(out_file, " The track slow time = %8.3f ", race_stats->slow_time); fprintf(out_file, " The median of best times = %8.3f ", race_stats->median);
  • 6. return; } /*--------------------------------------------------------*/ /* do_sort is a function to sort the drivers based on */ /* their best times */ void do_sort(driver_t driver_list[NRACERS]) { int k, j, m; driver_t hold; /* start the selection sort algorithm */ for (k=0; k < NRACERS-1; k++) { /* exchange min with next array value */ m = k; for (j = k+1; j < NRACERS; j++) { if ( driver_list[j].best_time < driver_list[m].best_time ) m = j; } hold = driver_list[m]; driver_list[m] = driver_list[k]; driver_list[k] = hold; } return; } Solution #include #include #define IN_FILENAME "lab5.dat" #define OUT_FILENAME "prog5.txt" #define NRACERS 11 /* 10 this changes to amount of racers in input file */ #define TRIES 3
  • 7. typedef struct{ char name[21]; double tries[TRIES]; double best_time; double deviation; } driver_t; typedef struct{ double best_average; double fast_time; double slow_time; double median; } stats_t; /* for loop i is used for each driver, must reset after use */ void get_stats(driver_t driver_list[NRACERS], stats_t *race_stats){ /*gets each drivers best time, overall average, overall fast and slow*/ race_stats -> best_average = 0; race_stats -> slow_time = race_stats -> fast_time = driver_list[0].tries[0]; int i; for(i=0; i slow_time = driver_list[i].tries[j] > race_stats -> slow_time ? driver_list[i].tries[j] : race_stats -> slow_time; race_stats -> fast_time = driver_list[i].tries[j] < race_stats -> fast_time ? driver_list[i].tries[j] : race_stats -> fast_time; } race_stats -> best_average += driver_list[i].best_time; } race_stats -> best_average /= NRACERS; for(i=0; i fast_time - driver_list[i].best_time; } } void get_median(driver_t driver_list[NRACERS], stats_t *race_stats){ race_stats -> median = 0; double best_each[NRACERS]; int i;
  • 8. /* puts driver bests in sorted list */ for(i=0; i< NRACERS; ++i){ best_each[i] = driver_list[i].best_time; } race_stats -> median = NRACERS%2 == 1 ? best_each[NRACERS/2] : (best_each[NRACERS/2-1]+best_each[NRACERS/2])/2; } /* Put your two structures here */ /* function prototypes */ /* get_data is a function to get and read data */ void get_data (char *filename, /* input */ driver_t driver_list[NRACERS] ); /* output */ /* open_out_file is a function to open the output file */ FILE * open_out_file (void); /* get_stats is a function to do figure the best time for */ /* each racer, compute the all-over average of the best */ /* times, and find the longest time and the shortest time */ /* on the track */ void get_stats( driver_t driver_list[NRACERS], /* in & out */ stats_t *ace_stats ); /* in & out */ /* do_sort is a function to sort the drivers based on */ /* their best times */ void do_sort( driver_t driver_list[NRACERS] ); /* get_median determines the median or mid value of */ /* the best times. */ void get_median(driver_t driver_list[NRACERS] , stats_t *race_stats ); /* print_all is a function to print things out. */ /* all of its arguments are input */ void print_all(FILE * out_file, driver_t driver_list[NRACERS] , stats_t *race_stats );
  • 9. /*-----------------------------------------------------------*/ int main(void) { driver_t driver_list[NRACERS]; stats_t race_stats = {0.0, 0.0, 0.0, 0.0}; FILE * out_file; /* file pointer for the output file */ /* Start the action. */ out_file = open_out_file (); get_data(IN_FILENAME, driver_list); get_stats(driver_list, &race_stats); do_sort(driver_list); get_median(driver_list, &race_stats); print_all(out_file, driver_list, &race_stats); return EXIT_SUCCESS; } /*----------------------------------------------------------*/ /* This function will open the output file and return the */ /* file pointer name to the main program. */ FILE * open_out_file (void) { FILE * outfile; outfile = fopen(OUT_FILENAME, "w"); if (outfile == NULL) { printf("Error on fopen of %s ", OUT_FILENAME); exit(EXIT_FAILURE); } fprintf(outfile, " Treewolf. Program 5 output. "); return outfile; } /*-----------------------------------------------------------*/ /* This function will open and read data into an array. */ void get_data (char *filename, /* input */ driver_t driver_list[NRACERS]) /* output */ {
  • 10. int d; FILE *in_file; /* Open the data file and read in the array */ in_file = fopen(filename, "r"); if (in_file == NULL) { printf("Error on fopen of %s ", filename); exit(EXIT_FAILURE); } /* Use an "d" for Driver. */ for(d = 0; d < NRACERS; d++) { fscanf(in_file, "%20c%lf%lf%lf ", &(driver_list[d].name), &(driver_list[d].tries[0]), &(driver_list[d].tries[1]), &(driver_list[d].tries[2]) ); driver_list[d].name[20] = '0'; } fclose(in_file); return; } /*----------------------------------------------------------------*/ /* get_stats is a function to do figure the best time for each */ /* racer, compute the all-over average of the best times, and */ /* find the longest time and the shortest time on the track */ /*--------------------------------------------------------*/ /* This function will print everything out. */ void print_all(FILE * out_file, driver_t driver_list[NRACERS] , stats_t *race_stats ) { int d,t; fprintf(out_file, " Track Results");
  • 11. fprintf(out_file," Drivers Try 1 " " Try 2 Try 3 Best Time Deviation"); fprintf(out_file, " -------------------- ---------" " --------- --------- ---------- --------- "); for (d = 0; d< NRACERS; d++) { fprintf(out_file, "%s", driver_list[d].name); for (t = 0; t < TRIES; t++) fprintf(out_file, " %8.3f ", driver_list[d].tries[t] ); fprintf(out_file, " %8.3f", driver_list[d].best_time ); fprintf(out_file, " %8.3f ", driver_list[d].deviation); } fprintf(out_file, " The average of best times = %8.3f ", race_stats->best_average); fprintf(out_file, " The track fast time = %8.3f ", race_stats->fast_time); fprintf(out_file, " The track slow time = %8.3f ", race_stats->slow_time); fprintf(out_file, " The median of best times = %8.3f ", race_stats->median); return; } /*--------------------------------------------------------*/ /* do_sort is a function to sort the drivers based on */ /* their best times */ void do_sort(driver_t driver_list[NRACERS]) { int k, j, m; driver_t hold; /* start the selection sort algorithm */ for (k=0; k < NRACERS-1; k++) { /* exchange min with next array value */ m = k; for (j = k+1; j < NRACERS; j++) { if ( driver_list[j].best_time < driver_list[m].best_time ) m = j; }
  • 12. hold = driver_list[m]; driver_list[m] = driver_list[k]; driver_list[k] = hold; } return; } /*--------------------------------------------------------*/ /* This function determines the median or mid value of */ /* the best times. */ /*--------------------------------------------------------*/ lab5.dat Jay Johnson 4.0 5.0 6.0 Lenny Loop 2.0 3.0 4.0 Missy Monroe 1.0 2.0 3.0 Ned Niner 3.0 7.0 5.0