Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

There is one extra structured type called result t which is similar to runner-t

ID: 3582562 • Letter: T

Question

There is one extra structured type called result t which is similar to runner-t except that it only contains one field for a running time. result-t is intended to be used when we are giving updates of recent races, or adding a new runner to the database. In the file running.c, we have declared one global variable of type runclub-t named slowAC, and this will be the database which all our functions refer to We assume that if there are k runners in the database, that slowAC.total will have the value k, and the runners will be stored at indices 0 k 1 of the WAC runners array. (a) Your first task is to implement a function which takes a single parameter of type runtime-t, and computes the minutes per-mile pace represented by this halfmarathon time. You should use the fact that a halfmarathon is 13.1 miles. The function prototype that you must complete is double minpermile(runtime-t time) 15 marks (b) The second task is to implement a function which takes no input arguments, but which returns the unique runner id (the value of the runid field) of the runner who has the fastest pb field in the global database slowAC. You must complete the following function prototype: int fastest 10 marks (c) The final task is to implement a function which takes one argument res of type result-t, and which either uses this input to update the entry of that runner (if res.runid has an entry in the database) or (if the runner with d res.runid is currently missing from the database) to add a new runner entry to the database, and update slowAC.total. When updating the entry, you must update the recent field; pb should also be updated if no better time than res.result is previously known. You must complete the function prototype below: int updateDB(result-t res) The value returned of the function should be 1 unless res .runid is a new runner and the database already has MAXRUNNERS (in which case you should return 0) 10 marks Your code should be entered into running.c. This file contains some helper functions, including a function to initialize the database with a collection of runners. There is also a detailed main function which runs a menu allowing various functions to be tested.

Explanation / Answer

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define MAXRUNNERS 100

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

/* Type declarations which are used for this program. */

/* DO NOT CHANGE (or add to) these type declarations. */

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

typedef struct {

int hr;

int min;

int sec;

} runtime_t;

typedef struct {

char name[30];

int runid;

runtime_t pb;

runtime_t recent;

} runner_t;

typedef struct {

int total;

runner_t runners[MAXRUNNERS];

} runclub_t;

typedef struct {

char name[30];

int runid;

runtime_t result;

} result_t;

/* ----------------- End of type declarations ------------------ */

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

/* We have one global variable storing a runclub_t database.     */

/*   DO NOT CHANGE and DO NOT ADD ANY EXTRA GLOBAL VARIABLES     */

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

runclub_t slowAC;

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

/* The following three function declarations must be completed */

/* as your solution for Q1 of Section B. ---------------------- */

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

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

/* This function should compute (and return) the minutes-per-mile */

/* pace indicated by the given half-marathon (13.1) mile time.   */

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

double minpermile(runtime_t time) {

/* BEGIN ANSWER (a) -- do not delete this line */

Minpermile=(60/(time.min + time.sec/60.))*13.1;

return 0;

/* END ANSWER (a) -- do not delete this line */

}

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

/* This function should search for the runner with the fastest 'pb' */

/* time in the database, and return the user id of this runner, or */

/* the value -1 if the database is empty.                          */

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

int fastest() {

/* BEGIN ANSWER (b) -- do not delete this line */

for (i=1:i<=100:i++)

{

}

return -1;

/* END ANSWER (b) -- do not delete this line */

}

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

/* This function takes one argument of type result_t, searches the */

/* running database for the user id mentioned of the argument. If */

/* the user is already in the database, the runners 'recent' time */

/* must be updated, and possibly the 'pb' (personal best) time. If */

/* the user id is not in the database, a new entry must be created */

/* with the details of the input argument, and the 'total' entry   */

/* of the database must be updated.                                */

/* This function should return 1 *unless* it is a new runner and   */

/* the database is full.                                           */

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

int updateDB(result_t res) {

/* BEGIN ANSWER (c) -- do not delete this line */

{

    int id, flag = 0, i = 1;

    char s[size];

    if (fp1 == NULL)

    {

        printf("File cant be opened");

        return;

    }

    printf("Enter runner id to update : ");

    scanf("%d", &id);

    result_t res = (struct result_t res *)malloc(1*sizeof(struct result_t res));

        result_t res ->name=(char *)malloc(size*sizeof(char));

    while(i<=count)

    {   

        fread(&result_t res ->id, sizeof(result_t res ->id), 1, fp1);

        fread(result_t res ->name,size,1,fp1);

        if (id == result_t res ->id)

        {

            printf("Enter new name of runner to update : ");   

            scanf(" %[^ ]s", s);

            fseek(fp1, -204L, SEEK_CUR);

            fwrite(&result_t res->id, sizeof(result_t res->id), 1, fp1);

            fwrite(s, size, 1, fp1);

            flag = 1;

            break;

        }

        i++;

    }

    if (flag != 1)

    {

        printf("No runner record found");

        flag = 0;

    }

    fclose(fp1);

    free(result_t res->name);        /* to free allocated memory */

    free(result_t res);

    return 1;

/* END ANSWER (c) -- do not delete this line */

}

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

/* -------- END OF FUNCTIONS WHICH MUST BE IMPLEMENTED ---------- */

/* -------- DO NOT CHANGE THE REMAINDER OF THE PROGRAM ---------- */

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

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

/* Initialisation function is to help you test. DO NOT CHANGE. */

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

void initialize() {

runtime_t tempt;

strcpy(slowAC.runners[0].name, "Vicky");

slowAC.runners[0].runid=10;

tempt.hr = 1; tempt.min = 10, tempt.sec= 03;

slowAC.runners[0].pb=tempt;

slowAC.runners[0].recent=tempt;

strcpy(slowAC.runners[1].name, "Haile");

slowAC.runners[1].runid=15;

tempt.hr = 1; tempt.min = 01, tempt.sec= 16;

slowAC.runners[1].pb=tempt;

slowAC.runners[1].recent=tempt;

strcpy(slowAC.runners[2].name, "Paula");

slowAC.runners[2].runid=6;

tempt.hr = 1; tempt.min = 22, tempt.sec= 13;

slowAC.runners[2].pb=tempt;

slowAC.runners[2].recent=tempt;

strcpy(slowAC.runners[3].name, "Gary");

slowAC.runners[3].runid=22;

tempt.hr = 1; tempt.min = 42, tempt.sec= 35;

slowAC.runners[3].pb=tempt;

slowAC.runners[3].recent=tempt;

strcpy(slowAC.runners[4].name, "Eamonn");

slowAC.runners[4].runid=14;

tempt.hr = 1; tempt.min = 10, tempt.sec= 11;

slowAC.runners[4].pb=tempt;

slowAC.runners[4].recent=tempt;

strcpy(slowAC.runners[5].name, "Sonia");

slowAC.runners[5].runid=33;

tempt.hr = 1; tempt.min = 03, tempt.sec= 20;

slowAC.runners[5].pb=tempt;

slowAC.runners[5].recent=tempt;

strcpy(slowAC.runners[6].name, "Barry");

slowAC.runners[6].runid=17;

tempt.hr = 1; tempt.min = 32, tempt.sec= 50;

slowAC.runners[6].pb=tempt;

slowAC.runners[6].recent=tempt;

slowAC.total=7;

}

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

/* Function to print a time in --:--:-- format. DO NOT CHANGE. */

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

void printtime(runtime_t time) {

printf("%2d:%2d:%2d", time.hr, time.min, time.sec);

}

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

/* This function searches for a runner with the given id, and    */

/* outputs runner details if the runner is in the database. It   */

/* is provided to help with your testing. DO NOT CHANGE.        */

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

void runnerdetails(int id) {

int r=0, found = 0; runner_t person;

while ((r<slowAC.total) && !(found)) {

    if (slowAC.runners[r].runid == id) {

      found = 1;

      person = slowAC.runners[r];

      printf(" %s (id %d). ", person.name, person.runid);

      printf("PB is ");

      printtime(person.pb);

      printf(" recent time is ");

      printtime(person.recent);

      printf(" ");

    }

    r++;

}

if (!found)

    printf("No existing half-marathon results for id %d. ", id);

}

/* ----------- end of pre-implemented functions -------------- */

/* DO NOT ALTER THE main PROGRAM */

/* THIS IS DESIGNED TO MAKE IT EASY TO TEST YOUR FUNCTIONS */

int main(void) {

int option, flag, i, id;

runtime_t result;

result_t newres;

slowAC.total =0;

option = 8;

while(option != 0) {

    printf("Options are 1 (test 'minpermile'), 2 (initialise database), ");

    printf("3 (Add new half-marathon result), 4 (find fastest runner), ");

    printf("5 (print a runner's details), 6 (print number of runners), 0 (exit): ");

    printf("Enter an option: ");

    scanf("%d", &option);

    switch(option) {

    case 1:

      printf(" Enter hours, mins, secs in --:--:-- format:");

      scanf("%d:%d:%d", &(result.hr), &(result.min), &(result.sec));

      printf("For a half-marathon, this gives a min-per-mile pace of ");

      printf("%f ", minpermile(result));

      break;

    case 2:

      initialize();

      printf(" The database has been initialised with 7 runners. ");

      break;

    case 3:

      printf(" Enter the runner name: ");

      scanf("%s", newres.name);

      printf("Enter runner id: ");

      scanf("%d", &(newres.runid));

      printf("Enter new time in --:--:-- format: ");

      scanf("%d:%d:%d", &(newres.result.hr), &(newres.result.min), &(newres.result.sec));

      flag = updateDB(newres);

      if (flag)

        printf("DB updated with new result. ");

      else

        printf("Update not possible. ");

      break;

    case 4:

      i = fastest();

      if (i == -1)

        printf(" Database is empty, no fastest runner. ");

      else {

        printf(" We have found the fastest runner. ");

        runnerdetails(i);

      }

      break;

    case 5:

      printf(" Enter the user id you want details for: ");

      scanf("%d", &id);

      runnerdetails(id);

      printf(" ");

      break;

    case 6:

      printf(" The current number of runners is %d. ", slowAC.total);

    }

}

return EXIT_SUCCESS;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote