/* File: struct3.c Sort an array of records according to GPA. */ #include #include "genlib.h" #include "simpio.h" #include "strlib.h" typedef struct dummy_tag { string name ; string major ; double gpa ; } student_record ; /* Function prototypes */ student_record read_record() ; void print_record(student_record) ; void SelectionSort(student_record A[], int size) ; int FindSmallest(student_record A[], int start, int stop) ; main() { student_record students[20] ; int i, size ; size = GetInteger() ; for (i = 0 ; i < size ; i++) { students[i] = read_record() ; } SelectionSort(students,size) ; for (i = 0 ; i < size ; i++) { print_record(students[i]) ; } } /* Read in one record */ student_record read_record() { student_record rec ; rec.name = GetLine() ; rec.major = GetLine() ; rec.gpa = GetReal() ; return(rec) ; } /* Print out one record neatly */ void print_record (student_record rec) { printf("%20s, %10s, GPA: %1.4f\n", rec.name, rec.major, rec.gpa) ; } /* Use Selection Sort to sort the array of records according to GPA. */ void SelectionSort(student_record A[], int size) { int unsorted, smallest_index; student_record temp ; for(unsorted=0; unsorted < size-1; unsorted++){ smallest_index = FindSmallest(A,unsorted,size-1); /* swap values */ temp = A[smallest_index] ; A[smallest_index] = A[unsorted] ; A[unsorted] = temp ; } } /* Find smallest value in array A */ int FindSmallest(student_record A[], int start, int stop) { int smallest_index, i ; double smallest_value ; smallest_index = start ; smallest_value = A[start].gpa ; for (i = start+1 ; i <= stop ; i++){ if (A[i].gpa < smallest_value) { smallest_index = i ; smallest_value = A[i].gpa ; } } return(smallest_index) ; }