sorting structs 
Author Message
 sorting structs

Does anybody know how to sort an array within a struct?  I'm a begining
programmer and I haven't taken a data structures course yet so if you
could please keep it fairly elementary I would truly appreciate it.  

I tried using bubble-sort and strcmp but to no use.  I had a lot of
trouble implementing it.  

Here's my struct:

struct tapelist /* Global structure definition */
{
        int tape_number;       /* variable thet holds the tape's number
                               in the inventory list */
        char artist_name[25];  /* array that holds the name of the artist */
        char album_name[25];   /* array that holds the name of the album */
        int year_realese;      /* variable that holds the year when a
                                specific album was realeased */
        char record_label[25]; /* array that holds the name of the record
                                company which realeased a specific album */
        int num_tracks;        /* variable that holds the number of tracks that
                                are found on a specific album */
        char tape_brand[15];   /* array that holds the brand of the tape */
        char tape_type[11];    /* array that holds the type of tape that
                                a specific album was recorded on */
        char tape_status[10];  /* array thet holds what status is the
                                current tape enjoying */
        } tape[10]; /* declares 10 occurences of this structure */

And here's my miserable excuse for a sorting function:

/******************************* Sort ***********************************/
void sort(void)
{ /* Start sort */
        tapelist ara[MAX];

        fillArray(ara);

        printf("Here are the unsorted numbers:\n");
        printArray(ara); /* prints the unsorted array */

        sortArray(ara); /* Sorts the array */

        printf("Here are the sorted numbers:\n");
        printArray(ara); /* prints the newly sorted array */
        return 0;

Quote:
} /* End sort */

/****************************** fillarray ******************************/

void fillArray(int ara[MAX])
{
        /* puts random numbers in the array */
        int ctr;
        int temp;

        temp = 0; /* initialize variable */
        for (ctr=0; ctr<MAX; ctr++)
        { temp++; ara[ctr] =  tape[temp].artist_name); } /* forces
               numbers to range between 0 and 99 */

        return;

Quote:
}

/***************************** printArray ******************************/
void printArray(int ara[MAX])
{
        /* prints the array */
        int ctr;
        for (ctr=0; ctr<MAX; ctr++)
                { printf("%d\n", ara[ctr]); }
        return;

Quote:
}

/******************************* Sortarray *****************************/

void sortArray(int ara[MAX])
{
        /* sorts the array */
        int temp; /* temporary variable to swap with */
        int ctr1, ctr2; /* need two loop counters to swap pairs of
numbers */

        for (ctr1=0; ctr1<(MAX-1); ctr1++)
                { for (ctr2=(ctr1+1); ctr2<MAX; ctr2++) /* test pairs */
                        { if (strcmp ara[ctr1], ara[ctr2]) >=0 ) {                          
{ temp = ara[ctr1]; /* pair is not in order */
                                  ara[ctr1] = ara[ctr2];
                                  ara[ctr2] = temp; /* "float" the lowest
to the highest */

Quote:
}

                        }
                }
                return;

Quote:
}

Thanks in advance for any advice.  You can reach me or my friend through
E-Mail.





Mon, 26 May 1997 09:06:15 GMT  
 sorting structs

Quote:
>Does anybody know how to sort an array within a struct?

The same way you sort another array.  The fact that it's in a struct makes
no difference.

Quote:
>struct tapelist /* Global structure definition */
>{
>    int tape_number;       /* variable thet holds the tape's number
>                               in the inventory list */
>    char artist_name[25];  /* array that holds the name of the artist */
>    char album_name[25];   /* array that holds the name of the album */
>    int year_realese;      /* variable that holds the year when a
>                                specific album was realeased */
>        char record_label[25]; /* array that holds the name of the record
>                                company which realeased a specific album */
>    int num_tracks;        /* variable that holds the number of tracks that
>                                are found on a specific album */
>    char tape_brand[15];   /* array that holds the brand of the tape */
>    char tape_type[11];    /* array that holds the type of tape that
>                                a specific album was recorded on */
>    char tape_status[10];  /* array thet holds what status is the
>                                current tape enjoying */
>    } tape[10]; /* declares 10 occurences of this structure */

Hmmm.  You have an array of structs, each of which contains multiple arrays.
I wonder what you're *actually* trying to sort.  I'll just take the question
at face value.

