Help! Sorting a Structure??

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sevenrules
    New Member
    • Mar 2007
    • 4

    #1

    Help! Sorting a Structure??

    Basically: How do I sort the structure here:

    struct prob {
    int num1[400];
    int num2[400];
    double pro[400];
    };


    according to the values in 'pro'... while keeping pro[x] related to num1[x] and num2[x]...

    num1[x] is a number that corresponds to something specific
    num2[x] is a number as well in the same way
    prob[x] corresponds to the probability of behavior 1 (num2) and behavior 2 (num2) occuring...

    Any ideas??
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Originally posted by sevenrules
    Basically: How do I sort the structure here:

    struct prob {
    int num1[400];
    int num2[400];
    double pro[400];
    };


    according to the values in 'pro'... while keeping pro[x] related to num1[x] and num2[x]...

    num1[x] is a number that corresponds to something specific
    num2[x] is a number as well in the same way
    prob[x] corresponds to the probability of behavior 1 (num2) and behavior 2 (num2) occuring...

    Any ideas??
    you have your structure wrong.

    The members num1, num2 and pro are related, you want an array of structures not a structure of arrays

    Code:
    struct prob {
    	int num1;
    	int num2;
    	double pro;
    };
    
    struct prob MyData[400];
    Once you have made this change you can just use the qsort function in the standard library.

    Comment

    • sevenrules
      New Member
      • Mar 2007
      • 4

      #3
      Yeah I just figured that out after I posted...

      I'm having trouble trying to put values in now though... how do I do that?

      I haven't done this in about year so bear with me...

      Comment

      • sevenrules
        New Member
        • Mar 2007
        • 4

        #4
        never mind figured it out

        Comment

        • sevenrules
          New Member
          • Mar 2007
          • 4

          #5
          How do I use qsort now? I can't figure it out based on google searches... any help?

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Did you find this link?

            It pretty much explains it, however qsort has this prototype

            Code:
            void qsort(void * base, 
                       size_t num, 
                       size_t size, 
                       int ( * comparator ) ( const void *, const void * ) );
            It takes 4 parameters and it sorts an array of any sized elements using a comparison function provided by the user (caller).

            The first 3 parameters are a pointer to the first element of the array, the number of elements in the array and the size of each element in the array. This information basically allows the function to correctly address every element of the array without having to know the type of the array elements.

            Because qsort has no knowledge of the type of each array element it does not know how to compare them. It gets round this by having the caller passing a pointer to a function that will compare 2 elements of the array and return a result, less than, equal or greater than.

            This function takes 2 parameters, these are pointer to array elements and are passed as void *. They should be cast to a pointer to the array element type, then the function should perform the comparison on the 2 elements and return <0 is parameter1 < parameter2, 0 if parameter1 == parameter2 and >0 if parameter1 > parameter2.

            Code:
            #include <stdlib.h>
            #include <stdio.h>
            #include <time.h>
            
            static const int HIGHLIMIT = 100;
            
            static int CompFn(const void *op1, const void *op2);
            
            int main(int argc, char **argp)
            {
                int array[10];
                int ix;
                int interval = RAND_MAX / HIGHLIMIT;
                int rand_max = interval * HIGHLIMIT;
            
            
                srand((unsigned)time(NULL));
            
                printf("List  : ");
            
                for(ix=0; ix<sizeof array/sizeof array[0]; ix++)
                {
                    array[ix] = (rand() % rand_max) / interval + 1;
            
                    printf("%3d ", array[ix]);
                }
            
                putchar('\n');
            
                qsort(array, sizeof array/sizeof array[0], sizeof array[0], CompFn);
            
                printf("Sorted: ");
            
                for(ix=0; ix<sizeof array/sizeof array[0]; ix++)
                {
                    printf("%3d ", array[ix]);
                }
            
                putchar('\n');
            
                return EXIT_SUCCESS;
            }
            
            int CompFn(const void *op1, const void *op2)
            {
                return *(int *)op1 - *(int *)op2;
            }

            Comment

            Working...