Thread: Fine tuning with SORT function using STRUC...真arhhhhh真

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    13

    Fine tuning with SORT function using STRUC...真arhhhhh真

    Hi all, im really getting confused (prolly confusing myself more than anything) with trying to get this function to work.
    The idea is that Student ID's will be entered (Within the main) and then the function to sort them ascending, and obviously print them- (printing is obviously the easy part)

    struct student {
    char *id;
    };

    typedef struct student Student;
    void sort( Student [] );

    main () ...........
    eg: Please enter first student ID: 00027270
    '' '' second ID: 00028578
    '' '' third ID: 00027896

    int i;
    Student student_id[ 3 ];
    student_id[ 0 ].id = "00027270";
    student_id[ 0 ].id = "00028578";
    student_id[ 0 ].id = "00027896";

    sort( all_student ); //call the function

    //SORT function
    void sort( Student s[] ) {
    int i, counter;
    Student hold;
    for ( counter = 0; counter < 3 -1; counter++ ){
    for( i = 0; i < 3 - counter - 1; i++ ){
    if( strcmp( ( s + i )->first, (s + i + 1 )->first ) > 0 ) { // strcmp( s[ i ].first, s[ i + 1 ].first ) > 0
    hold = s[ i ];
    s[ i ] = s[ i + 1 ];
    s[ i + 1 ] = hold;
    }
    }
    }
    }
    with this part:--
    ( strcmp( ( s + i )->first, (s + i + 1 )->first ) > 0 )
    ??=> should this part continue on untill ninth (9) number
    eg: (s + i + 2 )->first, (s + i + 3 )->first etc all the way untill 9.
    or am i going about his the wrong way, like is their an easier way?

    would u just have to reverse the coding to get them to sort in descending order.....

    getting in over my head all, any bit of help would be soooooooooooo much appreciated, u have no idea

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    This reeks of homework.

    Use [ code ][ /code ] tags to post code.

    EDIT: What's ->first?
    If you understand what you're doing, you're not learning anything.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    A board search for qsort would probably throw up plenty of examples
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    13

    i wrote the wrong variable from main ()

    HI, thanks for someone replying,

    i did have a search throughout the C programming boards, and there wasnt much of a function that i coud relate to...

    itsme86 asked
    Quote
    "EDIT: What's ->first? "

    it was meant to be ->id (as im just trying to sort student id's)

    i cant figure out if the strcmp needs anything more as im trying to sort 8 digit id's....

    any more help would be greatly appreciated.
    ps- this isn't homework (if it was id be right stuck in a hole) although yes it is an asignment (due pretty soon).... i thought i got so close to getting it done, but sorta cant do any more.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    13
    ps i will be posting the updated code in a couple of hours
    ta

  6. #6
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    If you don't have to use strcmp() you could simply store each ID as a long int and then do sorting by < and > operations, which would probably be much faster.

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Well, my apologies for making the homework assumption. Is this what you're looking for?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct
    {
      int id;
    } Student;
    
    void sort(Student *list, int num)
    {
      Student *outer, *inner;
      Student temp;
    
      for(outer = list;outer-list < num;++outer)
        for(inner = outer+1;inner-list < num;++inner)
          if(outer->id > inner->id)
          {
            temp = *outer;
            *outer = *inner;
            *inner = temp;
          }
    }
    
    int main(void)
    {
      Student *list = NULL, *ptr;
      int n_students = 0;
      char buf[256];
    
      puts("Enter IDs one per line. Blank line to end.");
      for(;;)
      {
        printf("ID %d: ", n_students+1);
        fflush(stdout);
        fgets(buf, sizeof(buf), stdin);
        if(*buf == '\n')
          break;
        if(!(ptr = realloc(list, sizeof(Student)*(n_students+1))))
        {
          puts("Memory allocation error. Processing existing IDs...");
          break;
        }
        list = ptr;
        list[n_students++].id = atoi(buf);
      }
    
      sort(list, n_students);
    
      for(ptr = list;ptr-list < n_students;++ptr)
        printf("%08d\n", ptr->id);
    
      free(list);
      return 0;
    }
    itsme@dreams:~/C$ ./idsort
    Enter IDs one per line. Blank line to end.
    ID 1: 27270
    ID 2: 28578
    ID 3: 27896
    ID 4:
    00027270
    00027896
    00028578
    itsme@dreams:~/C$
    Last edited by itsme86; 09-14-2004 at 08:47 PM.
    If you understand what you're doing, you're not learning anything.

  8. #8
    Registered User
    Join Date
    Mar 2004
    Posts
    13
    itsme86, dude U RocK!
    Qutoe: "<Is this what you're looking for?>" .... X-actly

    thanks for that, i was wasnt expecting so much, but this is great,
    This program is going to eventually have a alot more Struct's with several more functions..... i can incorporate this code for lots of other tasks that i have planned

    once agian thanks itsme

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    ps- this isn't homework (if it was id be right stuck in a hole) although yes it is an asignment (due pretty soon).... i thought i got so close to getting it done, but sorta cant do any more.
    Ok, wait a second. Do you even know the definition of home work? An assignment "due pretty soon" counts in my book as homework. Don't pretend it isn't.

    I don't know even why I bother any more. Everyone just does all the work for people, none of the new people read any of the Announcements, use code tags or anything else like they're supposed to. God damn I'm getting tired of people.

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    I don't blame you quzah. It's all in the art of handouts.

    Help gears toward improvement and direction, not result or solution. I believe the difference is weighed greatly.
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  11. #11
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Well I guess I'm just the kind of person that, when I receive help in the way I give it, I try my best to understand how and why the author did it the way they did. And if I can't understand it then I ask specifics. It's easier for me to see the whole picture and then try to understand little pieces of it instead of getting told how to do little pieces and then mutate it into what I want.

    But that's just me and everybody's different. Doesn't mean I'm right, doesn't mean you're right. If there's a well-defined policy here that says not to give code in full then I'll stop doing it. Until I see something like that then I'll help people the way that I think is best. If my help isn't welcome then I'll stop posting.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM