ulvis.paste.net

Paste Search Dynamic
Recent pastes
student list
  1.  
  2. #include <stdio.h>
  3.  
  4. #include <string.h>
  5.  
  6. struct _Student {
  7.  
  8.    char name[32] ;
  9.  
  10.    unsigned char age ;
  11.  
  12.    char yearInCollege ;
  13.  
  14. } ;
  15.  
  16.  
  17.  
  18. struct _Student StudentList [ 8 ] =
  19. {
  20.  
  21.  
  22.  // TASK 1 FILE THE CODE TO INITIALIZE THIS STRUCURE WITH DATA I GAVE IN CANVAS
  23.  
  24.  
  25.  
  26.    {"Erik", 21, 'A'},
  27.    {"Jeff", 22, 'A'},
  28.    {"Beth", 23, 'D'},
  29.    {"Brad", 24, 'D'},
  30.    {"Cindy", 28, 'C'},
  31.    {"Bill", 25, 'B'},
  32.    {"Jack", 26, 'C'},
  33.    {"Jill", 29, 'D'},
  34. };
  35.  
  36. void printStudentList(struct _Student *ptr, int num )
  37. {
  38.  
  39.   int i=0 ;
  40.   for (i=0;i<num;i++) {
  41.  
  42.         printf ("%s %d %c\n", ptr->name, ptr->age, ptr->yearInCollege);
  43.         ptr++;
  44.   }
  45.  
  46.   printf("\n");
  47.  
  48.  
  49.    // FILL IN THE CODE TO PRINT ALL FIELDS IN EACH RECORD
  50.  
  51.  
  52. }
  53.  
  54.  
  55. void sortStudentListUsingName(struct _Student ptr[ ], int num )
  56. {
  57.         struct _Student temp;
  58.         int i,j;
  59.         for (i=0;i<num-1;i++){
  60.                 for (j=0;j<(num-1-i);j++){
  61.                         if (strcmp(ptr[j].name, ptr[j+1].name) >0) {
  62.                                 temp = ptr[j];
  63.                                 ptr[j] = ptr[j+1];
  64.                                 ptr[j+1] = temp;
  65.                         }
  66.                 }
  67.         }
  68. }
  69.  
  70.  
  71.  
  72. int main ( )
  73.  
  74. {
  75.  
  76.  
  77.      int numCells = sizeof ( StudentList)/ sizeof ( StudentList[0]);
  78.  
  79.  
  80.      printStudentList(StudentList , numCells) ;
  81.  
  82. sortStudentListUsingName(StudentList, numCells) ;
  83.  
  84.  
  85.      printStudentList (StudentList , numCells) ;
  86. }
  87.  
  88.  
  89.  
Parsed in 0.008 seconds