/* Sample Solution to Assignment #2 */ #include #include #include #define MAX_STUDENTS 25 /* Maximum number of students enrolled */ #define MAX_COURSE_NUM 5 /* Maximum number of courses a student can take*/ #define TOTAL_COURSES 7 /* Number of courses offered */ struct course { int num; /* unique course number */ int credits; /* number of credits per course */ }; struct student{ int num; /* student number */ int courses[MAX_COURSE_NUM]; /* course numbers of courses taken */ char grades[MAX_COURSE_NUM]; /* student grade in each course above */ int courses_taken; /* actual number of courses being taken */ float GPA; /* grade point average */ }; /* courses offered at Llenroc */ struct course courses_offered[TOTAL_COURSES] = {{100, 4}, {113, 1}, {212, 4}, {213, 2}, {611, 6}, {709, 1}, {990, 8}}; /* Error function. Print out the error message argument and terminate program execution. The exit function terminates the program with the argument (in this case, 1 - I'm using a non-zero value because calling fail indicates non-standard termination. exit is found in */ void fail(char *error_message) { printf("\n"); printf("%s",error_message); printf("\n"); exit(1); } /* Fail if the argument is not a valid grade */ void check_grade(char g) { switch(g){ case 'A': case 'B': case 'C': case 'D': case 'F': break; default: printf("%c\n", g); fail("Illegal Grade"); } return; } /* Fail if the course_num argument is not on the list of courses offered */ void check_course(int course_num) { int i; for (i=0; i< TOTAL_COURSES; i++){ if (courses_offered[i].num == course_num) return; } fail("Illegal Course Number"); return; } /* Scan in student information other than the GPA */ void scan_student(struct student *s) { int i; printf("Student number:"); scanf("%d",&(s->num)); /* Get number of courses taken, checking against maximum of MAX_COURSE_NUM */ printf("Number of Courses taken(Maximum %d): ", MAX_COURSE_NUM); scanf("%d",&s->courses_taken); if (s->courses_taken > MAX_COURSE_NUM) fail("Max number of courses exceeded"); /* Scan in courses and grades */ for (i =0; i < s->courses_taken; i++){ printf("Course: "); scanf("%d", &(s->courses[i])); check_course(s->courses[i]); printf("Grade: "); scanf("%1s", &(s->grades[i])); check_grade(s->grades[i]); } return; } /* Print out student information */ void print_student(struct student s) { int i; printf("Student number: %d\n", s.num); for(i=0; inum = s2.num; for (i=0;icourses[i] = s2.courses[i]; s1->grades[i] = s2.grades[i]; } s1->courses_taken = s2.courses_taken; s1->GPA = s2.GPA; return; } /* swap the values in structure pointed to by s1 in structure pointed to by s2 */ void swap(struct student *s1, struct student *s2) { struct student temp; copy_student(&temp, *s1); copy_student(s1, *s2); copy_student(s2, temp); return; } /* insert the array element at index into sorted section of array ranging from 0 to index-1. Array sorted in non-increasing order by GPA */ void insert(struct student students[], int index) { int i; for (i=index; i> 0; i--){ if (students[i-1].GPA < students[i].GPA) swap(&students[i-1], &students[i]); else break; } return; } /* Sort array students of given length in non-increasing order by GPA */ void isort(struct student students[], int length) { int i; for (i=1; i< length; i++) insert(students, i); return; } /* Read in students. Calculate GPA. Sort students by GPA. Print out students. */ int main(void) { int i; struct student students[MAX_STUDENTS]; int number_of_students; /* Scan in number of students */ printf("How many students are there?\n"); scanf("%d", &number_of_students); /* Error checking: */ if (number_of_students <= 0) fail("No students?"); if (number_of_students > MAX_STUDENTS) fail("Too many students"); for (i=0; i