Welcome to Lesson 5! Learning Objectives of Today's Lesson
Announcements
Review Exercises Find a partner and answer the following questions.Part 1:
What Gets Printed to the Screen? //assume below statement are part of a main function int oranges = 5; int apples = 3; int numFruit = apples + oranges; cout << "Total Fruit: " << numFruit << endl; apples = apples - 2; oranges = apples; cout << "Apples: " << apples << endl; cout << "Oranges: " << oranges << endl; Input and Output Cont'd
Output Using |
Name | Description | Example | Result |
---|---|---|---|
fabs | absolute value | fabs(-3.9) fabs(3.9) | 3.9 3.9 |
exp | exponent (e^{x}) | exp(1.0) | 2.71828 |
log | natural log | log(10.0) | 3.10259 |
pow | powers (x^{y}) | pow(2.0, 3.0) | 8 |
sqrt | square root | sqrt(4.0) | 2 |
sin | sine | sin(0) | 0 |
cos | cosine | cos(0) | 1 |
- In addition, the
cmath
library includes two similar functions:ceil
, andfloor
Name Description Example Result ceil ceiling: round up ceil(3.3)
ceil(3.7)
4
4
floor floor: round down floor(3.3)
floor(3.7)
3
3
- Both return whole numbers, although they are of type
double
- Why might this be the case?
Using Mathematical Functions
- How are mathematical functions evaluated?
- Whatever is within the parenthesis of the function call is evaluated first
- Thus, in the following example, we get the square root of
9.0
cout << sqrt(3.0 * 3) << endl;
- If the function is used in an arithmetic expression, they are handled just like a number of the type returned
- For example, in the following, the value
4.0
is stored in the double variablenum
:double num = 1 + sqrt(3.0 * 3.0); cout << num << endl;
- Note that the function evaluates the
sqrt(3.0 * 3)
before adding it to1.0
- Thus functions have a higher precedence than arithmetic operators.
Activity 5.2: Arithmetic (10 pts)
Through the miracles of computer science, we will now convert your $1000 computer into a $10 calculator! Along the way, we learn how to work with arithmetic using C++.
Specifications
- Find a partner for pair programming.
- Open up Eclipse and create a new project called Arithmetic with a file named arithmetic.cpp.
- At the top of your program, add a block comment with your names and section information:
- Within the curly braces of the
main()
function, declare twodouble
variables nameda
andb
, and assign them a value of7
and2
respectively. For instance:double a = 7, b = 2;
- Add a line of code to display the arithmetic expression
(a + b)
and then recompile and run the program.cout << "a + b = " << a + b << endl;
The output when you run the program should look like this:
a + b = 9
If you do not see this output, please ask a classmate or the instructor for help.
- Add three more lines of code like the previous one that computes the expressions:
a - b
,a * b
anda / b
. Compile and run your program again and make sure your program now displays the following output:a + b = 9 a - b = 5 a * b = 14 a / b = 3.5
- The
order of operations matters in C++ just like it does in algebra.
Multiplication and division are performed before addition and
subtraction. Add the following two statements to your program:
cout << "a + b / 2 = " << a + b / 2 << endl; cout << "(a + b) / 2 = " << (a + b) / 2 << endl;
- Compile and run your program again and compare the output. Your program should now display the following output:
a + b = 9 a - b = 5 a * b = 14 a / b = 3.5 a + b / 2 = 8 (a + b) / 2 = 4.5
Note how the output of the two statements is different. You can change the order of operation using parenthesis, just like in algebra.
As you can see, arithmetic in C++ works much like you would expect. However, there are some mysteries when working with integer variables which we will now explore:
- Truncation in integer division
- Modulus (%) operator
- Truncation in integer division: Change the data type of the two variables from
double
toint
, like this:int a = 7, b = 2;
- Compile
and run your program again and compare the output. Note how the result
of the division operation changed. What happened to the decimal part of
the result?
In programming terms, we say that the decimal part is truncated (cut short). You have to watch out for this in C++ programming or you may get unexpected results in your calculations.
- Modulus (%) operator:
Sometimes we want the integer remainder from an integer division. To
see the integer remainder, we use the modulus (%) operator. Add the
following statement to your program:
cout << "a % b = " << a % b << endl;
- Compile and run your program again with this added statement. Your program should now display the following output:
a + b = 9 a - b = 5 a * b = 14 a / b = 3 a + b / 2 = 8 (a + b) / 2 = 4 a % b = 1
- Mathematical functions: More complex mathematical operations require the use of a function in C++. one such function is
sqrt(number)
which calculates the square root of the number inside the parenthesis. Add the following statement to your program:cout << "sqrt(a + b) = " << sqrt(a + b) << endl;
- You
program will not compile with this new statement because you must
include a library of the mathematical functions. Add the statement:
#include <cmath>
to the top of your program like this:#include <iostream> #include <cmath> // math function library using namespace std;
- Compile
and run your program again with this added statement. Your program
should now compile and display the following output when run:
a + b = 9 a - b = 5 a * b = 14 a / b = 3 a + b / 2 = 8 (a + b) / 2 = 4 a % b = 1 sqrt(a + b) = 3
For more information on mathematical functions see the section below.
- Save your program source code that displays all eight (8) expressions and submit it to Catalyst.
Completed Program
Once you are finished, your source code should look like the following:
Wrap Up
- When finished, answer the questions from today's learning objectives
- With a partner, answer the following questions:
- What will be printed to the console when I run each of the lines of code below:
a1. int result = 7.0 / 2.0;
cout << result;
2. cout << 7 / 2.0;
3. cout << 7.0 / 2;
4. cout << 7 / 2;
5. cout << 7 % 2;
6. cout << 9 + 3 * 5 / 2 - 3;
Upcoming Assignments
- Assignment 5 due Monday at 9:20am
- Lab 3 due Friday at midnight on CodeLab Website
~See You Monday!~