Sorting array structures

This is a discussion on Sorting array structures within the C Programming forums, part of the General Programming Boards category; How do I sort arrays structures? I just need a quick explanation or small ex. to lead me in the ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    16

    Sorting array structures

    How do I sort arrays structures? I just need a quick explanation or small ex. to lead me in the direction.
    Code:
       */
       This program requires three command arguments: The text file name, 
       the number of entries to be scanned from the input file, and the input 
       file name.  The program will scan the input file's entries into a 
       structured array and sort the arrays.  It will also print the unsorted and
       sorted arrays.  Lasltly, it will allow the user to run queries for certain
       entries using the SSN.  
    *****************************************************************************/
    
    /* Preprocessing Directives */
    #include <stdio.h>
    #include <stdbool.h>
    #include <stdlib.h>
    #define LEN 30
    #define NUM 10
    
       typedef struct
          {
          int month;
          int day;
          int year;
          } BDATE;
    
       typedef struct
          {
          char firstName[LEN];
          char lastName[LEN] ;
          int grade;
          char SSN [10];
          BDATE bDate;
          } STUDENT;
    
    void displayName ();
    bool openFile (char *argv[], FILE* *pfpIn);
    bool readFile (char *argv[], FILE* afpIn, STUDENT stud[]);
    void displayFile (char *argv[], STUDENT stud[]);
    void sortName (char *argv[], STUDENT stud[]);
    void displaySortName (char *argv[], STUDENT stud[]);
    void sortSSN ();
    void displaySortSSN ();
    void binSearch ();
    
    // Function main calls other functions and returns control back to OS.
    int main (int argc, char *argv[])
    {
       int quant = atoi (argv[1]);
       STUDENT stud[NUM];
       FILE* afpIn;
       
       displayName ();
      
       if (argc == 3 && quant > 1 && quant < 10)
          {
     printf("\nNumber of command arguments: %d", argc);
     printf("\nNumber of strings to scan from file: %s", argv[1]);
     printf("\nName of input file: %s", argv[2]);
          if ( openFile (argv, &afpIn) && readFile(argv, afpIn, stud) )
             {
             displayFile(argv, stud);
             sortName(argv, stud);
             displaySortName(argv, stud);
             }
          }
       else
          ("\nPlease try again later\n");
    
       return 0;
    }
    
    // Function displayName displays the Programmer's name.
    void displayName()
    {
       printf("\nThomas Ciocco\n");
    
    }
    
    // Function openFile opens the input file
    bool openFile (char *argv[], FILE* *pfpIn)
    {
       bool openFileSuccess = false;
    
       if ((*pfpIn = fopen (argv[2], "r")) != NULL)
          {
          openFileSuccess = true;
          printf ("\nFile open success\n\n");
          }
       else 
          printf("\nFile did not open successfully\n");
    
       return openFileSuccess;
    }
    
    // Function readFile scans the input file data into a structured array
    bool readFile (char *argv[], FILE* afpIn, STUDENT stud[])
    {
       bool readFileSuccess = true;
       int counter;
       int quant = atoi (argv[1]);
    
       char* tempMonth[quant][LEN];
       char* tempDay[quant][LEN];
       char* tempYear[quant][LEN];
      
       for (counter = 0; counter < quant ; counter++)     
          {
          fscanf(afpIn, "%s %s %d %s %s %s %s", 
                 stud[counter].firstName, stud[counter].lastName, 
                 &stud[counter].grade, stud[counter].SSN, 
                 tempMonth[counter], tempDay[counter], tempYear[counter]);
          }   
    
        // Converts file month strings to ints and fills bDate.month
       for (counter = 0; counter < quant; counter++)
          {
          if(!strcmp(tempMonth[counter],"January"))
             {
             stud[counter].bDate.month = 1;
             }
          if(!strcmp(tempMonth[counter],"February"))
             {
             stud[counter].bDate.month = 2;
             }
          if(!strcmp(tempMonth[counter],"March"))
             {
             stud[counter].bDate.month = 3;
             }
          if(!strcmp(tempMonth[counter],"April"))
             {
             stud[counter].bDate.month = 4;
             }
          if(!strcmp(tempMonth[counter],"May"))
             {
             stud[counter].bDate.month = 5;
             }
          if(!strcmp(tempMonth[counter],"June"))
             {
             stud[counter].bDate.month = 6;
             }  
          if(!strcmp(tempMonth[counter],"July"))
             {
             stud[counter].bDate.month = 7;
             } 
          if(!strcmp(tempMonth[counter],"August"))
             {
             stud[counter].bDate.month = 8;
             }
          if(!strcmp(tempMonth[counter],"September"))
             {
             stud[counter].bDate.month = 9;
             }
          if(!strcmp(tempMonth[counter],"October"))
             {
             stud[counter].bDate.month = 10;
             }
          if(!strcmp(tempMonth[counter],"November"))
             {
             stud[counter].bDate.month = 11;      
             }
          if(!strcmp(tempMonth[counter],"December"))
             {
             stud[counter].bDate.month = 12;
             }          
    
          } 
    
       // Converts string days and months to ints and fills bDate.day and year
       for (counter = 0; counter < quant; counter++)
          {
          stud[counter].bDate.day = atoi(tempDay[counter]);
          stud[counter].bDate.year = atoi(tempYear[counter]);
          }
    
    
       return readFileSuccess;
    }
    
    // Function displayFile prints the file data
    void displayFile (char *argv[], STUDENT stud[])
    {
       int counter;
       int quant;
    
       quant = atoi (argv[1]);
    
       printf("                 Name");
       printf("    Level");
       printf("            SSN");
       printf("    BirthDate\n");  
       printf("                 ----");
       printf("    -----");
       printf("            ---");
       printf("    ---------\n");  
       for (counter = 0; counter < quant; counter++)
          {
          printf("%10s %10s %7d %15s %5d/%d/%d \n", 
                 stud[counter].firstName, stud[counter].lastName,
                 stud[counter].grade, stud[counter].SSN, 
                 stud[counter].bDate.month, stud[counter].bDate.day,
                 stud[counter].bDate.year);
          }
    }
    // Function sortName sorts the array by Name data
    void sortName (char *argv[], STUDENT stud[])
    {
       int quant = atoi (argv[1]);
       int last;
       int current;
       int walk;
       int smallest;
       int tempData;
     
       STUDENT temp[quant];
    }
    
    // Function displaySortName prints the array sorted by name 
    void displaySortName(char *argv[], STUDENT stud[])
    {
       int counter;
       int quant;
       int last;
    
       STUDENT temp;
       STUDENT* pCurrent;
       STUDENT* pWalker;
       STUDENT* pSmallest;
     
       quant = atoi (argv[1]);
       last = quant + 1; 
    
       for (counter= 0; counter < last; counter++)
          {
          ;
          }
    
       
    }
    
    
    // Function sortSSN sorts the array by the SSN
    void sortSSN ( )
    {
    
    }
    
    // Function displaySortSSN prints the array sorted by SSN
    void displaySortSSN ( )
    {
    
    }
    
    // Function binSearch allows user to search database for entry via SSN
    void binSearch ( )
    {
    
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The same way you sort an array of anything else.
    Code:
    if( array[x] < array[y] )
        ...do something...
    Becomes:
    Code:
    if( array[x].value < array[y].value )
        ...do something...
    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting an array of structures
    By kisiellll in forum C Programming
    Replies: 15
    Last Post: 04-06-2009, 05:25 AM
  2. array of structures
    By tish in forum C Programming
    Replies: 9
    Last Post: 04-05-2009, 03:17 AM
  3. trying to initialize an array of structures
    By dreamgoat in forum C Programming
    Replies: 4
    Last Post: 09-26-2004, 05:33 PM
  4. passing array of structures to function
    By bvnorth in forum C Programming
    Replies: 3
    Last Post: 08-22-2003, 07:15 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

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