CS 241 Data Organization using C
Homework 7: Structs


October 24, 2018
This homework will involve writing a short program using structs. By the end, you should be able to create a struct, update it, and allocate memory for the struct on the heap.

Course Scheduling

It’s about time for registration at UNM. In that spirit, we will be writing some code that assigns students to classes and includes all the relevant information about the student and instructors involved.

The program needs to be named scheduler.c.

People at the University

Create a struct to represent a person at the university and call it Person. People all have the following attributes (you’re free to name these in your underlying datastructure whatever you want as long as your names are appropriately descriptive:

In tandem with this, you should have the following methods:

void printperson(struct Person* p);

This method will print out the person’s first and last name, separated by a space. If the person is not a student, then the person is a professor and the method will print out “Prof. ” followed by their names.

struct Person* createPerson(char* firstname, char* lastname, int  
    idnum, int student);

This method will create a Person struct, allocate space for them on the heap, instantiate all the fields in the struct, and then return a pointer on the heap.

Classes at the University

Create a struct to represent a class taught at the university and call it UNMclass. Classes all have the following attributes (again, free naming)

Note: Your struct can contain other variables inside it to help you with the problem. This is the minimum that your UNMclass needs to contain.

In tandem with this, you should have the following methods:

void printunmclass(struct UNMclass* c);

This method will print out the UNM department followed by the course number, separated by a space.

struct UNMclass* createunmclass(char* dept, int coursenum, struct Person* instructor, int cap);

This method will create a UNM class struct, allocate space for them on the heap, instantiate all the fields in the struct (except for the array of student pointers), and then return a pointer on the heap.

Testing

Create a few students and allocate memory for them on the heap. Write a few print statements to make sure that you’re properly writing to these structures. In particular, char *s can be tricky to write to.

Create a few UNMclasses and appropriate instructors for the courses, allocating memory for them on the heap.

Create a data structure on the heap to hold all of the UNM classes in for a particular semester and call it spring19. This should be a variable held in main. (If you try to make this a global, your program will give you an error. Why?) You can safely assume there are going to be fewer than one thousand classes in a semester. Your data structure should not duplicate UNMclasses. What type should this be?

Be sure to update your datastructure after calling the createunmclass method each time.

Enroll students

Fill in the following method:

int enroll(struct Person* student, struct UNMclass* course);

enroll should return a zero if enrollment was successful and a different number otherwise. Enrollment is unsuccessful if a UNMclass is full or a person is not a student.

Print out a students’ schedule

Fill in the following method:

void printschedule(struct Person* student, struct UNMclass* schedule[]);

printschedule should look through the spring19 schedule of classes to find each one that the student is enrolled in. This method should print the department name followed by a space followed by the class number. Each course a student is enrolled in should be on a new line.

For instance, if a student “Julia Campbell” was enrolled in CS241, CS251, Math162, and English107, this method should print out:

CS 241  
CS 251  
Math 162  
English 107

Using testing code

We provide some testing code in scheduler.c which the instructors will use to grade your assignment along with the expected output in testscheduler.out. You will need to provide additional testing code for your testing grade. Think of additional edge cases to cover.

Files to Produce

scheduler.c
- a C program that follows the spec and includes all of the students’ testing code in the main method.

Turning in your assignment

Zip your file and turn it in via the hw-07 folder in the course dropbox.

Grading Rubric (total of 20 points)

12 points:Program compiles without errors or warnings on moons.cs.unm.edu using /usr/bin/gcc with the -Wall -ansi -pedantic options. Correct output for the provided main method. The expected output is found in testscheduler.out. Note that we will use a diff test, so whitespace matters. Everything allocated on the heap is later freed.
4 points:The program starts with a comment stating the students first and last name. The program is correctly named. The code follows the CS 241 coding standard.
4 points:The code the student wrote in the main method tests the functionality of the program in normal and error cases. The testing code is nontrivially different than the provided code (eg more than just names are changed).