/* An example program demonstrating how to use C structures to create records. It creates a studentType record to maintain data on students. It also creates a classType record to maintain data on a roster of students for a particular class. 1. Test this program by compiling it, and running it. Enter several names of students in our class. 2. Read through the code and then answer the following questions. a. Calculate how much memory a variable of type studentType requires. You can assume that integers require four bytes and that characters require one byte. b. Based on your previous answer, calculate how much memory a variable of type classType requires. c. Test if your answers are correct by adding the following print statement to the main program: printf("Size of student: %d, size of class: %d\n", sizeof(cs21.student[0]), sizeof(cs21)); d. Why does the PrintOneStudent function use the "." operator while the PrintClass function uses the "->" operator? e. Why does the ReadOneStudent function pass a student record in by reference? Is this necessary? f. Why does the PrintClass function pass a class in by reference? Is this necessary? If you are unsure of any of your answers, ask me about them. 3. Write a function called AddStudent which takes as input the class and the information for a student (name, major, and graduation year), and adds the student to the class. It should make sure that the class is not full before performing the addition. Be sure to increment the number of students in the class if the add is successful. 4. Test the AddStudent function and show me that it works before going on. 5. Write a function called DeleteStudent which takes as input the class and the name of the student to be deleted. If the student is not found in the class, then a message should be printed stating this fact and the function should do nothing. Remember to use strcmp to determine if two strings are equal. If the student is found, then shift all the students down that are after this student in the array. Be sure to decrement the number of students in the class if the delete is successful. 6. Test the DeleteStudent function and demonstrate it for me. I will show you my solutions for AddStudent and DeleteStudent next week. */ #include #include #include "simpio.h" #define NameLength 30 #define DegreeLength 6 #define CourseLength 52 #define ClassSize 30 typedef struct { char name[NameLength]; char major[DegreeLength]; int year; } studentType; typedef struct { int numStudents; char courseName[CourseLength]; studentType student[ClassSize]; } classType; void PrintOneStudent(studentType student); void PrintClass(classType *class); void ReadOneStudent(studentType *student); void ReadClass(classType *class); char *LimitedReadString(char *prompt, int limit); main() { classType cs21; ReadClass(&cs21); PrintClass(&cs21); } void PrintClass(classType *class) { int i; printf("Roster for %s\n", class->courseName); printf("There are %d students in the class.\n", class->numStudents); for (i=0; inumStudents; i++) { printf("%d: ", i+1); PrintOneStudent(class->student[i]); } } void PrintOneStudent(studentType student) { printf("%30s %6d %6s\n", student.name, student.year, student.major); } void ReadClass(classType *class) { int i=0; char answer, termch; strcpy(class->courseName, LimitedReadString("course name", CourseLength-1)); while (i < ClassSize) { ReadOneStudent(&(class->student[i])); i++; printf("Continue (y/n)? "); scanf("%c%c", &answer, &termch); if (tolower(answer) == 'n') break; } class->numStudents = i; printf("Successfully read %d students\n\n", i); } // Reads in a limited size string from the user. Continues to prompt // the user for the string until it receives something within the // limit, which it then returns. char *LimitedReadString(char *prompt, int limit) { char *answer; while (1) { printf("Enter %s> ", prompt); answer = GetLine(); if (strlen(answer) > limit) printf("%s must be no more than %d characters, try again.\n", prompt, limit); else return(answer); } } void ReadOneStudent(studentType *student) { strcpy(student->name, LimitedReadString("student name", NameLength-1)); strcpy(student->major, LimitedReadString("student major", DegreeLength-1)); printf("Enter graduation year> "); student->year = GetInteger(); }