Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8456263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:30:11+00:00 2026-06-10T12:30:11+00:00

I dont know where I ‘m going wrong. Code below is expected to accept

  • 0

I dont know where I ‘m going wrong. Code below is expected to accept user input of olympic swimmers’ fname, lname country and finishing time and qsort the result on the fastest time as below;

**Sample Input**
ADLINGTON Rebecca GBR 4:03.01 
MUFFAT Camille FRA 4:01.45  
FRIIS Lotte DNK 4:03.98 
SCHMITT Allison USA 4:01.77

**Sample Output**
MUFFAT Camille FRA 4:01.45 
SCHMITT Allison USA 4:01.77 
ADLINGTON Rebecca GBR 4:03.01 
FRIIS Lotte DNK 4:03.98
struct mtime_t {
    int mins;
    int secs;
    int fsecs;
};
typedef struct mtime_t mtime;

struct olympians { 
    char fname[15];
    char lname[15];
    char country[3];
    int time;
    int mins, secs, fsecs;
    mtime otime;
};

/* qsort struct comparision function (time float field) */ 
int struct_cmp_by_time(const void *a, const void *b) 
{ 
    struct olympians *ia = (struct olympians *)a;
    struct olympians *ib = (struct olympians *)b;
    return (int)(60*ia->time - 60*ib->time); 
}

/* struct array printing function */ 
void print_struct_array(struct olympians *array, size_t len) 
{ 
    size_t i;
    for(i=0; i<len; i++) 
        printf("%s %s %s \t %d:%d,%d\n", array[i].lname, array[i].fname, 
               array[i].country, &array[i].otime.mins, 
           &array[i].otime.secs, &array[i].otime.fsecs);

    puts("\n");
} 

/* sorting structs using qsort() */ 
void sort_structs_time() 
{ 
    int i, ath_num;

    struct olympians *ath_recs;
    scanf("%d", &ath_num);

    ath_recs = (struct olympians *) malloc(ath_num*sizeof(struct olympians));

    for(i = 0; i < ath_num; i++) {
        scanf("%s %s %s %d:%d,%d\n", ath_recs[i].fname, ath_recs[i].lname, 
              ath_recs[i].country, &ath_recs[i].mins, &ath_recs[i].secs, &ath_recs[i].fsecs);
    }

    puts("\n");

    /* print original struct array */ 
    print_struct_array(ath_recs, ath_num);

    /* sort array using qsort function */ 
    qsort(ath_recs, (size_t) ath_num, sizeof(struct olympians), struct_cmp_by_time);

    /* print sorted struct array */ 
    print_struct_array(ath_recs, ath_num);
}  

