Objectives
- Gain experience with boolean behaviour in C++
- Write some multi-way decisions and complex Boolean expressions
Setup
Update your labs repo by cd
ing 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:
- 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
Read (using
cat
orless
) the filedecisions.cpp
and examine the functiontrace_me
. Without running the program, try to predict and write down what you think the output will be.Next, call
trace_me
frommain.cpp
, compile, and run the program. Was your prediction correct? If not, why not?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.
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
The tiny island nation of Babbage charges its citizens income tax each year. The tax rates are as shown in the following table:
Income Tax Rate $0 - $5,000 0% $5,001 - $10,000 3% $10,001 - $20,000 5.5% Over $20,000 10.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.
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:
Sensor Timer Slow Street Light Explanation true
59 Red Timer is not at 60 yet true
60 Green Timer is at 60 and cars are present true
120 Green Another minute has elapsed and cars are present false
180 Red No cars are present true
119 Red Another minute hasn’t elapsed yet Implement the function
slow_gets_green
such that it returnstrue
if Slow should get a green light, andfalse
otherwise.In
main
, prompt the user for the sensor and timer values, then callslow_gets_green
. If it returnstrue
, 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 runningmake clean
first to delete the old test program. Sometimes it gets confused.
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
Read the function
bonus
. What output do you expect? Call it frommain
to check your answer.Can you modify the condition so that when the numbers are very close to each other the program treats them as equal?
Declare a new function named
compare_double
infunctions.h
, then implement it infunctions.cpp
. It should take two double values to compare, plus a double value to indicate a precision. The function must returntrue
if and only if the two values are considered equal, within the given precision. Inbonus
, update theif
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 .