homework 5 for cs240 PDF

Title homework 5 for cs240
Course Programming In C
Institution Purdue University
Pages 6
File Size 69 KB
File Type PDF
Total Downloads 89
Total Views 148

Summary

hw for cs240...


Description

====================================================================== CS 24000 PROGRAMMING IN C Fall 2018 Homework 4 ======================================================================

Goals The purpose of this homework is to give you experience in using typdef and structure declarations as well as defining variables of those types. We’ll also get a small peek at using an enumerated data type. You’ll use file manipulation operations to load values from files into structures and then store the structures back to files.

Getting Started 1. Log in to one of the Purdue CS Linux systems. These include data.cs.purdue.edu and borgNN.cs.purdue.edu where NN=01,02, etc 2. Inside the cs240 directory, setup a hw4 directory by running the following commands. $ cd cs240 $ git clone ~cs240/repos/$USER/hw4 $ cd hw4 3. This creates your hw4 directory, and also copies what is called a Makefile into it. This Makefile has a number of targets defined to make your life easier... When you are ready to try your work, you can run $ make hw4_main to build hw4_main, or run $ make hw4_test to build the test module. $ make to build both. $ make clean can be used to clear out all of the temporary files Note: any time you run make, your changes are automatically pushed to a git repository in the cs240 course account. As mentioned before, we strongly encourage you to always work on a Purdue CS Linux system. This mechanism cannot work otherwise.

The Big Idea At this point you understand how structures work and what they’re used for. This time, we’re going to do something useful by using a structure with many fields in it. For this homework, we’re going to create a structure that will be used to represent a student. There are lots of pieces of information for a student that should be kept together in one group. The information to be maintained will be as follows: - ID number: this is an unsigned integer - First name: this is an array of 20 characters - Last name: this is an array of 20 characters - u_or_g: this is an enum type corresponding to Undergraduate or Graduate - Money: this is a float - Dorm room: this is an unsigned integer - Schedule: this is a two dimensional array of characters. The first (left) dimension represents the day of the week (M-F). The second dimension represents the hour of the day (7AM-4PM). If the student is busy during the hour, the array entry contains a non-zero value. The structure should look like this: (we combine it with a typedef so that we can refer to it with one name) typedef struct { unsigned int char char enum u_or_g float unsigned short char } student_t;

id_number; first_name[20]; last_name[20]; classification; money; dorm_room; schedule[5][10];

The Assignment You are to write a series of functions that deal with arguments of type student_t. You’ll modify occurrences of students that exist in a global array of MAX_STUDENT student_t structures called g_student_array. You’ll also manipulate another global variable called g_student_count that keeps track of how many student_t records you’ve loaded into the array.

Functions You Will Write You will write the following functions:

int read_students(char *); This function will read a list of students from the file specified by the first argument. The students should be stored into the g_student_array; the first one stored at offset zero and so on. This function should assert that the argument is not NULL. If any other errors occur, it should return an appropriate error code from the list of error codes below. Otherwise, the function should return the number of student records read from the input file. If the function exits with successful status, it should set the value of g_student_count to be the number of student records read.

int write_students(char *, int); This function will write a list of students to the file specified by the first argument. The second argument specifies the number of entries from g_student_array to write. The function should write exactly that many. This function should assert that the first argument is not NULL. This function should assert that the second argument is non-negative. If any other errors occur, it should return an appropriate error code from the list of error codes below. Otherwise, the function should return the number of student_t records written the the output file.

int find_student_by_id(unsigned int); This function will search the array for a student_t in g_student_array whose ID number matches the value of the argument. The function should return the array index of that student_t in the array. If the student cannot be found, the function should return a NOT_FOUND error.

int find_student_by_name(char *, char *); This function will search for a student_t entry in g_student_array whose first name matches the first argument and whose last name matches the second argument. When the student is found, the function should return its index in the array. This function should assert that neither argument is NULL. If no student can be found, the function should return NOT_FOUND. If multiple student entries are found with the same name, the function should return the lowest numbered index.

