# Lab 1

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

## In-lab presentation

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.

## Purpose and Outcomes

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.

## Preparation

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:

1. What is an expression?

2. ~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?~

3. 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$.

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

5. 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?

## Procedure

### Import the lab template

File > Import > General > Existing Projects into Workspace

### Implement the percentage function

Add 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

### Complete the printBatteryLevel function

Finish 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

download your program to the 3$\pi$: