idastan97

CSCI151 L28 P1

Nov 11th, 2016
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.29 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct {
  5.     int id;
  6.     char name[12];
  7. } student;
  8.  
  9. void selectionSortById(student *x[], int size) {
  10.  
  11.     // Put your code here for sorting based on id
  12.     //
  13.     // Hint: Copy the code from the int array version of
  14.     //  selection sort and paste here, and make the
  15.     //  needed changes from that
  16.     int i;
  17.     for (i=0; i<size-1; i++){
  18.         int j;
  19.         int minidi=i;
  20.         for (j=i+1; j<size; j++){
  21.             if (x[j]->id<x[minidi]->id){
  22.                 minidi=j;
  23.             }
  24.         }
  25.         student *temp=x[minidi];
  26.         x[minidi]=x[i];
  27.         x[i]=temp;
  28.     }
  29. }
  30.  
  31. _Bool nameCompare(char name1[], char name2[]) {
  32.  
  33.     int i = 0;
  34.     do {
  35.         if (name1[i] < name2[i]) {
  36.             return 1;
  37.         } else if (name1[i] > name2[i]) {
  38.             return 0;
  39.         }
  40.         i++;
  41.     } while (name1[i] != '\0' || name2[i] != '\0');
  42.  
  43.     return 0;
  44. }
  45.  
  46.  
  47. void insertionSortByName(student *x[], int size) {
  48.  
  49.     // Put your code here for sorting based on name; use
  50.     // the nameCompare function below
  51.     //
  52.     // Hint: Copy the code from the int array version of
  53.     //  insertion sort and paste here, and make the
  54.     //  needed changes from that
  55.     int i;
  56.     for (i=0; i<size; i++){
  57.         int j;
  58.         student *toinsert=x[i];
  59.         for (j=i; j>=0; j--){
  60.             if (j==0 || !nameCompare(toinsert->name,x[j-1]->name)){
  61.                 x[j]=toinsert;
  62.                 break;
  63.             } else {
  64.                 x[j]=x[j-1];
  65.             }
  66.         }
  67.     }
  68. }
  69.  
  70.  
  71. void printArray(student *x[], int size) {
  72.  
  73.     int i;
  74.     for (i = 0; i < size; i++) {
  75.         printf("%8i %12s \n", x[i]->id, x[i]->name);
  76.     }
  77.     printf("\n");
  78. }
  79.  
  80. int main() {
  81.  
  82.     student *roster[8];
  83.  
  84.     // Opening the input file
  85.     FILE *file = fopen("records.txt", "r");
  86.     if (file == NULL) {
  87.         printf("Cannot find file.");
  88.         exit(1);
  89.     }
  90.     printf("File opened successfully.\n");
  91.  
  92.     // Reading the records into the roster from the file
  93.     int i;
  94.     for (i = 0; i < 8; i++) {
  95.         roster[i] = malloc(sizeof(student));
  96.         fscanf(file, "%i %s \n", &roster[i]->id, roster[i]->name);
  97.     };
  98.     fclose(file);
  99.  
  100.     // Printing out the roster, in the initial order
  101.     printArray(roster, 8);
  102.  
  103.     // Sorting the array by id (hopefully)
  104.     selectionSortById(roster, 8);
  105.  
  106.     // Printing out the roster, after sorting by id
  107.     printArray(roster, 8);
  108.  
  109.     // Sorting the array by name (hopefully)
  110.     insertionSortByName(roster, 8);
  111.  
  112.     // Printing out the roster, after sorting by name
  113.     printArray(roster, 8);
  114.  
  115.     return 0;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment