purpose | preparation | procedure

This lab will allow you to practice writing simple functions that perform arithmetic calculations (e.g., the state of a battery).

The slides that Ms Hogan, the lab instructor, delivered in the lab are visible in the frame to the right. If they are difficult to see, however, you can also download the slides.

In the lab, you will write a function to calculate the current state of your robot’s batteries. This will give you a chance to:

- practice translating mathematical expressions into C++,
- practice writing the definition of a simple arithmetic function and
- practice calling a function.

Before coming to the lab, you should ensure that you have completed the
setup procedure from last week.
This procedure needs to be completed **the first time you program a robot**
using the Eclipse workspace in your account.

You should also complete the following prelab questions:

What is an

*expression*?~

~~When a function is~~~*declared*,*defined*and*called*within a C++ source file, what are the constraints on their ordering? That is, must any of these things appear before any others in the file?Given the following graph for operating pressures in a fictitious system:

write an equation that describes the relationship between $P$ (the pressure in the system) and $x$ (a percentage ranging from 0 at minimum pressure to 100% at maximum). Solve for $P$. Solve for $x$.

Visit the 3$\pi$ robot’s specifications page. What is the minimum operating voltage? The maximum?

Given a variable $V$ (the current battery voltage in a 3$\pi$ robot), write an

**expression**for the voltage as a percentage of operating range (i.e., 0 for the minimum operating voltage and 100% for the maximum). What will the percentage value be if the battery voltage is 3.2 V? What will it be if the battery voltage is 5.5 V?

Download lab1.zip (right-click, “Save link as…”) and import it as an existing project:

`File`

> `Import`

> `General`

> `Existing Projects into Workspace`

`percentage`

functionAdd a definition for the `percentage`

function.

- the declaration is already given in
`lab1.cpp`

:

```
/*
* Calculate the percentage of a value in a [minimum, maximum] range.
*
* @pre value, minimum, maximum should all be >= 0
*/
int percentage(double value, double minimum, double maximum);
```

given this declaration, you must write the

**definition**somewhere in`lab1.cpp`

**hint:**you may find the “functions” module in the course notes helpful as a reminder

`printBatteryLevel`

functionFinish the implementation of the `printBatteryLevel`

function.

- currently, the function prints out the voltage in mV, followed by
`0 %`

- you must modify the second call to
`print_long`

:- remove the value 0
- replace it with an expression for the battery voltage as a percentage

Compile your program:

If the **Problems** tab at the bottom of the screen is empty:

download your program to the 3$\pi$:

Record your observations in your log book.

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.