Questions, Answers and Review
 Questions from last class?
 Questions about homework?
Numbers and Arithmetic
Computations Using Numbers and Arithmetic
Types of Numbers
Voltage Computation
#include <ArduinoSTL.h>
using namespace std;
void setup() {
Serial.begin(9600);
cout << "Enter the current in amps" << endl;
double amps = 0, ohms = 0;
cin >> amps;
cout << "Enter the resistance in ohms" << endl;
cin >> ohms;
double volts = amps * ohms;
cout << "The voltage is " << volts << endl;
}
void loop() {
// no code here
}
Check Yourself
 The two types of numbers in C/C++ are ________ and ________.
 Of the following literal numbers, the single integer is ________.
1
1.2
1.23
1.234
 For the following code, the literal number is ________
Serial.begin(9600);
Arithmetic
 C++ uses the following operators for arithmetic:
 + for addition
  for subtraction
 * for multiplication
 / for division
 % for modulus (remainder after integer division)
 The first four operators should be familiar and we will discuss modulus (
% ) today
 As in algebra, multiplication and division (including modulus) are performed before addition and subtraction
 To change the order of operation, we use parentheses
 For example:
is written as: a + b / 2
is written as: (a + b) / 2
Notes About Arithmetic
 We can use parentheses to group expressions
 Anything within the parentheses is evaluated first
2 * (10 + 5)
 We can have parentheses within parentheses
 Innermost parenthesis evaluated first
(2 * (10 + 5))
 Parentheses cannot be used to indicate multiplication
 Invalid expression:
(2)(3)
 Must use the
* operator: 2 * 3
Programming Style
 Programming style: add spaces around binary operators
 Programming style: no spacing after opening or before closing parenthesis
Check Yourself
 The five arithmetic operators in C++ are ________.
+, , /, *, %
+, , \, *, %
+, , /, *, ^
+, , \, *, ^
 The first operation performed in the following arithmetic expression is ________.
1 + 2 * 3 / 4 % 5
 If we wanted a different ordering of operations in the above example, we add ________ to the expression.
MixedMode Expressions
 Different data types are stored in different sizes on the Arduino, like:
 An
int is stored in 2 bytes
 A
float or double is stored in 4 bytes
 The numbers are stored in different binary formats as well:
 The computer needs both operands in the same format before it can perform arithmetic
 If one operand is different than the other, the compiler converts it to the wider of the two types
 For example:
2 + 2.3
 First number (
2 ) is an int
 Second number (
2.3 ) is a double
 C/C++ will automatically convert an
int to a double
 Then the arithmetic operation can take place to produce a result of
4.3
 Remember that the result of arithmetic with an
int and a double is a double
Check Yourself
 The result of adding an integer with a double in the following expression is ________.
1 + 2.3
 In the above expression, C++ converts the integer
1 to type ________.
 The data type of the number returned by the following expression is ________.
3 + 4.5
Integer Division and Modulus
Program Testing for Even or Odd Numbers
#include <ArduinoSTL.h>
using namespace std;
void setup() {
Serial.begin(9600);
cout << "Enter an integer number" << endl;
}
void loop() {
int testNum;
cin >> testNum;
int quotient = testNum / 2;
int remainder = testNum % 2; //pronounced testNum "mod" two
cout << testNum << " / 2 = " << quotient << endl;
cout << "The remainder is " << remainder;
if (0 == remainder) {
cout << " (even)" << endl;
} else {
cout << " (odd)" << endl;
}
}
Check Yourself
 In division between two integer numbers, the remainder is ________.
 rounded up
 rounded down
 averaged
 truncated
 To compute the integer remainder we use the operator ________ .
 What is the result of the following arithmetic operations?
9 / 4
17 / 3
14 / 2
9 % 4
17 % 3
14 % 2
Mathematical Functions
 Operators provide only the simplest mathematical operations
 For more complex operations, we use mathematical functions
 A C/C++ function is like a mathematical function that takes an argument ("input") and produces ("returns") a value
 For example:
cout << sqrt(9.0);
 In the above example, the input is
9.0 and the sqrt() function returns the square root of the argument
 The Arduino IDE has built in Math and Trigonometry functions documented in the Language Reference
 Some of the commonly used functions are listed below
