CS1003 Homework #5
Due by Tuesday, April 20, at
11:00am
There are two parts to this homework assignment: a theory
portion, worth 10 points, and a programming portion,
worth 15 points. Please be sure to review the submission instructions in
advance, and make sure to include a README.
This version is revised as of 4/12. Minor clarification
on getHour and getMinute: they take integer representations of
times, not dates; and to listAllEvents: it needs one
parameter. Also added more tips on how to use sscanf and
printf.
Written questions
- (3 points) Insert the following elements into a binary
search tree, and draw the result: 72, 35, 61, 10, 4, 98, 32,
55
- (4 points) You're given the following buggy snippet of code:
int a = 5, *b = &a, *c = &b;
printf("%d\n", *c);
-
(2 points) Without running this snippet, describe
why this code has a bug, and suggest what the program
might output.
-
(2 points) Suggest a change to this program that would fix
this bug and give correct output, and explain why it would
work.
- (3 points) You're given the following elements sitting in a
stack: 45, 57, 12, 9, 32, 10, 5 -- such that 45 is at the "top"
of the stack (i.e., the first element to be popped). Now, for
every element in this stack, we pop that element and push it
into a second stack. Once the first stack is empty, we now pop
each element in the second stack and print them out, one by one.
In what order are the numbers printed, and why?
Programming assignment: calendaring program
In this assignment, you're going to build a simple program that
will be able to store and list appointments, as well check for
simple scheduling conflicts (i.e., overlapping appointments).
In order to accomplish this, we will build a hierarchical data
structure and manipulate it appropriately.
For simplicity's sake, we will impose some restrictions on our
calendar program. First, it will only store one year's
information; in fact, you don't need to encode year anywhere
inside the program. Second, in order to detect conflicts, we're
going to represent times as ints, not strings or complex time
structures -- so, 12:15 might be represented as the integer
"1215", while 6:05 might be represented as the integer "605".
By doing so, we can easily check to see if events overlap while
adding them. We will also assume military time (i.e., no AM/PM
designators), and will assume valid input (i.e., the user's not
going to specify 2/31 at 64:05).
-
(4 points) Design the data structures for this assignment.
You will need the following:
- An event structure, consisting of the event's
name, a start time, and an end time. The times will be
stored as ints so that we can do comparisons on them.
- A day structure, consisting of an array of
events, and a counter to keep track of the number of events
actually filled with legitimate data. Set it up so that it
can contain at least 10 events.
- A month structure, which consists of an array of
days. Use 31 as the length so that any month can be
represented.
- A calendar structure, which consists of an array of
12 months.
- (3 points) Build an addEvent function that takes four
parameters: a pointer to a day structure, the name of the event,
and the start and end times defined as ints. Have it return an
int representing the success of the addEvent -- in particular,
have it fail if there is either a conflict or if there is no
more array space for events on that day. (In order to determine
scheduling conflicts, you'll have to scan the list of existing
events for that day and compare the times against the new event
to be added.)
- (2 points) Build three functions -- timeToInt, which
takes the hour and minute as separate parameters and returns an
int that combines the two; getHour, which extracts the
hour from the integer time, and getMinute, which extracts
the minute from the integer time.
- (2 points) Build a listEvent function that takes one
parameter -- a pointer to a day structure -- and prints out the
date (excepting year), followed by the events for that day.
Make sure to convert the time integers into individual
constitutients so that you can print out the times in a
human-readable form.
- (2 points) Build a listAllEvents function that takes one
parameter -- a pointer to the entire calendar structure -- and
prints out all of the events in the calendar. Have it
repeatedly call listEvent to do so.
- (2 points) Build a user interface inside main that
allows the following functions. Use a methodology similar to the
previous homeworks.
- a: Add an event. It should prompt for date,
name, start time, and end time, and should then call
the addEvent function. It should print an error if
there is a conflict and/or if the array is full.
- l: List events for a day. It should prompt
just for date, and then call the listEvent function.
- la: List all events. No further input is
required; the listAllEvents method is called.
- q: Quit.
Here are some hints you may find helpful.
- You do NOT need to use malloc for this assignment.
Static lengths for the arrays are sufficient.
- Make sure to zero out the counter in the day structure.
You can't do this in a struct declaration, so you'll
have to set up a nested loop inside main to do so.
- For the timeToInt function, simply add the hours and minutes
together after multiplying hours by 100. Then, to get the
individual parts, simply divide or mod by 100.
- To parse a "HH:MM" or "MM/DD" format input (i.e., if the
user types "6:00" without the quotes), you can use
sscanf after the initial fgets call. For example,
sscanf(tempString, "%d/%d", &tempMonth, &tempDay);.
- To print out a number using printf so that it is
a 2-digit number prefixed by zero (e.g., the minutes part of the
time), use the format specifier %02d.
- Remember that arrays are zero-based, unlike month or day
designators, so you'll have to shift by one occasionally to
convert dates to proper array locations.
(5 points extra credit) Create a new add feature called
"Add in next midday slot", which takes a date and the name of an
appointment and actually scans for the next available slot between
9AM and 5PM. You can assume hourly intervals. If you do this,
make sure to add a new user-interface option and leave the regular
add user interface option alone.