Lab 6

purpose | preparation | procedure

In this lab, you will iterate over arrays using a loop.

Purpose and Outcomes

The purpose of your lab this week to make a 3π robot trace out arbitrarily-complex shapes. For instance, instead of merely driving forwards or turning as in lab 3, you will make your robot trace out a square, drive back and forth between two points or even do a little dance!

In this lab, you provide descriptions of patterns for your robot to draw out in the form of arrays of distances and angles. You must write C++ code to make the 3π robot interpret each value in these arrays and follow the provided instruction. You will do this by implementing the drawShape function that is documented in lab6.h:

 * Have the robot draw a particular shape by providing arrays of distances and angles
 * for the 3pi robot to travel and turn, respectively.
 * @param   distances   an array of distances (in m) you want your robot to travel,
 *                      sequentially; must be of size length
 * @param   angles      an array of angles (in deg) you want your robot to turn
 *                      sequentially (turns occur between each travel segment);
 *                      must be of size length
 * @param   length      the number of distances and angles in distances and angles arrays
void drawShape(double distances[], double angles[], int length);

You will also need your implementations of driveForward and turn from lab 3.


Answer the following questions:

  1. What is the interior angle of a square?
  2. What is the interior angle of a five-pointed star?
  3. Assuming that you have a robot that can be made to drive forwards and turn with functions called driveForward and turn, write a list of instructions to make your robot draw the following shapes:
    • a square
    • the letter “A” and
    • a shape of your choosing
  4. Five-pointed star Assuming that you have two arrays of the same length (called angles and distances) and the following pseudocode:

    loop with i from 0 to length-1:

    what values should the angles and distances arrays contain to make the robot draw a five-pointed star?


1. Initial setup

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

  2. Create a stub (incomplete) implementation of the drawShape function.

  3. Copy your implementations of driveForward and turn from lab 3.

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

2. Implement the drawShape function

Define the drawShape function, declared for you in lab6.h, using the pseudocode from question 4 of the pre-lab preparation.

3. Draw a star shape

Using your answer to Question 3 of the pre-lab preparation, Change the code in main that draws a square to instead draw a five-pointed star.

4. Draw a shape of your choosing

Using your answer to Question 3 of the pre-lab preparation, change the code in main to draw a shape of your choosing.

5. Conclusion

Describe what you observed in this lab. Were there any pitfalls along the way? What did you learn?

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.