Some Commonly Used Math Functions
Name 
Description 
Example 
Result 
abs (value) 
Absolute value of a number 
abs(3.9)
abs(3.9) 
3.90
3.90 
max (x, y) 
Maximum of two numbers 
max(1, 2.2) 
2.20 
min (x, y) 
Minimum of two numbers 
min(1, 2.2) 
1.00 
pow(base, exp) 
Number raised to a power (x^{y}) 
pow (2.0, 3.0) 
8.00 
sqrt(x) 
Square root of a number 
sqrt (4.0) 
2.00 
Some Commonly Used Trignometric Functions
Name 
Description 
Example 
Result 
cos (rad) 
Cosine of an angle in radians 
cos(PI) 
1.00 
sin (rad) 
Sine of an angle in radians 
sin(PI) 
0.00 
tan (rad) 
Tangent of an angle in radians 
tan(PI / 4) 
1.00 
Using Mathematical Functions
Check Yourself
 The following code snippet prints ________.
int x = 42;
cout << abs(x);
 The value of
x after following code snippet executes is ________.
double x = pow(3, 2);
2
3
8
9
 The value of
x after following code snippet executes is ________.
double x = max(4, 2);
x = sqrt(x * x);
2
4
8
16
More Information
 Language Reference: Arduino documentation on core functions, including Math and Trigonometry
 math.h: Arduino documentation on trignometric and exponential functions
Assignment Operators
Compound Assignment Operators
 C/C++ has additional assignment operators that combine other operators with assignment
 Known as compound assignment operators
 The general syntax is:
variable op = expression ;
 Where op is an operator like one of the five arithmetic operators:
+ ,  , * , / , %
 For example, the following two statements create the same result:
x = x + 3;
x += 3;
 Shown below are some assignment operators with examples of how they are used
Summary of Assignment Operators Used in Arithmetic Operations
Operator 
Description 
Example 
Equivalent To 
= 
Assigns the value of the expression on the right to the variable on the left 
x = 3 

+= 
Adds the expression on the right to the variable on the left 
x += 3 
x = x + 3 
= 
Subtracts the expression on the right from the variable on the left 
x = 3 
x = x  3 
*= 
Multiplies the expression on the right to the variable on the left and saves the result in the variable on the left 
x *= 3 
x = x * 3 
/= 
Divides the variable on the left by the expression on the right and saves the result in the variable on the left 
x /= 3 
x = x / 3 
%= 
Calculates the remainder from dividing variable on the left by the
expression on the right and saves the result in the variable on the
left 
x %= 3 
x = x % 3 
Increment and Decrement Operators
Check Yourself
 The value of
count after the following code executes is ________.
int count = 0;
count = count + 1;
 What is an equivalent statement for each of the following shortcut assignments?
a += b;
a = b;
a *= b;
a /= b;
a %= b;
 List four ways to add the integer
1 to a variable named c .
Exercise 1: Calculator Deluxe
In this exercise we convert your Arduino into a deluxe calculator!
Parts
Starter Code
#include <ArduinoSTL.h>
using namespace std;
void setup() {
Serial.begin(9600);
cout << "Enter two numbers separated by a space" << endl;
}
void loop() {
int a;
int b;
if (Serial.available()) {
cin >> a >> b;
cin.ignore(1);
cout << "You entered: " << a << " and " << b << endl;
int numAdd = a + b;
cout << "a + b = " << numAdd << endl;
// add more arithmetic code here
}
}
Specifications
 Start the Arduino IDE, copy the starter code above and paste it into the main IDE window.
 Save the project using the name calculator (
File > Save As... ) to a convenient location like the Desktop or the Arduino projects folder.
 Compile the sketch to verify you copied the starter code correctly.
 Upload the code and open the Serial Monitor to view the output.
 Add equivalent code for subtraction, multiplication, division and modulus. For example, here is the code for subtraction:
int numSub = a  b;
cout << "Subtraction: " << numSub << endl;
Hint: copy and paste the above code and then modify the copied code for each type of operation.
 Compile and upload your code to verify it works correctly.
You should now be able to see output like the following in the Serial Monitor:
Enter two numbers separated by a space
You entered: 3 and 2
a + b = 5
a  b = 1
a * b = 6
a / b = 1
a % b = 1
 Attempt to change the data types of a and b as follows:
double a;
double b;
Why doesn't the calculator show you floating point numbers after making this change? What would you have to change to make it work?
 Save your
calculator.ino file to submit to Canvas with the next homework.
When finished, please help those around you.
Summary
Wrap Up and Reminders
 When class is over, please shut down your computer.
 Complete unfinished exercises from today before the next class meeting
