Write a program that uses structures and pointers. You will have to write two functions: get_stats, and get_median. You will have to write a makefile.
(1) First in lab7.h, you need to declare a structure type driver_t.
I named my structure driver_t and its 4 parts are:
Figure: see image.
(2) Next in lab7.h, you need to declare a structure type: stats_t
I named my structure stats_t and its 4 parts are:
(3) Add your name to the comment block in lab7.h. Eventually you will need to shift the comment marks ( // ) on the four #define statements: two for the data file, two for the NRACERS
(4) Write the function get_stats. This function will figure the driver's best time, the track slow time and fast time, the average of the driver's best times, and the driver's deviation from the fast time. The prototype is:
void get_stats(driver_t driver_list[NRACERS], /* in & out */
stats_t *race_stats ); /* in & out */
(5) Write the function get_median. It will find the mid best time from the sorted list of driver best times. Examples of computing median are on the top of page 4. The prototype for get_median is:
void get_median(driver_t driver_list[NRACERS], stats_t *race_stats );
(6) Write the makefile with a comment block at the top that includes your name.
The program input is a set of driver's names and their three tries on the racetrack in one file. The race times are type double. Each record/line of the file has a student name and three times.
The first line from the sample data file is:
Jay Johnson 4.100 5.300 6.700
The output is printed to lab7.txt as shown in the sample output.
/*-------------------------------------------------------------*/
main
/* This function already exists. */
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);
/*-------------------------------------------------------------*/
FILE * open_out_file(void)
/* This function already exists. */
/* Opens the output file */
/*-------------------------------------------------------------*/
void get_data (char *filename, /* input */
driver_t driver_list[NRACERS] ); /* output */
/* This function already exists. */
/*It opens the data file and reads it into the appropriate places. */
/*-------------------------------------------------------------*/
void print_all(FILE * out_file,
driver_t driver_list[NRACERS] ,
stats_t *race_stats )
/* This function already exists. */
/*-------------------------------------------------------------*/
void do_sort(student_t student_list[NSTUDENTS])
/* This function already exists. */
/*-------------------------------------------------------------*/
/*-------------------------------------------------------------*/
/* THIS IS A SUB-FUNCTION THAT YOU HAVE TO WRITE */
// Remember to include lab7.h and put this code in its own file
void get_stats( driver_t driver_list[NRACERS], /* in & out */
stats_t *race_stats ) /* in & out */
Zero out the average_of_best (HINT: use the -> notation)
Set the slowest_time to the first driver’s first try.
Set the winning_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 , d_best_time.
find the winning and slowest track times. winning_time, slowest_time.
}
add the driver's best time (d_best_time) into the running total of driver’s best times
}
compute the average of the best times. average_of_best.
loop from d=zero to < NRACERS increment by one
{
figure the driver's deviation from the fastest_time
(deviation is fastest time minus driver's best time)
}
return
/*-------------------------------------------------------------*/
/* THIS IS A SUB-FUNCTION THAT YOU HAVE TO WRITE */
// Remember to include lab7.h and put this code in its own file
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.
/*-------------------------------------------------------------*/
The median is the value in the middle of a group of values, assuming that the values are sorted. If there is an odd number of values, the median is the value in the middle. If there is an even number of values, the median is the average of the values in the two middle positions.
EXAMPLES:
This is the sample data example. It does not match the lab7.dat file in length or in value!
Jay Johnson 4.100 5.300 6.700
Missy Monroe 1.000 2.000 3.500
Ned Niner 3.800 7.000 5.500
Lenny Loop 2.200 3.400 4.600
Your Name. Lab 7 output.
Track Results
Drivers Try 1 Try 2 Try 3 Best Time Deviation
-------------------- --------- --------- --------- ---------- ---------
Missy Monroe 1.000 2.000 3.500 1.000 0.000
Lenny Loop 2.200 3.400 4.600 2.200 -1.200
Ned Niner 3.800 7.000 5.500 3.800 -2.800
Jay Johnson 4.100 5.300 6.700 4.100 -3.100
The average of best times = 2.775
The track fast time = 1.000
The track slow time = 7.000
The median of best times = 3.000