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 8440639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:20:40+00:00 2026-06-10T08:20:40+00:00

Error sort_structs_time. Program to use struct and qsort to acccept and store user input;

  • 0

Error sort_structs_time. Program to use struct and qsort to acccept and store user input; firstname, lastname, country and time. Output should be sorted on time using qsort e.g.

input

BEKELE Tariku ETH 27:31.43

RUPP Galen USA 27:30.90

FARAH Mo GB 27:30.42

output

FARAH Mo GB 27:30.42

RUPP Galen USA 27:30.90

BEKELE Tariku ETH 27:31.43

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

struct olympics { 
    //char athlete[25];
    char fname[15];
    char lname[15];
    char country[5];
    float time;
};

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

/* struct array printing function */ 
void print_struct_array(struct olympics *array, size_t len) 
{ 
    size_t i;

    for(i=0; i<len; i++) 
        printf("%s %s %s \t %.2f\n", array[i].fname, array[i].lname, array[i].country, array[i].time);

    puts("****");
} 

/* sorting structs using qsort() */ 
void sort_structs_time(void) 
{ 
    struct olympics structs[] = {
        scanf("%s %s %s %.2f\n", fname, lname, country, &time)
    };

    size_t structs_len = sizeof(structs) / sizeof(struct olympics);

    puts("**** Athletes finishing time...");

    /* print original struct array */ 
    print_struct_array(structs, structs_len);

    /* sort array using qsort functions */ 
    qsort(structs, structs_len, sizeof(struct olympics), struct_cmp_by_time);

    /* print sorted struct array */ 
    print_struct_array(structs, structs_len);
} 


/* 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-10T08:20:41+00:00Added an answer on June 10, 2026 at 8:20 am

    You need to do some changes in your sort_structs_time and struct_cmp_by_time functions.
    It’s obvious that you have not understood C structs and matrices, so do a revision on this topics.

    int struct_cmp_by_time(const void *a, const void *b) 
    { 
        struct olympics *ia = (struct olympics *)a;
        struct olympics *ib = (struct olympics *)b;
    
        if (ia->time < ib->time) return -1;
        else if (ia->time == ib->time) return 0;
        else return 1;
    }
    

    See this qsort documentation and have a look on the comparison function presented there.

    void sort_structs_time() 
    { 
    int i, ath_num;
    
    struct olympics *ath_recs;
    
    printf("For how many athletes do you want to insert their records? \n");
    scanf("%d", &ath_num);
    
    ath_recs = (struct olympics *) malloc(ath_num*sizeof(struct olympics));
    
    printf("Please insert athletes records. \n");
    printf("type a random string and press ENTER when you done. \n");
    for(i = 0; i < ath_num; i++){
        scanf("%s %s %s %f\n", ath_recs[i].fname, ath_recs[i].lname, ath_recs[i].country, &ath_recs[i].time); 
    //Don't put %.2f on scanf!!! 
    //Also, note that the fname, lname, country and time are struct fields, 
    //so you have to access them this way.
    }
    
    
    puts("**** Athletes finishing time...");
    
    /* 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 olympics), struct_cmp_by_time);
    
    /* print sorted struct array */ 
    print_struct_array(ath_recs, ath_num);
    } 
    

    There are other ways also to correct your code. I just find this easier to understand .

    A better representation of time, imo, is as this:

    struct time{
        int mins;
        int secs;
        int fsecs;
    }
    

    so you can print the time this way:

    printf("%d:%d,%d\n", mins, secs, fsecs);
    

    (You have to change the parts of your program related to time if you use this representation, i.e. the comparison function.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Error display as Incorrect syntax near '@Cmp_DocPath', if i use comment Line code I
I cannot sort a compile time error. I am compiling C project. My main.c
With the following Grammar, I get a syntax error with this sort of input:
How do I sort out (distinguish) an error derived from a disk full condition
I'm getting a compiler error for this line: Collections.sort(terms, new QuerySorter_TFmaxIDF(myInteger)); My customized Comparator
Error creating bean with name 'sessionFactory' defined in class path resource [ApplicationContext.xml]: Invocation of
Error on link: ld: duplicate symbol _OBJC_METACLASS_$_Reachability in /Users/jmf/Documents/iOS Development/jmf_Client/jmf/jmf/ww_libs/libmfcbroem_ios.a(Reachability.o) and /Users/tpf/Library/Developer/Xcode/DerivedData/jmf-erlknghfcpoomnfanzovahfacgpv/Build/Intermediates/jmf.build/Debug-iphoneos/jmf.build/Objects-normal/armv7/Reachability.o for architecture
Error I have a web app with a mass uploader (Plupload) for photos and
Error: undefined method `author' for nil:NilClass In my helper: def last_updated(group) g = group.last_updated_version
Error occurred during deployment: Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException:

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.