Lab 5

purpose | preparation | procedure

This assigment will give you the chance to practice passing arguments by reference.

Purpose and Outcomes

In this lab, you will use pass-by-reference to effectively “return” multiple values from a single function. While doing this, you will also continue practicing loop design.

This week, your robot will be required to drive along a line that has other lines intersecting with it (see example to the right). These intersections, as shown, may be on one side or both. You must write C++ code to make the 3π robot:

  1. drive to the end of a line and
  2. count the intersections observed to the left or the right.


  1. Imagine that you are being driven along a road with many small towns on it. In this scenario, you cannot see out of the vehicle, but you can ask “are we currently in a town” as often as you like (and receive a truthful answer). Describe, in pseudocode, an algorithm for counting the number of towns that you are driven through.

  2. Given a function double temperature() that returns the current temperature in degrees Celsius and a function void pause() that pauses for ten minutes, describe an algorithm (in pseudocode) that will count the number of times the temperature crosses zero (from positive to negative or vice versa) in a day.

  3. Find the declaration for readSensors() in’s engi1020.h and summarize what its purpose is, what its parameters mean, and what its return value means.


1. Design the countIntersections function

It can be quite helpful to design your algorithm before you open Eclipse and start to write code. Specifically, you should design an algorithm that will make your robot:

  1. drive to the end of the line,
  2. beep whenever it encounters an intersection and
  3. count the intersections on the left vs the right.

When designing this algorithm, you should consider:

  • what information will you need to keep/remember in order to solve the high-level problem?
  • in the loop that you will need to design:
    • in the general case (i.e., not worrying about how to start or stop):
      • what information will you get from readSensors()?
      • what should you do with this information?
    • in special cases (e.g., the first and last times through the loop):
      • what do you assume when the countIntersections function is called?
      • how will your robot know when to stop?

2. Initial setup

  1. Import the lab 5 template file ( just as you did in previous labs.

  2. Create a stup (incomplete) implementation of countIntersections(int&, int&).

  3. Ensure that the code compiles and can be downloaded to your 3$\pi$ robot, even though it doesn’t yet make the robot move.

3. Implement the countIntersections function

You must define the countIntersections function that has been declared for you in lab5.h and that you designed above.

4. Conclusion

Describe what you observed in this lab. What important techniques did you use to make your algorithms work correctly?

Submit your work:

Once you have finished writing up your lab activity, submit your log book to the TAs in the lab. If you need extra time to finish the lab, you can submit your log book in the Engineering One Help Centre.