Lab 06: Pass by reference

Jan 23, 2024  β”‚  m. Jan 26, 2024 by Charlotte Curtis

Objectives

Program description

When complete, this lab’s program should do the following:

Setup

Update the source code for your labs repo by running the following commands in the terminal:

$ cd labs
$ git pull

January 26 update: If you haven’t run the steps to fix your labs configuration, now is a good time. Follow the instructions here .

Steps

  1. Open by_ref.cpp, by_ref.h, and main.cpp. Copy the function header for get_time from the header file to by_ref.cpp and implement a function that reads a time entered in the format HH:MM and updates 2 reference parameters - the hours and the minutes. You can assume the data is always in the correct format.

    To test this write a simple test driver main program (in main.cpp) that writes a prompt, calls the function, and prints out the time. Don’t worry about formatting - just use a simple cout.

  2. Modify main so it also prompts for a waiting time, calls the get_time function to read the wait time, and prints it out (mostly to confirm things are working).

  1. Now implement the function calc_end_time. This function receives 4 input parameters (hours and mins for start time and wait time) and updates 2 reference parameters (hours and mins for the end time). The time returned must be valid in 24 hour time (e.g. if you start at 23:30 and wait 2:15, the end should be 1:45).

    This is the function that is assessed for the lab! From now on, this yellow box highlights the assessed code.

  1. Modify main to call calc_end_time (and print out the end time after the call)

  2. Implement the function convert_12hour, which is given a reference to the hours in 24-hour format. This function should update the hours parameter to represent the time in a 12-hour format. It should also update a parameter which will contain 'A' or 'P' for AM or PM respectively.

    Hint: think about how you would cycle around the clock - the modulo operator might be handy! You will need an if statement for the AM/PM part though.

  3. Modify main to call convert_12hour and print out the result as follows:

    Finish at 7:25PM
    
  4. To test your complete program, create a text file called time.txt. It should just contain 2 lines as follows:

    17:45
    1:40
    

    Use this file as the source of input (rather than having to enter it from the keyboard) by using a feature of Linux called input redirection. This is a useful way to saving test input, so that you don’t need to retype it each time you run a program.

    To use input redirection, run your program as follows:

    $ ./main < time.txt
    

Lab Assessment

As in the previous lab, there is a unit test associated with this lab. To run it, cd up a directory to your top-level labs repository and recompile the test program:

$ cd ..
$ make lab=by_ref

This will both compile and run the test program. Note that this has also changed from last week, as I realized that there are more problems caused by trying to run the tests for all labs at once.

January 26 update: If you haven’t run the steps to fix your labs configuration, now is a good time. Follow the instructions here .



Previous: Lab 05: Functions
Next: Lab 07: Decisions