float compute_undergraduate_percent(); This function should return the percentage of the student body (represented by the g_student_array) that is classified as undergraduate.

int grant_scholarship(); This function should find the member of the student body with the least amount of money, subtract one dollar from everyone’s account, and give it to the poorest student. (Ok, so this isn’t realistic at all, but it’s a nice idea, isn’t it?) Note that if two or more students are tied for being the poorest, the function should return NOT_FOUND. Otherwise, the function should return the index of the student in g_student_array.

int find_full_dorm_room(); This function should scan through the student body database and find a dorm room that contains three or more people. If no such room is found, it should return the NOT_FOUND error. If such a room is found, it should return the room number. If multiple full dorm rooms are found, the function should return the lowest numbered dorm room of this kind. Hint: Dorm room numbers will always be less than DORMS. See the hw4.h file.

unsigned int find_earliest_meeting(unsigned int, unsigned int); This function will accept the ID numbers of two students in the student body database and find the first time during the week that neither of them have a schedule conflict. If either student is not found, the function should return NOT_FOUND. If the two students have no free time in common, the function should return NOT_SCHEDULABLE. Otherwise, the function should return an integer that represents the day of the week (M-F) times 100 plus the hour of the day (7-16). For instance, Monday at 2pm would be represented by 0*100 + 14 = 14. Tuesday at 4pm would be represented by 1*100 + 16 = 116.

Input Files We’ll provide a file called “input” that contains an example of the data to be loaded into the g_student_array array. The format of each line looks like this: 1682620999,Kennedy,Frank,U,$348.82,512,10101101000001000110100101011000001001000111000101

Where the fields are: id_number,last_name,first_name,classification,$money,dorm_room,schedule

The schedule field is organized as occurrences of meetings from Monday 7am to Friday 4pm. It will be somewhat of a challenge to parse this into a record. Keep the following things in mind: - You can put characters like ‘$’ and ‘,’ into your fscanf() format string to explicitly read past those characters where they occur - You should read names with format specifiers like %[^,] to get everything but commas in the names - Scan the schedule field into a string. Make sure it’s 50 characters long. Then check individual characters of the string to set the schedule field of a student_t structure

Header Files We provide a header file, “hw4.h”, for you. It contains prototypes for each of the functions that you will write as well as #definitions for the constants. You should not alter this file. We will replace it with the original when grading.

Error Codes NON_READABLE_FILE: the file cannot be opened for read access NON_WRITABLE_FILE: the file cannot be opened for write access READ_BAD_RECORD: a record was read from a file that didn’t make sense or was incomplete NOT_FOUND: a database search failed NOT_SCHEDULABLE: a scheduling request could not be satisfied

Hints - If you’re trying to use fscanf() to read into an unsigned short variable, the format specifier to use is %hu. - Take a good look at the hw4_main.c file. It will give you good ideas for writing your hw4.c file

These Standard Rules Apply: - You may add any #includes you need to the top of your hw4.c file. - You may not create any global variables other than those that are specified in this handout. Creation of additional global variables will impact your style grade. - You should check for any failures and return an appropriate value. - You should not assume any maximum size for the input files. The test program will generate files of arbitrary size. - Do not look at anyone else’s source code. Do not work with any other students.

Submission To submit your program for grading, type: $ make submit In your hw4 directory. You can do this as often as you wish. We encourage you to submit your code as often as possible. Only your final submission will be graded.

Grading The operation of your functions will be graded out of 100 points. The point breakdown will be determined by the test program. The test program will be run many times when grading. It is your job to do the same. The lowest score will be your final grade. This homework will also have a style grade based on 20 points, with 2 points deducted for each code standard violation found. Your code must compile successfully using -Wall -Werror any credit. Code that does not compile will be assigned score of 0. If your program crashes (e.g., segmentation point during the testing process, you will also receive

to receive an automatic fault) at any a score of 0....


Similar Free PDFs