Lab 07: Decisions

Jan 25, 2024  β”‚  m. Jan 29, 2024 by Charlotte Curtis

Objectives

Setup

Update your labs repo by cding into it and running git pull. Then, cd into the decision directory to begin the lab.

If the git pull does not work, here are a couple of things you can try:

  1. Do you have any uncommitted changes from the last lab? If so, you will need to add and commit them before you can 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 .

Warm up

  1. Read (using cat or less) the file decisions.cpp and examine the function trace_me. Without running the program, try to predict and write down what you think the output will be.

    Next, call trace_me from main.cpp, compile, and run the program. Was your prediction correct? If not, why not?

  2. Determine (by examining, not running the code!) how to make trace_me produce the opposite output by inverting only one variable’s initialization. Which variable?

    Make the change and validate your analysis.

  3. Implement the function num_range. This function should take an int as a parameter and write the message “low range” if the number is less than 50, “high range” if the number is greater than or equal to 80, and “middle range” otherwise.

    Test num_range with a variety of inputs to ensure it works correctly. What inputs should you use to test exhaustively?

Main Exercises

  1. The tiny island nation of Babbage charges its citizens income tax each year. The tax rates are as shown in the following table:

    IncomeTax Rate
    $0 - $5,0000%
    $5,001 - $10,0003%
    $10,001 - $20,0005.5%
    Over $20,00010.8%

    Implement the function babbage_tax that takes a citizen’s income and returns their tax bill.

    Call your function from main and print the results.

  1. Speedy Street is a very busy road. At its intersection with Slow Street, there is a set of traffic lights. Because there isn’t much traffic on Slow, the cars on Speedy should have a green light most of the time. However, Slow should get an occasional green if one or more cars have been waiting there for awhile. To this end, the city engineers have installed a sensor and a timer at the lights. The sensor senses if cars are present on Slow. The timer is a clock which counts up from zero in seconds. Every 60 seconds, if the sensor indicates cars are present, Slow gets a brief green light.

    Note: when the lights are first powered on, the timer starts counting from zero. It increments by one each second, and does not reset back to zero after 59.

    Several examples of traffic light behaviour are as follows:

    SensorTimerSlow Street LightExplanation
    true59RedTimer is not at 60 yet
    true60GreenTimer is at 60 and cars are present
    true120GreenAnother minute has elapsed and cars are present
    false180RedNo cars are present
    true119RedAnother minute hasn’t elapsed yet

    Implement the function slow_gets_green such that it returns true if Slow should get a green light, and false otherwise.

    In main, prompt the user for the sensor and timer values, then call slow_gets_green. If it returns true, print “Slow gets a green light”, otherwise print “Slow gets a red light”.

    Hint: you can’t read a boolean directly from the user, but you can read a char and convert it to a boolean. For example:

    char sensor;
    cout << "Does the sensor detect a car on Slow street? (y/n) ";
    cin >> sensor;
    bool sensor_detected = (sensor == 'y' || sensor == 'Y');

    When you are satisfied that the function is behaving, cd to the top-level labs directory and build and run the test program:

    $ cd ..
    $ make lab=decisions
    

    If it says something about test is up to date, try running make clean first to delete the old test program. Sometimes it gets confused.

  1. Implement the function print_race_results that should determine the order of the top three finishers in a race based on the runner’s times. It is guaranteed that there will be no equal times, i.e. ties are not possible. The function should output the finishers in order of placing, i.e. first place, second place and third place.

    For example a run of the program should look like this:

     $ ./main
     Enter the times for three runners in order of 1 to 3, each separated by one space: 12 8 14
     First place is runner 2
     Second place is runner 1
     Third place is runner 3

    In this example, user input is read in main before passing the values to the function.

Bonus Exercise

  1. Read the function bonus. What output do you expect? Call it from main to check your answer.

    1. Can you modify the condition so that when the numbers are very close to each other the program treats them as equal?

    2. Declare a new function named compare_double in functions.h, then implement it in functions.cpp. It should take two double values to compare, plus a double value to indicate a precision. The function must return true if and only if the two values are considered equal, within the given precision. In bonus, update the if statement’s test so that it uses this new function.

      For example, for a precision of 0.0001:

      • 3.141593 is considered equal to 3.1415927
      • 3.142 is not considered equal to 3.1415927

    Note: this function must work for all values which the caller might try to compare. The order in which the two are passed shouldn’t matter. Therefore, you should consider using the abs function from the <cmath> library within your solution.

Assessment

When you are satisfied with your slow_gets_green function, add and commit your changes to your labs repo, then git push to submit. Your instructor will be building and running the exact same tests to determine whether you passed the lab.

There’s been some confusion over which files need to be committed, and the short answer is anything that you’ve changed should be committed. An easy way to do this is to add all your modified files to the staging area with:

$ git add .

where the . is a shortcut for “current directory”.

Then commit and push as usual.

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 06: Pass by reference
Next: Lab 08: loops