Quote:
>                    { if (strcmp ara[ctr1], ara[ctr2]) >=0 ) {

Here's the strcmp() problem.  strcmp() is a function, not a keyword.  You need
to put its arguments in parentheses to call it.  (You probably knew this,
since you have a closing parenthesis, but just in case....)  Thus,

        if (strcmp (ara[ctr1], ara[ctr2]) >= 0) /* do something */

The fact that you're comparing strings at all tells me that you probably
want to do something other than what you asked about.  But as I said, I'm
answering what you asked, not what is implied.  Now, as far as sorting the
array in a struct goes, let me make up a new struct:

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

/* This could be local to main(), but for this example I'll leave it here. */
struct s_foo {
        int data [20];
        char bar [15];

Quote:
} foo;

void dump_array (int *array, int size);
void sort_array (int *array, int size);

int main (void) {
        int i;

        for (i = 0; i < 20; i++)             /* make up some values */
                foo.data[i] = 30 - i;
        strcpy (foo.bar, "string");

        /* We want to sort the data array in the foo struct.  We'll ignore
           bar. */
        dump_array (foo.data, 20);
        sort_array (foo.data, 20);
        dump_array (foo.data, 20);

Quote:
}

/* Notice that dump_array operates on the array, but doesn't care about the
   struct. */
void dump_array (int *array, int size) {
        int i;

        for (i = 0; i < size; i++)
                printf ("%d ", array[i]);
        putchar ('\n');

Quote:
}

void sort_array (int *array, int size) {
        /* Here you would implement your generalized sorting routine.
           As far as this function is concerned, array is just a bunch
           of int values.  This function doesn't care about the struct. */

Quote:
}

/* Beware: the above code has not been tested. */
--
# Greg Wooledge




Tue, 27 May 1997 03:22:29 GMT  
 sorting structs


:Does anybody know how to sort an array within a struct?

Sure.  

I'll put the general suggestions at the top, for those of you who
don't want to wade through the appended code.

In order to program well, you have to master a set of related skills
and habits.  Stuff like

        - Start out with a small piece of code.  Make sure it's
          correct.  Then make a small addition to it.  Make sure
          it's correct.  Make another small addition.  Etc.  You
          might recognize this as "stepwise refinement", or some
          other buzzword.  This sort of thing is absolutely crucial
          to making reasonable progress, especially with an unfamiliar
          language, and especially for new programmers.

        - At each point in the code, know what all variables are,
          and what valid operations can be performed on them.  You
          can't use a variable if you don't know what it holds, or
          if you don't know what operations are valid.

        - This is really a corollary to the above rule: minimize
          global variables and keep procedures small and simple.

        - Never worry about efficiency unless you have to.  Particularly
          at the beginning level, you should write code to be
          understandable.  If you can't understand your own code,
          how can you be sure it does what you want?

:struct tapelist /* Global structure definition */
/* contents deleted */
:       } tape[10]; /* declares 10 occurences of this structure */

:/******************************* Sort ***********************************/

You should always start out with a list of prototypes for all functions.

:void sort(void)
:{ /* Start sort */
:       tapelist ara[MAX];

What is the definition of "tapelist"?  You gave the definition of
"struct tapelist" above, but it's not the same thing (unless you
explicitly define it to be the same with "typedef").

[deletia]
:       return 0;
        ^^^^^^^^
void functions can't return values.

:} /* End sort */
:/****************************** fillarray ******************************/
:
:void fillArray(int ara[MAX])
:{
[more deletia]
:       for (ctr=0; ctr<MAX; ctr++)
:       { temp++; ara[ctr] =  tape[temp].artist_name); } /* forces
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This assignment makes no sense.  "ara[ctr]" is an integer variable.
"tape[temp].artist_name is an array of characters -- the assignment
is simply not compatable.

:               numbers to range between 0 and 99 */
:
:       return;
:}
:
[still more deletia]
:void sortArray(int ara[MAX])
:{
:                       { if (strcmp ara[ctr1], ara[ctr2]) >=0 ) {
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^
Of course, this is not syntactically legal.  Beyond that, you
seem to be trying to tread ara[ctr1] and ara[ctr2] as if they
were strings, when they are declared as integers.

-Dave
--
***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***
  *     *     *     *     *     *     *     *     *     *     *     *     *
 *     *     *     *     *     *     *     *     *     *     *     *     *



Tue, 27 May 1997 08:42:56 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. sort struct

2. Sorting structs

3. How to sort structs by particular member?

4. Q: Sorting structs

5. sorting structs with qsort()??

6. struct member sort

7. help me sort my array of structs

8. Sort array of struct

9. sorting an array of structs

10. struct member sort

11. struct sort

12. Question About Sorting an Array of Structs

 

 
Powered by phpBB® Forum Software