Thread: help to sort array of structs

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    19

    help to sort array of structs

    hi i need to sort this array of structures

    i tried using bubble sort but there is something wrong with wut i have so far...it is outputting a few of the elements in the array and repeating them more than once

    here is my code (i attached the input file im reading from)

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    typedef struct {
    int size;
    char name[21];
    int boxnumber;
    }item;
    
    
    void swap(item *a, item *b){
    
    item tmp;
    
    tmp = *a;
    *a = *b;
    *b = tmp;
    }
    
    void bubbleSort(item a[], int size){
    
    int i, j;
    
    for (i=0; i<size-1; i++){
    for (j=size-1; j>i; j--)
    if
    (a[j].size < a[j-1].size)
    swap(&a[j], &a[j-1]);
    
    }
    }
    
    
    int main() {
    int s,n; 
    int i;
    
    
    
    FILE * iFile;
    iFile = fopen ("inputzou.txt","r");
    
    fscanf (iFile, "%d, %d", &s, &n);
    
    
    printf ("%d, %d\n", s, n);
    
    for (i=0;i<=n;i++){
    item *array;
    array=(item *) malloc (sizeof(item));
    array[i].boxnumber=0; //initialize boxnumber to 0
    
    fscanf(iFile, "%s %d", array[i].name, &array[i].size);
    
    
    bubbleSort(array,n);
    
    printf("%s %d\n", array[i].name, array[i].size);
    
    
    
    
    free(array);
    }
    fclose(iFile);
    
    
    
    return 0;
    
    
    }

  2. #2
    Registered User
    Join Date
    Jun 2006
    Posts
    75
    You aren't reading the data properly into an array. I've changed the code to the following and it works (hopefully ):

    Code:
    /*My comments are in red*/
    #include <stdio.h>
    #include <stdlib.h>
    /*You don't need math.h*/
    
    typedef struct {
        int size;
        char name[21];
        int boxnumber;
    }item;
    
    void swap(item *a, item *b){
        item tmp;
        tmp = *a;
        *a = *b;
        *b = tmp;
    }
    
    void bubbleSort(item a[], int size){
        int i, j;
    
        for (i=0; i<size-1; i++){
            for (j=size-1; j>i; j--)
                if (a[j].size < a[j-1].size)
                    swap(&a[j], &a[j-1]);
        }
    }
    
    int main(void) {
        int s,n; 
        int i;
        item *array;
    
        FILE *iFile;
        iFile = fopen ("inputzou.txt","r");
    
        fscanf (iFile, "%d, %d", &s, &n);
    
        printf ("%d, %d\n", s, n);
        
        array = calloc (n, sizeof(item)); /*Don't cast the return value of malloc or calloc*/
    
        for (i=0;i<n;i++){ /*You're reading n values, not n+1, so the condition shouldn't be i<=n*/
            array[i].boxnumber=0; /*initialize boxnumber to 0*/ /*using // comments isn't completely portable*/ 
            fscanf(iFile, "%s %d", array[i].name, &array[i].size);
        }
        
        bubbleSort(array, n);
        
        for (i=0; i<n; i++) {
            printf("%s %d\n", array[i].name, array[i].size);
        }
    
        free(array);
    
        fclose(iFile);
    
        return 0;
    }
    Output:
    295, 59
    Sanctuary_Records 34
    Kittie 38
    Led_Zeppelin 42
    Smashing_Pumpkins 42
    Goldfinger 43
    Rammstein 46
    Static-X 47
    Alexisonfire 48
    Reset 48
    Puddle_Of_Mudd 49
    Taproot 49
    U2 50
    Abandoned_Pools 52
    HIM 52
    Godsmack 53
    Garbage 54
    Savage_Garden 59
    30_Seconds_To_Mars 60
    3_Doors_Down 61
    Lifehouse 61
    Hoobastank 64
    Alient_Ant_Farm 65
    I_Mother_Earth 66
    Cold_Play 68
    Nine_Inch_Nails 70
    Weezer 71
    The_White_Stripes 73
    RageAgainstTheMachin 75
    AFI 76
    Nickelback 76
    Trapt 77
    The_Get_Up_KIds 80
    Massive_Attack 81
    Adema 82
    System_of_a_down 86
    Kidrock 91
    Pink 110
    Incubus 114
    Nodoubt 117
    Disturbed 118
    Mudvayne 121
    Nirvana 122
    QueensOfTheStoneAge 123
    Matthew_Good 125
    The_Offspring 128
    Green_Day 130
    RedHotChiliPeppers 130
    Staind 130
    Deftones 138
    Evanescence 139
    SlipKnot 140
    FooFighters 141
    Papa_Roach 154
    Linkin_park 181
    Blink182 191
    Pink_Floyd 202
    Limp_Bizkit 234
    Michael_Jackson 238
    Korn 246

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. return an array of structs
    By MK27 in forum C Programming
    Replies: 14
    Last Post: 02-14-2010, 08:31 PM
  2. better way to scanf of a array of structs member?
    By stabu in forum C Programming
    Replies: 3
    Last Post: 07-17-2008, 04:51 PM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. Recursive Array Sort
    By Nakeerb in forum C++ Programming
    Replies: 3
    Last Post: 12-13-2002, 08:27 PM
  5. sorting an array of structs
    By singletrackmind in forum C++ Programming
    Replies: 8
    Last Post: 11-13-2001, 03:33 AM