SHARE
TWEET

Untitled

a guest Jun 27th, 2019 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. typedef struct rec {
  2.     int recordnumber;
  3.     char firstname[20];
  4.     char lastname[20];
  5.     int age;
  6.     char gender;
  7. } record;
  8.  
  9. void SortArr(record* rec, int numrecsread)
  10. {
  11.     int sortchoice;
  12.     int i2, j2, temp, ret;
  13.     record *r1, *r2;
  14.     record trec;
  15.  
  16.     printf("Press 1 to sort by record number or press 2 to sort by last name.n");
  17.     scanf("%d", &sortchoice);
  18.  
  19.  
  20.     if (sortchoice == 1)
  21.     {
  22.         printf("You have chosen to sort record numbers in ascending order...n");
  23.         for (i2 = 1; i2 < numrecsread; i2++)
  24.         {
  25.             for (j2 = 0; j2 < numrecsread - 1; j2++)
  26.             {
  27.                 if (rec[j2].recordnumber > rec[i2].recordnumber)
  28.                 {
  29.                     Swap(&rec[i2], &rec[j2]);
  30.  
  31.                 }
  32.             }
  33.         }
  34.     }
  35.     else if (sortchoice == 2)
  36.     {
  37.         printf("You have chosen to sort last names alphabetically...n");
  38.  
  39.         for (i2 = 1; i2 < numrecsread; i2++)
  40.         {
  41.             for (j2 = 0; j2 < numrecsread - 1; j2++)
  42.             {
  43.                 ret = strcmp(rec[j2].lastname, rec[j2 + 1].lastname); //this has to be called multiple times to fully sort
  44.                 if (ret > 0)
  45.                 {
  46.  
  47.                     Swap(&rec[i2], &rec[j2]);
  48.  
  49.                 }
  50.             }
  51.         }
  52.  
  53.     }
  54.     else if (sortchoice < 1 || sortchoice > 2)
  55.     {
  56.         printf("Invalid input.n");
  57.     }
  58.  
  59. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top