Thread: Sort by 97, 98, 99, 00, 01, 02 etc.

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    9

    Sort by 97, 98, 99, 00, 01, 02 etc.

    I have a sort function and I want to sort my registration numbers by 97, 98, 99, 00, 01 etc. at the moment it works by 00, 01, 02, 97, 98, 99 etc. Any ideas?


    Code:
    void sortRec() // Sort records by Registration Number
    {
    	int i,j;
    	PERSON tp;
    	printf("\nRecords now sorted by Registration Number.\n\n");
        for(i=0;i<9;i++)
    	{
    		for(j=i+1;j<10;j++)
    		{
     			if(strcmp(student[i].regNo,student[j].regNo) > 0) 
    			{
    				tp=student[i];
    				student[i]=student[j];
    				student[j]=tp;
    			}
    		}
    	}
    }
    Cheers
    John

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >Any ideas?

    First convert the text of student[i].regNo and student[j].regNo each to an int. Then add the century, i.e. add 1900 or 2000 based on some identifying characteristic -- say values less than 50 are 2000's and greater than 50 are 1900's. Then do the comparison of the resulting ints to determine whether to swap.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    9
    Thats easier said than done, because the regNo field is made up of 8 characters. Only the first two characters denote the year of entry i.e. 97220132, 00343821 etc.

    Is there a way of adding two characters to the start of the string? Then I could just add 19 to the 90's and 20 to the 00's.

    Any ideas?

    cheers
    John

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >Thats easier said than done, because the regNo field is made up of 8 characters. Only the first two characters denote the year of entry i.e. 97220132, 00343821 etc.

    There are better ways, but...
    Code:
    #include <stdio.h>
    
    int main (void)
    {
       const char *text[] = {"97220132", "00343821"};
       int year;
       size_t i;
       for(i = 0; i < sizeof text / sizeof *text; ++i)
       {
          if(sscanf(text[i], "%02d", &year) == 1)
          {
             year += year > 50 ? 1900 : 2000;
             printf("year = %d\n", year);
          }
       }
       return 0;
    }
    
    /* my output
    year = 1997
    year = 2000
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Nov 2003
    Posts
    9
    Hi Dave,
    Heres my code so far, any idea how I could implement the code you suggested into my sort function.
    The file I use is just a txt file with information seperated by commas.

    cheers
    John


    Code:
    #include <string.h>
    #include <stdio.h>
    
    char seps[]   = " , ";
    char *token;
    
    typedef enum Menu { FIRST = 1, NEXT, PREVIOUS, LAST, SORT, EXIT };  // Define the menu
    
    typedef struct  // Define the student structure
    {
    	char fName[10];
    	char lName[10];
    	char regNo[10];
    	char doB[30];} PERSON;
    
    	PERSON student[11];
    
    EnterChoice();					// display main menu
    void firstRec();                // view First record
    void nextRec();                 // view Next record
    void previousRec();             // view Previous record
    void lastRec();                 // view Last record
    void sortRec();                 // Sort records by Registration Number
    
    char choice;
    int i;
    int counter;
    //-----------------------------------------------------------------------------------
    int main()
    {
    	char s[100];
    	FILE * f;
    	int len, i;
    	i=-1;
    	f = fopen("c:\\students.txt","r");
    
    		   for ( ; ; ) {
    			fgets(s, 100, f);
    			if feof(f) break;
    			len = strlen(s);
    			if (len) { s[len] = 0;} 
    			if (i>=9) break;
    	   i++;	
       {
    	    token = strtok( s, seps ); strcpy(student[i].fName, token);
    		token = strtok( NULL, seps ); strcpy(student[i].lName, token);
    		token = strtok( NULL, seps ); strcpy(student[i].regNo, token);
    		token = strtok( NULL, seps ); strcpy(student[i].doB, token);
    		
    		}
    		}
    
    			fclose(f);	
    
    while ( ( choice = EnterChoice() ) != EXIT )    // menu loop
       { 
          switch ( choice )
          { 
             case FIRST:		firstRec();		break;
             case NEXT:			nextRec();		break;
             case PREVIOUS:     previousRec();	break;
             case LAST:			lastRec();		break;
             case SORT:			sortRec();		break;
          }
    }
    }
    //------------------------------------------------------------------------------------
    		EnterChoice()	// Menu function
    { 
       printf( "\nEnter your choice\n"
          "1 - View first record\n"
          "2 - View next record\n"
          "3 - View previous record\n"
          "4 - View last record\n"
          "5 - Sort records by Registration Number\n"     
    	  "6 - End program\n? " );
       scanf( "%d", &choice );
       return choice;
       }
    
    void firstRec()  // View the first record
    {
    	counter=0;
    	printf("\nFirst Name: %s\n\n", student[counter].fName);
    	printf("Last Name: %s\n\n", student[counter].lName);
    	printf("Registration Number: %s\n\n", student[counter].regNo);
    	printf("Date of Birth: %s\n", student[counter].doB); 
    }
    void nextRec()  // View the next record
    {
    	++counter;
    	if (counter >=10)
    	{
    	printf("\nThere are no more records to view!\n\n");
    	}
      else
    	{
    	printf("\nFirst Name: %s\n\n", student[counter].fName);
    	printf("Last Name: %s\n\n", student[counter].lName);
    	printf("Registration Number: %s\n\n", student[counter].regNo);
    	printf("Date of Birth: %s\n", student[counter].doB); 
    	}
    	}
    void previousRec()  // View the previous record
    {
    	--counter;
    	if (counter <=-1)
    	{
    	printf("\nThere are no more records to view!\n\n");
    	}
      else
    	{
    	printf("\nFirst Name: %s\n\n", student[counter].fName);
    	printf("Last Name: %s\n\n", student[counter].lName);
    	printf("Registration Number: %s\n\n", student[counter].regNo);
    	printf("Date of Birth: %s\n", student[counter].doB); 
    	}
    	}
    void lastRec()  // View the last record
    {
    	counter=9;
    	printf("\nFirst Name: %s\n\n", student[counter].fName);
    	printf("Last Name: %s\n\n", student[counter].lName);
    	printf("Registration Number: %s\n\n", student[counter].regNo);
    	printf("Date of Birth: %s\n", student[counter].doB); 
    }
    
    void sortRec() // Sort records by Registration Number
    {
    	int i,j;
    	PERSON tp;
    	printf("\nRecords now sorted by Registration Number.\n\n");
        for(i=0;i<9;i++)
    	{
    		for(j=i+1;j<10;j++)
    		{
    
     			if(strcmp(student[i].regNo,student[j].regNo) > 0) 
    			{
    				tp=student[i];
    				student[i]=student[j];
    				student[j]=tp;
    			}
    
    		}
    	}
    	printf("\nFirst Name  Last Name  Registration Number  DOB\n"); 
    	for(i=0;i<10;i++)
    	{
    		printf("%s ", student[i].fName);
    		printf("%s ", student[i].lName);
    		printf("%s ", student[i].regNo);
    		printf("%s", student[i].doB);
    	}
    }

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    I didn't take a really close look, but from my initial understanding of the problem, I might suggest something like this.
    Code:
    void sortRec()
    {
       /* ... */
       for ( i=0;i<9;i++ )
       {
          for ( j=i+1;j<10;j++ )
          {
             int x,y;
             if ( sscanf(student[i].regNo, "%02d", &x) == 1 &&
                  sscanf(student[j].regNo, "%02d", &y) == 1 )
             {
                x += x > 50 ? 1900 : 2000;
                y += y > 50 ? 1900 : 2000;
                if ( x > y )
       /* ... */
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Nov 2003
    Posts
    9
    Dude,
    Thankyou so much!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    It works perfectly!

    god damn i love this forum

    cheers
    Thanks again

    John

  8. #8
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    Originally posted by JLan
    Dude,
    Thankyou so much!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    It works perfectly!

    god damn i love this forum

    cheers
    Thanks again

    John
    of course, it will only work until the year 2050 .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers + Malloc = Painloc
    By Chalks in forum C Programming
    Replies: 9
    Last Post: 10-19-2008, 01:10 PM
  2. trouble with text file reading/writing
    By sbeehre in forum C Programming
    Replies: 25
    Last Post: 08-02-2006, 09:48 PM
  3. binary data file
    By tucker81 in forum C Programming
    Replies: 8
    Last Post: 06-08-2006, 12:50 AM
  4. Resource ICONs
    By gbaker in forum Windows Programming
    Replies: 4
    Last Post: 12-15-2003, 07:18 AM
  5. resources with dev-c++
    By lambs4 in forum Windows Programming
    Replies: 0
    Last Post: 04-13-2003, 06:06 AM