Answer all numbered and bulleted questions. Upload your answers and program files to the VikingWeb coursework section. Besides uploading your written answers to the VikingWeb, please turn in a printed copy either in class on the due date or leave a copy in the box outside my door later on during the day. (You may turn in the printed copies later than 5PM on the due date, but the files must be uploaded to VikingWeb at or before 5PM.)
You may work in pairs for all parts of this assignment, including the homework exercises.
Computing days of the week: Write a program that will input a date in the format mm/dd/yyyy and then print out which day of the week that date falls (or fell) on. Your program should run as follows (the bold text is what the user types):
$ ./datecompute Please enter date in the format mm/dd/yyyy: 2/30/1892 2/30/1892 is a Tuesday. |
Name your source code file: datecompute.c
Your program should contain a reasonable amount of error-checking. If the user enters invalid input such as characters instead of numbers, then the program should print an error message and exit neatly (by returning -1 indicating an error), instead of crashing.
Use proper programming style, including indentation, appropriate variable names, etc.
In order to compute the day of the week from a given date, you can use Zeller's algorithm, described below.
Let A, B, C and D denote integer variables that contain the following values:
A = the month of the year, with March having the value 1, April the value 2, . . . , December the value 10, and with January and February being counted as months 11 and 12 of the preceding year (in which case, subtract 1 from C).
B = the day of the month (1, 2, 3, . . . , 28, 29, 30, 31).
C = the year of the century (this would be 95 for the year 1895).
D = the century (this would be 18 for the year 1895).
Next let W, X, Y, Z and R denote integer variables, and compute the following values in this order, using integer arithmetic.
W = (13A - 1) / 5 X = C / 4 Y = D / 4 Z = W + X + Y + B + C - 2D R = the remainder obtained when Z is divided by 7.
The value of R represents the day of the week, where r = 0 represents Sunday, r = 1 represents Monday,. . . , and r = 6 represents Saturday. If R turns out to have a negative value such as -4, add 7 to it to get a nonnegative value between 0 and 6.
Note #1: If the month values are January or February, then the previous year is used for the computation, not the current year. This has to do with the fact that at one point in history, the beginning of the year was considered to be March 1, not January 1.
Note #2: Zeller's Algorithm is very specifically tied to the Gregorian calendar. For this reason, it will probably not give a correct result for older dates.
These will allow you to add points to your exam grades. They should actually also help you do better on the exams by giving you more practice with different aspects of C programming, so you should try to complete them.
This Extra-Credit portion is due: Friday, March 4
GPA computer: Write a program that will read a list of course grades for a student and calculate the student's grade point average (GPA).
The input for this program will come from a data file (sample files are given below). You may use input redirection to get your program to input the data from the file. The input file will consist of a first line which lists the student's ID and an integer N, the number of courses. The first line will be followed by N more lines, where each line contains a course ID number, followed by the credit hours of the course, and the letter grade ('A', 'B', 'C', 'D', 'F') that the student earned. If the student took 0 courses (N = 0) there are no lines in the file after the first one.
To calculate the GPA:
The output of your program should match the sample outputs given below. Your program should list a table of the ID, credit hours, grade, and points earned for each course, followed by a summary of the total hours enrolled, the total hours successfully completed (i.e. non-F grade), the total grade points, and the GPA. Notice, the GPA will be a floating-point number and should be in the range 0.0000 to 4.0000.
Your program should be well-written, use appropriate variables and control statements, and well-documented. The output format must match the samples below (including spacing, formatting, etc).
Name your source code file: gpacompute.c
Sample input/output files (if you think there is something wrong with these output results, let me know):