$24
C1A7E0 (6 points total - 1 point per question – No program required)
Assume language standards compliance and any necessary standard library support unless stated otherwise. These are not trick questions and there is only one correct answer. Basing an answer on actual testing is risky. Place your answers in a plain text “quiz file” named C1A7E0_Quiz.txt formatted as:
a “Non-Code” Title Block, an empty line, then the answers:
A
C etc.
Personalized C1A7 requirements exclusively for Jose Medrano (U09845800)
C/C++ Programming I (Section 174273)
C1A7E1 (7 points – C++ Program)
Exclude any existing source code files that may already be in your IDE project and add three new ones,
naming them C1A7E1_MyTime.h, C1A7E1_DetermineElapsedTime.cpp, and C1A7E1_main.cpp. Do not
use #include to include either of the two .cpp files in each other or in any other file. However, you may
use it to include any appropriate header file(s) and you must include C1A7E1_MyTime.h in any file that
needs data type MyTime.
7
C1A7E1_MyTime.h must be protected by an include guard (note D.2) and must define type MyTime
exactly as shown below and contain a prototype for function DetermineElapsedTime.
10
struct MyTime {int hours, minutes, seconds;};
11
C1A7E1_DetermineElapsedTime.cpp must contain a function named DetermineElapsedTime that
computes the time elapsed between the start and stop times stored in the two type MyTime structures
pointed to by its two parameters, stores it in another MyTime structure, then returns a pointer to that
structure. For example, if the start time is 03:45:15 (3 hours, 45 minutes, 15 seconds) and the stop time is
09:44:03, DetermineElapsedTime computes 05:58:48.
IMPORTANT: If the stop time is less than or equal to the start time, the stop time is for the next day.
Function DetermineElapsedTime must:
• Have only two parameters, both of type “pointer to const MyTime”.
• Not modify the contents of either structure pointed to by its two parameters.
• Not declare any pointers other than its two parameters.
• Not declare any structures other than one that will hold the elapsed time.
• Not prompt or display anything.
• Return a “pointer to MyTime” that points to a MyTime structure containing the elapsed time.
25
C1A7E1_main.cpp must contain a function named main that contains a “for” statement whose body
gets executed 3 times. No other looping statements are permitted. The following must be done in order
during each execution:
1. Prompt the user to enter the start and stop times (in that order), space-separated on the same
line. Each must be in standard HH:MM:SS 2-digit colon-delimited format and the time values
must be input directly into the appropriate members of two MyTime structures.
2. Although a “real life” program would require that you carefully parse the input to ensure proper
formatting, use the minimalist approach below for this exercise. start is a MyTime structure and
delim is a type char variable whose value you must ignore:
35 cin >> start.hours >> delim >> start.minutes >> delim >> start.seconds
3. Call DetermineElapsedTime passing pointers to the two structures containing the user-entered
times, then store the pointer it returns in a type “pointer to MyTime” variable.
38 4. Display the user-entered times and the elapsed time in the standard HH:MM:SS 2-digit
colon-delimited format shown below. Use the pointer variable from the previous step to access
the elapsed time:
41 The time elapsed from HH:MM:SS to HH:MM:SS is HH:MM:SS
Function main:
• must not contain “if” statements or “?:” expressions.
• may declare non-pointer, non-structure, and non-char variables as appropriate, but only one
pointer, two structures, and one char.
46
• Use military time for both input and output: 23:59:59 is 1 second before midnight, 00:00:00 is midnight,
and 12:00:00 is noon.
• Use no non-constant external variables, including external structure variables.
• Use no dynamic storage allocation.
• Test with at least the following three start/stop time pairs:
52
00:00:00
00:00:00
12:12:12
13:12:11
13:12:11
12:12:12
1992-2022 Ray Mitchell
Page 1 of 2 of C1A7E1
Personalized C1A7 requirements exclusively for Jose Medrano (U09845800)
C/C++ Programming I (Section 174273)
See the course document titled “How to Prepare and Submit Assignments” for additional exercise
formatting, submission, and assignment checker requirements.
Hints:
57
How to define a structure type and declare and initialize an array of them in 1 statement:
Here is an example in which all members of the first 3 structures in an array of 4 structures named
boxes are initialized explicitly, while all members of the last structure in that array are initialized
implicitly:
62
struct Box
{
65 int height, width, depth, weight;
} boxes[4] = { { 8, 5, 7, 100 }, { 2, 9, 1, 4 }, { 26, 78, 16, 99 } };
Freeing memory
In this exercise you must individually dynamically allocate separate blocks of memory for each of
the foods input by the user. As a result, you must also individually free each of them before the
program terminates.
Testing dynamic memory allocations
Failing to test for successful dynamic memory allocations always is an error.
Uninitialized pointers
Simply declaring a pointer does not make it point to a valid location. All pointers must be explicitly
initialized before dereferencing. In this exercise the three uninitialized name pointers must be made
to point to a usable area of memory before the food names are stored. Dereferencing uninitialized
pointers often causes core dumps (crashes) or even more subtle problems.
Pointers and Memory Diagrams
As with all exercises involving pointers, if you have any doubts or problems, you should draw one or
more diagrams of relevant memory objects showing how they are affected by the various program
steps. In many cases it is beneficial to first draw a diagram of what those objects should look like
when your program has completed its primary task. This will allow you to then step through your
code and verify that it actually produces that configuration. On the next page I have drawn this
“finished” memory diagram for you for some typical user food inputs.
91 Please see the diagram on the next page.
1992-2023 Ray Mitchell Page 2 of 3 of C1A7E2
Page 7 (7/11/2023)