Emphasis on: structs or classes, arrays of structs or classes, sorting
You are to read a series of 24-hour time values (hrs and mins only no secs needed). Use either a struct or go ahead and use the class to represent the time. .
Processing (these operations dont necessarily have to be done in this order):
1) Read in the times from the file times.txt and store them in an array (read hours and minutes in integer format and use the SetTime function to store into an array of structs or classes.
2) Modify each time by adding to it a random number of minutes (0-100 minutes) also read from the file. Write a function to add a parameter number of minutes to the times.
3) Sort the resulting times into ascending 24-hour clock order. You can use the bubble sort function from the Malik book (Ill send you a file called Search and Sort Summary that contains all the searches and sorts from the book). You will probably want to write a function to compare two times. You have an EqualTime function, but you need to write a greater than function.
4) Print the list of sorted times. You have a PrintTime function that you can use.
Input: read from text file times.txt
Format:
col 1 – 5 a time in 24-hour-clock format, like this hh:mm
7 – 9 an integer number of minutes to be added to the time
To declare an array of class variables (objects):
ClockType times[20]; // looks and works just like an array of structs except that the data
// members are private instead of public
To assign a single time variable (object) to an array element:
ClockType inputTime;
inputTime.SetTime (inputHr, inputMin); // inputHr and inputMin are single ints
times[n] = inputTime;
You can add the extra minutes whenever you want to:
Read the input times:
Struct version:
1) read int hrs value
get rid of :
read int mins value
assign hrs and mins directly to an element of an array of structs
2) read int hrs value
get rid of :
read int mins value
assign hrs and mins to a single struct
copy single struct to an element of an array of structs
For class version:
prototype: void AddMinutes (int extraMins);
to call it: inputTime.AddMinutes (extraMins);
or: times[n].AddMinutes (extraMins);
prototype: bool GreaterThan (ClockType & b);
to call it: if (times[n].GreaterThan (times[n+1])
3) read int hrs value
get rid of :
read int mins value
assign hrs and mins directly to an element of an array of class objects (with SetTime
function)
4) read int hrs value
get rid of :
read int mins value
assign hrs and mins to a single class object (with SetTime function)
copy single object to an element of an array of objects
Bubble Sort
This version of the sort will sort an array of ints into ascending order. Youll need to change the highlighted components to handle an array of structs or an array of classes.
void BubbleSort (int list[], int length)
{
int temp; // used to hold an array element value in a swap of two elements
int iteration; // controls how many times to pass through the array (go through the loop)
int index; // subscript to look at the first of a pair of adjacent element values
for (iteration = 1; iteration < length; iteration++)
{ // make one pass through the array – iteration counts how many times
for (index = 0; index < length – iteration; index++)
// compare adjacent (side-by-side) element values
if (list [index] > list [index+1]) // if they’re out of order
{
temp = list [index]; // swap them to put them in order
list [index] = list [index+1];
list [index+1] = temp;
} // otherwise leave them as they are
} // end of outer loop to make one pass through the array
}
Comparing two times for greater than:
When do we have to compare minutes values?
Examples: if we're comparing 2:56 and 3:10, do we care about minutes?
if we're comparing 3:15 and 2:10, do minutes matter?
Emphasis on: a linked list of structs
This assignment is a rerun of program 2, except that were going to use a linked list to store the times in ascending time order rather than storing them in an array and sorting them.
You are to read a series of 24-hour time values (hrs and mins only no secs needed). Use either a struct or a class to represent the time. You are either given (SetTime, PrintTime, EqualTime) or have already written (AddMinutes, GreaterThan) the functions needed for this program.
The pointer operations you will use in this program are the same as those in the sample program Client-Managed Singly Linked List. For the greatest benefit you should try to write your own version and only look at my code if you get stuck.
Processing:
1) Read in the times from the file times.txt and insert them in time order into a linked list (read hours and minutes in integer format and use the SetTime function to store into dynamically allocated structs or classes.
2) Modify each time by adding to it a random number of minutes (0-100 minutes) also read from the file (either before or after assigning to a newly created node).
3) Print the list of times (Id recommend printing the list after each insertion as a debugging aid).
Input: read from text file times.txt
Format: col 1 – 5 a time in 24-hour-clock format, like this hh:mm
7 – 9 an integer number of minutes to be added to the time
Hints:
I found this assignment much easier implementing the time as a struct rather than as a class. There are a lot of nuances to creating and using classes that we didnt cover. Using a struct has the additional advantage of your program being more like my sample program.
So in the Intro to Classes document you were given a ClockType struct and three functions: SetTime, PrintTime, and EqualTime. For program 2 you wrote a GreaterThan function and an AddMinutes function. If you used a class for program 2, you would need to convert it back to operating on structs rather than classes restore the ClockType parameter and the qualifications of the struct fields. I didnt actually use the PrintTime or EqualTime functions. The sample program had a CreateInitialList function which wasnt necessary in this assignment because all of the times were in random order. Also there was no need to delete a time in this assignment, so I eliminated the Delete function and did a search and insert for each input time.
I considered creating a nodeType struct with a ClockType field and a link field, but ended up just adding the link pointer as a third field in the ClockType struct. I read the input hours and minutes into separate individual int variables and added the extra minutes immediately after input.
The SearchList function in the sample program had one int info parameter. You could either add a second int parameter to pass both the hours and the minutes values, or you could create one ClockType variable and use the SetTime function to assign the hours and minutes to the fields of the struct. Then you just had to change the int inputData parameter to a ClockType inputData parameter. It also allowed me to use the GreaterThan function that I had already written for program 2. The Search function called the GreaterThan function to locate the position where the input time should be added.
Since there was no possibility of a delete, I eliminated the return bool value from the Search and just automatically inserted the new time once the Search had located where it should be added. I wrote a PrintList function similar to that in the sample program and copied and pasted in the formatting for the output times from the PrintTime function, but you could call the PrintTime function from the PrintList function to get the same output formatting.
There are lots of different choices you could make in this assignment, but what I have described here worked for me. The pointer manipulations in the sample program were mostly unchanged. The main difference was that there were two data fields (hours and minutes) to read, assign, compare, and write instead of the one info field as in the sample program.
My algorithm for the main function:
While (not end of file) (I used the peek function to detect end of file)
{
Read hours, minutes, and extra minutes
Read the end-of-line marker (to make peek stop the loop when all the data is read)
Add the extra minutes (using an AddMinutes function)
Copy the hours and minutes into a single struct variable
Search the list to find where this time should be added
Insert the time into the list
Print the list (for debugging purposes to make sure the new time was properly added)
}
Print the final list