Please Help with sorting?

This is a discussion on Please Help with sorting? within the C Programming forums, part of the General Programming Boards category; Hello friends, here is the entire program. My only problem is that the call function to sort(); will not work. ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    11

    Please Help with sorting?

    Hello friends, here is the entire program. My only problem is that the call function to sort(); will not work. What am I missing in this call function sort(names,size); ?

    Please help.

    Thank you.



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

    #define MAX 4

    void sort(char array[], int);

    main()
    {
    int i, size=MAX;
    char names[MAX][20];

    printf("\t\t-----Enter 10 peoples names-----\n\n");

    for(i=0; i<MAX; i++)
    scanf("%s", names[i]);

    printf("\t\t-----Names entered were as follows-----\n\n");
    for(i=0; i<MAX; i++)
    printf("%s\n", names[i]);

    sort(names,size);

    printf("\t\t-----Names sorted look as follows-----\n\n");
    for(i=0; i<MAX; i++)
    printf("%s\n", names[i]);

    return 0;
    }


    void sort(char * array[], int size)
    {
    char* temp;
    int i, j;

    for (i = 0; i < size; ++i)
    for (j = 0; j < size; ++j)
    if (strcmp(array [i], array[j]) < 0)
    {
    temp = array[i];
    array[i] = array[j];
    array[j] = temp;
    }
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > void sort(char array[], int);
    Well this doesn't match definition and use

    Try
    void sort(char array[MAX][20], int);

    And in your sort function, you have
    char* temp;

    But this isn't the case is it - you dont have a pointer, you have an array

    Which means you need to strcpy each array row to save/restore it.


    Here
    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <string.h> 
    
    #define MAX 4 
    
    void sort( char array[MAX][20], int size); 
    
    int main() { 
        int i, size=MAX; 
        char names[MAX][20]; 
    
        printf("\t\t-----Enter 10 peoples names-----\n\n"); 
    
        for(i=0; i<MAX; i++) 
            scanf("%s", names[i]); 
    
        printf("\t\t-----Names entered were as follows-----\n\n"); 
        for(i=0; i<MAX; i++) 
            printf("%s\n", names[i]); 
    
        sort(names,size); 
    
        printf("\t\t-----Names sorted look as follows-----\n\n"); 
        for(i=0; i<MAX; i++) 
            printf("%s\n", names[i]); 
    
        return 0; 
    } 
    
    
    void sort ( char array[MAX][20], int size) 
    { 
        char temp[20]; 
        int i, j; 
    
        for (i = 0; i < size; ++i) 
            for (j = 0; j < size; ++j) 
                if (strcmp(array[i], array[j]) < 0) 
                { 
                    strcpy(temp,array[i]); 
                    strcpy(array[i],array[j]); 
                    strcpy(array[j],temp); 
                } 
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 06-11-2009, 11:27 AM
  2. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  3. sorting structure members using pointers
    By robstr12 in forum C Programming
    Replies: 5
    Last Post: 07-25-2005, 05:50 PM
  4. Still Needing Help : selection sorting
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 10-14-2001, 08:41 PM
  5. selection sorting
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 10-13-2001, 08:05 PM

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