Problem

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:

  • a character array d_name that is 21 in length, (comes from the data file)
  • a double array of d_tries that has a length of TRIES, (comes from the data file)
  • a double named d_best_time, (value computed by program)
  • a double named deviation, (value computed by program).

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:

  • four variables, all type double, named: average_of_best, winning_time, slowest_time, and median.

(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.

Input/Output Description:

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.

Algorithm Development - Pseudo code:

/*-------------------------------------------------------------*/
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.

/*-------------------------------------------------------------*/

NOTES on the median:

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:

  • The median of values {1, 6, 18, 39, 86} is the middle value, or 18.
  • The median of values {1, 6, 18, 39, 86, 91} is the average of the two middle values, or (18 + 39)/2 or 28.5.

Sample Data:

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

Sample Output:

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
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.