Entering and splitting string

This is a discussion on Entering and splitting string within the C Programming forums, part of the General Programming Boards category; Now I have another problem. I'm unable to sort the names+ages alphabetically. I display them correctly, but I cannot find ...

  1. #16
    Registered User
    Join Date
    Dec 2007
    Posts
    16
    Now I have another problem. I'm unable to sort the names+ages alphabetically. I display them correctly, but I cannot find a suitable way to sort the 2d string array and display it (just display it sorted without rearranging the array order). I searched and I found qsort(), which I couldn't get to work, I also tried using strcpy() and strcmp() also unsuccessfully.

  2. #17
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    It's really quite simple. Assuming you know how to code a bubble sort, let's say these are some fragments of your number-handling code:
    Code:
    int array[ELEMENTS];  /* declare the number array */
    scanf("%d", &array[x]);  /* read in a number */
    printf("%d\n", array[x]);  /* print a number */
    if(array[x] > array[x+1])) {  /* compare two numbers (part of bubble sort) */
    array[x] = temp;  /* assign to a number (part of swap) */
    void function(int array[]) {  /* pass array to function */
    The corresponding string-handling code would look like this:
    Code:
    char array[ELEMENTS][MAX_STRING_LENGTH];  /* declare the string array */
    scanf("%s", array[x]);  /* read in a string */
    printf("%s\n", array[x]);  /* print a string */
    if(strcmp(array[x], array[x+1]) > 0) {  /* compare two strings (part of bubble sort) */
    strcpy(array[x], temp);  /* assign to a string (part of swap) */
    void function(char array[ELEMENTS][]) {  /* pass array to function */
    I said, "assuming you know how to code a bubble sort". If you don't, have a look at some online tutorials about bubble sorts.
    http://www.cprogramming.com/tutorial.../sorting1.html
    http://en.wikipedia.org/wiki/Bubble_sort

    Or have a look at this:
    Code:
    void sort_numbers(int array[], size_t elements) {
        size_t x = 0;
        int temp, done = 0;
    
        while(!done) {
            done = 1;
    
            for(x = 0; x + 1 < elements; x ++) {
                if(array[x] > array[x+1]) {
                    /* swap array[x] and array[x+1] */
                    temp = array[x];
                    array[x] = array[x+1];
                    array[x+1] = temp;
                }
            }
        }
    }
    You can use int instead of size_t if you want -- I just like using variables of type size_t for indexing arrays.

    You mentioned qsort(). qsort() is a C library function that performs a quick sort (usually) -- much more efficient than a bubble sort, but not as efficient as a quick sort could be, because qsort() has to be generic. (Quick sort is generally regarded as one of the fastest sorting algorithms.)

    I say usually because, despite its name, qsort() can use any sorting algorithm. Most implementations do use quick sort, however. Not that you, the programmer, care. As long as it gets sorted, who cares how it does it?

    To use quick sort, you need to create a function that will tell qsort() if the first of two elements is less than, greater than, or equal to the second. Here's an example for numbers: http://www.cplusplus.com/reference/c...lib/qsort.html

    Here's one that's a bit easier to understand:
    Code:
    int compare(const void *a, const void *b) {
        int x = *(int *)a, y = *(int *)b;
        if(x < y) return -1;
        else if(x > y) return 1;
        else return 0;  /* x == y */
    }
    For strings, you can get away with
    Code:
    int compare(const void *a, const void *b) {
        return strcmp(a, b);
    }
    because strcmp() does just what qsort is looking for.

    Then you call qsort like this:
    Code:
        int n, values[] = {1, 2, 3, 4, 5, 6};
        qsort(values, sizeof(values)/sizeof(*values), sizeof(int), compare);
    (Instead of sizeof(values)/sizeof(*values), you could use 6, I suppose. )

    That "compare" argument is the address of the function compare(). Never mind how it works, just put the name of your function in there. &compare and compare work equally well.
    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. #18
    Registered User
    Join Date
    Dec 2007
    Posts
    16
    Thank you, I did it with bubble sorting, it's working fine.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21