/* call to the function) */ 
int main() 
{ 
    /* run the function */
    sort_structs_time();
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T12:30:12+00:00Added an answer on June 10, 2026 at 12:30 pm

    Another problem is that your input and output formats use , as the decimal point; your sample data uses . as the decimal point.

    And another problem is that you don’t set the time field to any value during input. I’d probably go with:

    for (i = 0; i < ath_num; i++)
    {
        if (scanf("%s %s %s %d:%2d,%2d\n", ath_recs[i].fname, ath_recs[i].lname,
                  ath_recs[i].country, &ath_recs[i].mins, &ath_recs[i].secs,
                  &ath_recs[i].fsecs) != 6)
            break;
        ath_recs[i].time = (ath_recs[i].mins * 60 + ath_recs[i].secs) * 100 +
                           ath_recs[i].fsecs;
    }
    

    If I was being paranoid, I’d ensure that the minutes, seconds and fractional seconds were all zero or positive (non-negative). And I’d probably write the code to read a whole line into a buffer with fgets() and then parse with sscanf(); it makes error detection easier.

    char buffer[4096];
    int i = 0;
    
    while (fgets(buffer, sizeof(buffer), stdin))
    {
        if (i >= ath_num)
            ...too much data...
        if (sscanf(buffer, "%s %s %s %d:%2d,%2d\n", ath_recs[i].fname, ath_recs[i].lname,
                  ath_recs[i].country, &ath_recs[i].mins, &ath_recs[i].secs,
                  &ath_recs[i].fsecs) != 6)
        {
             ...report problems...but I've got the whole line to identify which record
             ...is giving the problem — which helps you and the user...
             break;
        }
        ...other checking...
        ath_recs[i].time = (ath_recs[i].mins * 60 + ath_recs[i].secs) * 100 +
                           ath_recs[i].fsecs;
        i++;
    }
    
    ...actual number of athletes is in i...
    

    It would be better not to make the user do the counting of the data; computers are good at counting. You just have to allocate the array as you go, which requires a modicum of care to avoid quadratic behaviour.

    Then, in the comparison code, there’s no need to multiply by 60:

    /* qsort struct comparision function (time float field) */ 
    int struct_cmp_by_time(const void *a, const void *b) 
    { 
        const struct olympians *ia = (struct olympians *)a;
        const struct olympians *ib = (struct olympians *)b;
        if (ia->time > ib->time)
            return +1;
        else if (ia->time < ib->time)
            return -1;
        ...else if ... tie-breaker decisions - name? ...
        return 0; 
    }
    

    I use the structure shown for comparator functions because it is extensible, and because it avoids problems with arithmetic overflow. In this application, it is very unlikely that the difference between two times will ever cause problems, but avoiding trouble is still a good idea, and subtracting two integers could lead to overflow (wraparound) in general.


    In your code:

        printf("%s %s %s \t %d:%d,%d\n", array[i].lname, array[i].fname, 
               array[i].country, &array[i].otime.mins, 
           &array[i].otime.secs, &array[i].otime.fsecs);
    

    My compiler complains about the & in front of mins, secs and fsecs. If your compiler isn’t doing that, turn up the warning levels until it does, or get a better compiler.

    Your athlete printing code uses the otime sub-structure of struct olympians, but your code doesn’t set it (and it duplicates the values in the separate members mins, secs, fsecs). That threw me off track for a bit while debugging your code. This code works. It includes a separate little function print_athlete() (which should probably be print_olympian()) to print a single athlete, and the print_struct_array() code uses it — but I was also able to use it while working out why the input data wasn’t being printed in the output (that call is still in the code). Echoing input after reading it is a basic debugging technique. The code also checks that malloc() succeeded. (I usually have a function such as void dump_olympian(FILE *fp, const char *tag, const struct olympian *athlete); to print a complex structure to a nominated file. The tag is printed too, which allows me to annotate each call to to the dump function. The dump function should normally dump every element of the structure.)

    In production code, I have a set of functions such as extern void err_error(const char *fmt, ...); which report an error with an interface like the printf() functions; err_error() exits too. This reduces the 4 lines of error reporting to just 1, which means I’m more likely to do it.

    For the input data (note switch of . and ,):

    4
    ADLINGTON Rebecca GBR 4:03,01 
    MUFFAT Camille FRA 4:01,45  
    FRIIS Lotte DNK 4:03,98 
    SCHMITT Allison USA 4:01,77
    

    The output is:

    Processing 4 athletes
    Rebecca ADLINGTON GBR    4:3,1
    Camille MUFFAT FRA   4:1,45
    Lotte FRIIS DNK      4:3,98
    Allison SCHMITT USA      4:1,77
    
    
    Rebecca ADLINGTON GBR    4:3,1
    Camille MUFFAT FRA   4:1,45
    Lotte FRIIS DNK      4:3,98
    Allison SCHMITT USA      4:1,77
    
    
    Camille MUFFAT FRA   4:1,45
    Allison SCHMITT USA      4:1,77
    Rebecca ADLINGTON GBR    4:3,1
    Lotte FRIIS DNK      4:3,98
    

    The first block is from the debug printing in the input loop; the other two are the before and after images, of course, from your original code.


    Unfixed issue

    There’s one problem you’ll need to resolve (in two parts). The easy bit is that the output:

    Rebecca ADLINGTON GBR    4:3,1
    

    should be

    Rebecca ADLINGTON GBR    4:3,01
    

    or even:

    Rebecca ADLINGTON GBR    4:03,01
    

    This is fixed by using %.2d instead of %d in the print formats.

    The hard part is that if the input time string is:

    4:03,1
    

    it needs to be treated as 4:03,10 and not as 4:03,01. Worse, 4:3,9 should be 4:03,90, not 4:03,09; that gives the athlete a 0.81 second advantage in the sorting, simply because the trailing zero was omitted (so it really does matter). This will require different input code; I might even go so far as to read the fraction part into a string of length 3 with %2[0-9], and then post-process that into a number. That way you can tell whether one or two digits were entered.

    Incidentally, you could avoid the conversion to time altogether by sorting directly on the component parts, and then the systematic structure of the comparator becomes beneficial:

    int struct_cmp_by_time(const void *a, const void *b) 
    { 
        const struct olympians *ia = (struct olympians *)a;
        const struct olympians *ib = (struct olympians *)b;
        int rc;
        if (ia->mins > ib->mins)
            return +1;
        else if (ia->mins < ib->mins)
            return -1;
        else if (ia->secs > ib->secs)
            return +1;
        else if (ia->secs < ib->secs)
            return -1;
        else if (ia->fsecs > ib->fsecs)
            return +1;
        else if (ia->fsecs < ib->fsecs)
            return -1;
        else if ((rc = strcmp(ia->lname, ib->lname)) < 0)
            return -1;
        else if (rc > 0)
            return +1;
        else if ((rc = strcmp(ia->fname, ib->fname)) < 0)
            return -1;
        else if (rc > 0)
            return +1;
        return 0; 
    }
    

    (On the whole, you were pretty close to getting your code right — well done.)

    Mildly hacked but working code

    Not all the recommended changes are in this code, but it produces reasonable output, more or less.

    #include <stdio.h>
    #include <stdlib.h>
    
    struct mtime_t {
        int mins;
        int secs;
        int fsecs;
    };
    typedef struct mtime_t mtime;
    
    struct olympians { 
        char fname[15];
        char lname[15];
        char country[4];
        int time;
        int mins, secs, fsecs;
        mtime otime;
    };
    
    /* qsort struct comparison function (time float field) */ 
    int struct_cmp_by_time(const void *a, const void *b) 
    { 
        struct olympians *ia = (struct olympians *)a;
        struct olympians *ib = (struct olympians *)b;
        return (int)(60*ia->time - 60*ib->time); 
    }
    
    static void print_athlete(const struct olympians *ath)
    {
        printf("%s %s %s \t %d:%d,%d\n", ath->lname, ath->fname, ath->country,
                ath->mins, ath->secs, ath->fsecs);
    }
    
    /* struct array printing function */ 
    void print_struct_array(struct olympians *array, size_t len) 
    { 
        size_t i;
        for(i=0; i<len; i++) 
            print_athlete(&array[i]);
        puts("\n");
    } 
    
    /* sorting structs using qsort() */ 
    void sort_structs_time(void) 
    { 
        int i, ath_num;
    
        struct olympians *ath_recs;
        scanf("%d", &ath_num);
        printf("Processing %d athletes\n", ath_num);
    
        ath_recs = (struct olympians *) malloc(ath_num*sizeof(struct olympians));
        if (ath_recs == 0)
        {
            fprintf(stderr, "Out of memory\n");
            exit(1);
        }
    
        for(i = 0; i < ath_num; i++) {
            if (scanf("%14s %14s %3s %d:%2d,%2d\n", ath_recs[i].fname, ath_recs[i].lname, 
                  ath_recs[i].country, &ath_recs[i].mins, &ath_recs[i].secs, &ath_recs[i].fsecs) != 6)
            {
                fprintf(stderr, "Ooops\n");
                exit(1);
            }
            ath_recs[i].time = (ath_recs[i].mins * 60 + ath_recs[i].secs) * 100 +
                                   ath_recs[i].fsecs;
            print_athlete(&ath_recs[i]);
        }
    
        puts("\n");
    
        /* print original struct array */ 
        print_struct_array(ath_recs, ath_num);
    
        /* sort array using qsort function */ 
        qsort(ath_recs, (size_t) ath_num, sizeof(struct olympians), struct_cmp_by_time);
    
        /* print sorted struct array */ 
        print_struct_array(ath_recs, ath_num);
    }  
    
    /* call to the function) */ 
    int main(void) 
    { 
        /* run the function */
        sort_structs_time();
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I dont know whats wrong with this Code. The left hover works properly but
I dont know what am doing wrong with this piece of Code. Its making
I dont know whats wrong going on... I am not able to start a
i dont know what is actual problem in the below code, but it doesn't
Dont know what I am doing wrong... Have this code: new_model = Model.new(:brand_id=>brand_id, :name=>new_model_name)
I dont know where exactly I am going wrong here. I have narrowed the
I dont know where I m going wrong.. I want to replace with I
I dont know how to make this with simple and easy code. I can
I dont know for which iPhone OS I am compiling my code, 3.0 or
I dont know what is going on with my java process. This process is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.