Thread: Sorting exam score in increasing order

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    4

    Post Sorting exam score in increasing order

    Hello, I'm a first year university student that has just started learning C language. Right now, I'm trying to learn the section on "Sorting" and found this program online, which generates ramdom student names and scores, and sort the scores out in increasing order. As indicated on the attached program (sort.c), I want to make the labled section as a separate function outside of the main function i.e. sort_mod.c <---not working

    Could someone teach me how would I go about doing this? Thanx in advance!

    Best regards, superman

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    That code doesn't free the malloc()ed memory. You should free it.

    First of all, you should put in a prototype for your function.

    Code:
    void mysort(int group[].point,SIZE)
    SIZE has to have a type, like int SIZE. Unless you're referring to the #define SIZE. In that case you don't need to pass it as an argument at all.
    Code:
    void mysort(struct student group[])
    And when you call your function (from main()), you would do it like this:
    Code:
    mysort(group);
    Read up on structs and functions.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    4
    Thank you very much your help! I appreciate it !

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    4
    I modified the program and compiled with GCC, and now it gives me the following errors!

    sort_mod.c:14: error: syntax error before '.' token
    sort_mod.c: In function `mysort':
    sort_mod.c:27: error: incompatible types in assignment
    sort_mod.c:27: error: incompatible types in assignment

    Could someone tell me what's wrong with the program?? Thanx in advance!

    Best regards, superman

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    The prototype for mysort is still wrong, make it more like:
    void mysort(struct student group[]);

    The w variable within mysort should be the same type that you wish to swap around:
    struct student w;
    (not int w;)
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Jul 2005
    Posts
    4
    Thanx!

    Regards, superman

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C program using structs to calculate grades
    By TampaTrinDM88 in forum C Programming
    Replies: 4
    Last Post: 07-06-2009, 12:33 PM
  2. trouble with creating a loop to read certain number of inputs
    By import tuner650 in forum C++ Programming
    Replies: 2
    Last Post: 03-20-2008, 07:28 PM
  3. new problem with class
    By jrb47 in forum C++ Programming
    Replies: 0
    Last Post: 12-01-2006, 08:39 AM
  4. Sorting in alphabetical order
    By fkheng in forum C Programming
    Replies: 3
    Last Post: 08-24-2003, 09:07 AM
  5. Sorting in Alphabetical order
    By Andre Santiago in forum C Programming
    Replies: 1
    Last Post: 12-13-2002, 06:14 PM