1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
1 Lecture 6 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
-
date post
19-Dec-2015 -
Category
Documents
-
view
218 -
download
0
Transcript of 1 Lecture 6 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
![Page 1: 1 Lecture 6 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.](https://reader037.fdocuments.net/reader037/viewer/2022110322/56649d3a5503460f94a14e73/html5/thumbnails/1.jpg)
1
Lecture 6
Chapter 3
Numeric Types, Expressions, and Output
Dale/Weems/Headington
![Page 2: 1 Lecture 6 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.](https://reader037.fdocuments.net/reader037/viewer/2022110322/56649d3a5503460f94a14e73/html5/thumbnails/2.jpg)
2
Chapter 3 Topics
Constants of Type int and float Evaluating Arithmetic Expressions Implicit Type Coercion and Explicit Type
Conversion Calling a Value-Returning Function Using Function Arguments Using C++ Library Functions in Expressions Calling a Void Function C++ Manipulators to Format Output String Operations length, find, substr
![Page 3: 1 Lecture 6 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.](https://reader037.fdocuments.net/reader037/viewer/2022110322/56649d3a5503460f94a14e73/html5/thumbnails/3.jpg)
3
Revising Lecture 5
Write a floating point value in scientific notation to represent 1 million
What is the result of this C++ expression ((12/5)*5) + (12%5)??
What is the output?– int k=23; while (++k<26) {cout<<k<<endl;}
What is the result? 8*25%3-4/5+6
![Page 4: 1 Lecture 6 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.](https://reader037.fdocuments.net/reader037/viewer/2022110322/56649d3a5503460f94a14e73/html5/thumbnails/4.jpg)
4
Variable = Expression
first, Expression on right is evaluated then the resulting value is stored in the
memory location of Variable on left
NOTE: An automatic type coercion occurs after evaluation but before the value is stored if the types differ for Expression and Variable
Assignment Operator Syntax
![Page 5: 1 Lecture 6 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.](https://reader037.fdocuments.net/reader037/viewer/2022110322/56649d3a5503460f94a14e73/html5/thumbnails/5.jpg)
5
What value is stored?
float a;
float b;
a = 8.5;
b = 9.37;
a = b;
a
b
a
b
8.5
9.37
?
?
![Page 6: 1 Lecture 6 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.](https://reader037.fdocuments.net/reader037/viewer/2022110322/56649d3a5503460f94a14e73/html5/thumbnails/6.jpg)
6
What is stored?
? float someFloat;
someFloat
someFloat = 12; // causes implicit type conversion
someFloat
12.0
![Page 7: 1 Lecture 6 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.](https://reader037.fdocuments.net/reader037/viewer/2022110322/56649d3a5503460f94a14e73/html5/thumbnails/7.jpg)
7
What is stored?
? int someInt;
someInt
someInt = 4.8; // causes implicit type conversion
someInt
4
![Page 8: 1 Lecture 6 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.](https://reader037.fdocuments.net/reader037/viewer/2022110322/56649d3a5503460f94a14e73/html5/thumbnails/8.jpg)
8
Type Casting is Explicit Conversion of Type
int(4.8) has value 4
float(5) has value 5.0
float(7/4) has value 1.0
float(7) / float(4) has value 1.75
![Page 9: 1 Lecture 6 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.](https://reader037.fdocuments.net/reader037/viewer/2022110322/56649d3a5503460f94a14e73/html5/thumbnails/9.jpg)
9
Some Expressionsint age;
EXAMPLE VALUE
age = 8 8
- age - 8
5 + 8 13
5 / 8 0
6.0 / 5.0 1.2
float ( 4 / 8 ) 0.0
float ( 4 ) / 8 0.5
cout << “How old are you?” cout
cin >> age cin
cout << age cout
![Page 10: 1 Lecture 6 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.](https://reader037.fdocuments.net/reader037/viewer/2022110322/56649d3a5503460f94a14e73/html5/thumbnails/10.jpg)
10
What values are stored?
float loCost;
float hiCost;
loCost = 12.342;
hiCost = 12.348;
loCost = float (int (loCost * 100.0 + 0.5) ) / 100.0;
hiCost = float (int (hiCost * 100.0 + 0.5) ) / 100.0;
![Page 11: 1 Lecture 6 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.](https://reader037.fdocuments.net/reader037/viewer/2022110322/56649d3a5503460f94a14e73/html5/thumbnails/11.jpg)
11
Values were rounded to 2 decimal places
12.34
hiCost
12.35
loCost
![Page 12: 1 Lecture 6 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.](https://reader037.fdocuments.net/reader037/viewer/2022110322/56649d3a5503460f94a14e73/html5/thumbnails/12.jpg)
12
Function Concept in Math
f ( x ) = 5 x - 3
When x = 1, f ( x ) = 2 is the returned value.
When x = 4, f ( x ) = 17 is the returned value.
Returned value is determined by the function definition and by the values of any parameters.
Name of function
Parameter of function
Function definition
![Page 13: 1 Lecture 6 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.](https://reader037.fdocuments.net/reader037/viewer/2022110322/56649d3a5503460f94a14e73/html5/thumbnails/13.jpg)
13
Functions
every C program must have a function called main
program execution always begins with function main
any other functions are subprograms and must be called
![Page 14: 1 Lecture 6 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.](https://reader037.fdocuments.net/reader037/viewer/2022110322/56649d3a5503460f94a14e73/html5/thumbnails/14.jpg)
14
Function Calls
one function calls another by using the name of the called function together with ( ) containing an argument list
a function call temporarily transfers control from the calling function to the called function
![Page 15: 1 Lecture 6 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.](https://reader037.fdocuments.net/reader037/viewer/2022110322/56649d3a5503460f94a14e73/html5/thumbnails/15.jpg)
15
What is in a block?
{
0 or more statements here
}
![Page 16: 1 Lecture 6 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.](https://reader037.fdocuments.net/reader037/viewer/2022110322/56649d3a5503460f94a14e73/html5/thumbnails/16.jpg)
16
Every C++ function has 2 parts
int main ( ) heading{
body block
return 0;
}
![Page 17: 1 Lecture 6 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.](https://reader037.fdocuments.net/reader037/viewer/2022110322/56649d3a5503460f94a14e73/html5/thumbnails/17.jpg)
17
Shortest C++ Program
int main ( )
{
return 0;
}
type of returned value name of function
![Page 18: 1 Lecture 6 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.](https://reader037.fdocuments.net/reader037/viewer/2022110322/56649d3a5503460f94a14e73/html5/thumbnails/18.jpg)
18
What is in a heading?
int main ( )
type of returned valuename of function
says no parameters
![Page 19: 1 Lecture 6 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.](https://reader037.fdocuments.net/reader037/viewer/2022110322/56649d3a5503460f94a14e73/html5/thumbnails/19.jpg)
19
More About Functions
it is not considered good practice for the body block of function main to be long
function calls are used to do tasks
every C++ function has a return type
if the return type is not void, the function returns a value to the calling block
![Page 20: 1 Lecture 6 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.](https://reader037.fdocuments.net/reader037/viewer/2022110322/56649d3a5503460f94a14e73/html5/thumbnails/20.jpg)
20
Where are functions?
located in libraries
OR
written by programmers
![Page 21: 1 Lecture 6 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.](https://reader037.fdocuments.net/reader037/viewer/2022110322/56649d3a5503460f94a14e73/html5/thumbnails/21.jpg)
21
HEADER FILE FUNCTION EXAMPLE VALUE OF CALL
fabs(x) fabs(-6.4) 6.4
<cmath> pow(x,y) pow(2.0,3.0) 8.0
<cmath> sqrt(x) sqrt(100.0) 10.0
<iomanip> setprecision(n) setprecision(3)
<cmath> log(x) log(2.0) .693147
sqrt(x) sqrt(2.0) 1.41421
<cstdlib> abs(i) abs(-6) 6
![Page 22: 1 Lecture 6 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.](https://reader037.fdocuments.net/reader037/viewer/2022110322/56649d3a5503460f94a14e73/html5/thumbnails/22.jpg)
22
Write C++ Expressions for
The square root of b2 - 4ac
sqrt ( b * b - 4.0 * a * c )
The square root of the average of myAge and yourAge
sqrt ( ( myAge + yourAge ) / 2 )
![Page 23: 1 Lecture 6 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.](https://reader037.fdocuments.net/reader037/viewer/2022110322/56649d3a5503460f94a14e73/html5/thumbnails/23.jpg)
23
Program with Several Functions
main function
Square function
Cube function
![Page 24: 1 Lecture 6 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.](https://reader037.fdocuments.net/reader037/viewer/2022110322/56649d3a5503460f94a14e73/html5/thumbnails/24.jpg)
24
Program with Three Functions
#include <iostream>
int Square( int ); // declares these functionsint Cube( int );
using namespace std ;
int main( ){ cout << “The square of 27 is “
<< Square(27) << endl; // function call
cout << “The cube of 27 is “ << Cube(27) << endl; // function call
return 0;}
![Page 25: 1 Lecture 6 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.](https://reader037.fdocuments.net/reader037/viewer/2022110322/56649d3a5503460f94a14e73/html5/thumbnails/25.jpg)
25
Rest of Program
int Square( int n ) // header and body here{ return n * n;}
int Cube( int n ) // header and body here{ return n * n * n;}
![Page 26: 1 Lecture 6 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.](https://reader037.fdocuments.net/reader037/viewer/2022110322/56649d3a5503460f94a14e73/html5/thumbnails/26.jpg)
26
Function Call
a function call temporarily transfers control to the called function’s code
when the function’s code has finished executing, control is transferred back to the calling block
![Page 27: 1 Lecture 6 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.](https://reader037.fdocuments.net/reader037/viewer/2022110322/56649d3a5503460f94a14e73/html5/thumbnails/27.jpg)
27
FunctionName = ( Argument List )
The argument list is a way for functions to communicate with each other by passing information.
The argument list can contain 0, 1, or more arguments, separated by commas, depending
on the function.
Function Call Syntax
![Page 28: 1 Lecture 6 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.](https://reader037.fdocuments.net/reader037/viewer/2022110322/56649d3a5503460f94a14e73/html5/thumbnails/28.jpg)
28
A void function call stands alone
#include <iostream>
void DisplayMessage ( int n ) ; // declares function
int main( ){ DisplayMessage( 15 ) ; //function call cout << “Good Bye“ << endl ;
return 0 ;}
![Page 29: 1 Lecture 6 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.](https://reader037.fdocuments.net/reader037/viewer/2022110322/56649d3a5503460f94a14e73/html5/thumbnails/29.jpg)
29
A void function does NOT return a value
// header and body here
void DisplayMessage ( int n ) { cout << “I have liked math for “ << n << “ years” << endl ;}
![Page 30: 1 Lecture 6 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.](https://reader037.fdocuments.net/reader037/viewer/2022110322/56649d3a5503460f94a14e73/html5/thumbnails/30.jpg)
Two Kinds of Functions
Always returns a single value to its caller and is called from within an expression.
Never returns a value to its caller, and is called as a separate statement.
Value-Returning Void
![Page 31: 1 Lecture 6 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.](https://reader037.fdocuments.net/reader037/viewer/2022110322/56649d3a5503460f94a14e73/html5/thumbnails/31.jpg)
31
<< is a binary operator
<< is called the output or insertion operator
<< is left associative
EXPRESSION HAS VALUE
cout << age cout
STATEMENT
cout << “You are “ << age << “ years old\n” ;
![Page 32: 1 Lecture 6 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.](https://reader037.fdocuments.net/reader037/viewer/2022110322/56649d3a5503460f94a14e73/html5/thumbnails/32.jpg)
32
<iostream> is header file
for a library that defines 3 objects
an istream object named cin (keyboard)
an ostream object named cout (screen)
an ostream object named cerr (screen)
![Page 33: 1 Lecture 6 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.](https://reader037.fdocuments.net/reader037/viewer/2022110322/56649d3a5503460f94a14e73/html5/thumbnails/33.jpg)
33
No I/O is built into C++
instead, a library provides input stream and output stream
Keyboard Screenexecutingprogram
istream ostream
![Page 34: 1 Lecture 6 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.](https://reader037.fdocuments.net/reader037/viewer/2022110322/56649d3a5503460f94a14e73/html5/thumbnails/34.jpg)
34
Manipulators
manipulators are used only in input and output statements
endl, fixed, showpoint, setw, and setprecision are manipulators that can be used to control output format
endl is use to terminate the current output line, and create blank lines in output
![Page 35: 1 Lecture 6 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.](https://reader037.fdocuments.net/reader037/viewer/2022110322/56649d3a5503460f94a14e73/html5/thumbnails/35.jpg)
35
Insertion Operator ( << )
the insertion operator << takes 2 operands
the left operand is a stream expression, such as cout
the right operand is an expression of simple type, or a string, or a manipulator
![Page 36: 1 Lecture 6 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.](https://reader037.fdocuments.net/reader037/viewer/2022110322/56649d3a5503460f94a14e73/html5/thumbnails/36.jpg)
36
Output Statements
SYNTAX (revised)
cout << ExpressionOrManipulator
<< ExpressionOrManipulator . . . ;
![Page 37: 1 Lecture 6 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.](https://reader037.fdocuments.net/reader037/viewer/2022110322/56649d3a5503460f94a14e73/html5/thumbnails/37.jpg)
37
Output Statements
SYNTAX
These examples yield the same output.
cout << “The answer is “ ;
cout << 3 * 4 ;
cout << “The answer is “ << 3 * 4 ;
cout << Expression << Expression . . . ;