I have the shell for this program already I just need the tw

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 <stdio.h>
#include <stdlib.h>

#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 <stdio.h>
#include <stdlib.h>

#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<NRACERS; ++i){
       driver_list[i].deviation = 0;
       driver_list[i].best_time = driver_list[i].tries[0];
       int j;
       for(j=0; j<TRIES; ++j){
           driver_list[i].best_time = driver_list[i].tries[j] < driver_list[i].best_time ? driver_list[i].tries[j] : driver_list[i].best_time;
           race_stats -> 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<NRACERS; ++i){
       driver_list[i].deviation = race_stats -> 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

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 YO
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 YO
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 YO
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 YO
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 YO
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 YO
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 YO
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 YO
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 YO

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site