Programming in C++
-
Upload
fiona-rios -
Category
Documents
-
view
31 -
download
0
description
Transcript of Programming in C++
![Page 1: Programming in C++](https://reader035.fdocuments.us/reader035/viewer/2022062517/56812e95550346895d943b3c/html5/thumbnails/1.jpg)
1
Programming in C++
Chapter 3
Arithmetic Expressions, Function Calls, and Output
![Page 2: Programming in C++](https://reader035.fdocuments.us/reader035/viewer/2022062517/56812e95550346895d943b3c/html5/thumbnails/2.jpg)
2
What is an Expression in C++?
An expression is any valid combination of operators and operands.
In C++ each expression has a value.
![Page 3: Programming in C++](https://reader035.fdocuments.us/reader035/viewer/2022062517/56812e95550346895d943b3c/html5/thumbnails/3.jpg)
3
Operators can be
binary involving 2 operands 2 + 3
unary involving 1 operand - 3
ternary involving 3 operands later
![Page 4: Programming in C++](https://reader035.fdocuments.us/reader035/viewer/2022062517/56812e95550346895d943b3c/html5/thumbnails/4.jpg)
4
Some C++ Operators
Precedence Operator Description Higher ( ) Function call
+ Positive
- Negative
* Multiplication
/ Division % Modulus
(remainder)
+ Addition
- Subtraction
Lower = Assignment
![Page 5: Programming in C++](https://reader035.fdocuments.us/reader035/viewer/2022062517/56812e95550346895d943b3c/html5/thumbnails/5.jpg)
5
Precedence
Higher Precedence determines which operator is applied first in an expression having several operators.
![Page 6: Programming in C++](https://reader035.fdocuments.us/reader035/viewer/2022062517/56812e95550346895d943b3c/html5/thumbnails/6.jpg)
6
Associativity
Left to right Associativity means that in an expression having 2 operators with the same priority, the left operator is applied first.
In C++ the binary operators
* , / , % , + , - are all left associative.
Expression 9 - 5 - 1 means ( 9 - 5 ) - 1
4 - 1
3
![Page 7: Programming in C++](https://reader035.fdocuments.us/reader035/viewer/2022062517/56812e95550346895d943b3c/html5/thumbnails/7.jpg)
7
7 * 10 - 5 % 3 * 4 + 9means (7 * 10) - 5 % 3 * 4 + 9
70 - 5 % 3 * 4 + 970 - (5 % 3) * 4 + 9 70 - 2 * 4 + 9 70 - ( 2 * 4 ) + 9 70 - 8 + 9 ( 70 - 8 ) + 9
62 + 9 71
Evaluate the Expression
![Page 8: Programming in C++](https://reader035.fdocuments.us/reader035/viewer/2022062517/56812e95550346895d943b3c/html5/thumbnails/8.jpg)
8
Parentheses Parentheses can be used to change the
usual order Parts in ( ) are evaluated first Evaluate (7 * (10 - 5) % 3) * 4 + 9
( 7 * 5 % 3 ) * 4 + 9
( 35 % 3 ) * 4 + 9 2 * 4 + 9
8 + 9 17
![Page 9: Programming in C++](https://reader035.fdocuments.us/reader035/viewer/2022062517/56812e95550346895d943b3c/html5/thumbnails/9.jpg)
9
VariableName = Expression
First, Expression on right is evaluated.
Then the resulting value is stored in the memory location of VariableName on left.
NOTE: An automatic type coercion occurs after evaluation but before the value is stored if the types differ for Expression and VariableName
Assignment Operator Syntax
![Page 10: Programming in C++](https://reader035.fdocuments.us/reader035/viewer/2022062517/56812e95550346895d943b3c/html5/thumbnails/10.jpg)
10
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 11: Programming in C++](https://reader035.fdocuments.us/reader035/viewer/2022062517/56812e95550346895d943b3c/html5/thumbnails/11.jpg)
11
What is stored?
?float SomeFloat;
SomeFloat
SomeFloat = 12; // causes implicit type conversion
SomeFloat
12.0
![Page 12: Programming in C++](https://reader035.fdocuments.us/reader035/viewer/2022062517/56812e95550346895d943b3c/html5/thumbnails/12.jpg)
12
What is stored?
?int SomeInt;
SomeInt
SomeInt = 4.8; // causes implicit type conversion
SomeInt
4
![Page 13: Programming in C++](https://reader035.fdocuments.us/reader035/viewer/2022062517/56812e95550346895d943b3c/html5/thumbnails/13.jpg)
13
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 14: Programming in C++](https://reader035.fdocuments.us/reader035/viewer/2022062517/56812e95550346895d943b3c/html5/thumbnails/14.jpg)
14
<iostream.h> 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 15: Programming in C++](https://reader035.fdocuments.us/reader035/viewer/2022062517/56812e95550346895d943b3c/html5/thumbnails/15.jpg)
15
No I/O is built into C++
Instead, a library provides input stream and output stream
Keyboard Screenexecutingprogram
istream ostream
![Page 16: Programming in C++](https://reader035.fdocuments.us/reader035/viewer/2022062517/56812e95550346895d943b3c/html5/thumbnails/16.jpg)
16
>> is a binary operator
>> is called the input or extraction operator
>> is left associative
EXPRESSION HAS VALUE
cin >> Age cin
STATEMENT
cin >> Age >> Weight ;
![Page 17: Programming in C++](https://reader035.fdocuments.us/reader035/viewer/2022062517/56812e95550346895d943b3c/html5/thumbnails/17.jpg)
17
<< 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 18: Programming in C++](https://reader035.fdocuments.us/reader035/viewer/2022062517/56812e95550346895d943b3c/html5/thumbnails/18.jpg)
18
Some Expressionsint Age;
EXAMPLE VALUE
Age = 8 8- Age - 85 + 8 135 / 8 06.0 / 5.0 1.2float ( 4 / 8 ) 0.0float ( 4 ) / 8 0. 5cout << “How old are you?” coutcin >> Age cincout << Age cout
![Page 19: Programming in C++](https://reader035.fdocuments.us/reader035/viewer/2022062517/56812e95550346895d943b3c/html5/thumbnails/19.jpg)
19
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 20: Programming in C++](https://reader035.fdocuments.us/reader035/viewer/2022062517/56812e95550346895d943b3c/html5/thumbnails/20.jpg)
20
Values were rounded to 2 decimal places
12.34
LoCost
HiCost
12.35
![Page 21: Programming in C++](https://reader035.fdocuments.us/reader035/viewer/2022062517/56812e95550346895d943b3c/html5/thumbnails/21.jpg)
21
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 22: Programming in C++](https://reader035.fdocuments.us/reader035/viewer/2022062517/56812e95550346895d943b3c/html5/thumbnails/22.jpg)
22
What is in a block?
{
0 or more statements here
}
![Page 23: Programming in C++](https://reader035.fdocuments.us/reader035/viewer/2022062517/56812e95550346895d943b3c/html5/thumbnails/23.jpg)
23
Function Calls
One function calls another by using the name of the called function together with ( ) containing a parameter list
A function call temporarily transfers control from the calling function to the called function
![Page 24: Programming in C++](https://reader035.fdocuments.us/reader035/viewer/2022062517/56812e95550346895d943b3c/html5/thumbnails/24.jpg)
24
Parts of a Function
Every function has 2 parts
int main (void) heading{
body block return 0;
}
![Page 25: Programming in C++](https://reader035.fdocuments.us/reader035/viewer/2022062517/56812e95550346895d943b3c/html5/thumbnails/25.jpg)
25
What is in a heading?
int main (void)
type of returned valuename of function
says no parameters
![Page 26: Programming in C++](https://reader035.fdocuments.us/reader035/viewer/2022062517/56812e95550346895d943b3c/html5/thumbnails/26.jpg)
26
More about functions
It is not considered good practice for the body block of 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 27: Programming in C++](https://reader035.fdocuments.us/reader035/viewer/2022062517/56812e95550346895d943b3c/html5/thumbnails/27.jpg)
27
Where are functions?
located in libraries
OR
written by programmers
![Page 28: Programming in C++](https://reader035.fdocuments.us/reader035/viewer/2022062517/56812e95550346895d943b3c/html5/thumbnails/28.jpg)
28
HEADER FILE FUNCTION EXAMPLE VALUE OF CALL
fabs(x) fabs(-6.4) 6.4
<math.h> pow(x,y) pow(2.0,3.0) 8.0
<math.h> sqrt(x) sqrt(100.0) 10.0
<iomanip.h> setprecision(n) setprecision(3)
<math.h> log(x) log(2.0) .693147
sqrt(x) sqrt(2.0) 1.41421
<stdlib.h> abs(i) abs(-6) 6
![Page 29: Programming in C++](https://reader035.fdocuments.us/reader035/viewer/2022062517/56812e95550346895d943b3c/html5/thumbnails/29.jpg)
29
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 30: Programming in C++](https://reader035.fdocuments.us/reader035/viewer/2022062517/56812e95550346895d943b3c/html5/thumbnails/30.jpg)
30
Manipulators
Manipulators are used only in input and output statements.
endl, 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 31: Programming in C++](https://reader035.fdocuments.us/reader035/viewer/2022062517/56812e95550346895d943b3c/html5/thumbnails/31.jpg)
31
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 32: Programming in C++](https://reader035.fdocuments.us/reader035/viewer/2022062517/56812e95550346895d943b3c/html5/thumbnails/32.jpg)
32
Output Statements
SYNTAX (revised)
cout << ExprOrStringOrManipulator << ExprOrStringOrManipulator . . . ;
![Page 33: Programming in C++](https://reader035.fdocuments.us/reader035/viewer/2022062517/56812e95550346895d943b3c/html5/thumbnails/33.jpg)
33
setprecision(n)
requires #include <iomanip.h> and appears in an expression using insertion operator (<<)
specifies n as the number of places displayed after the decimal point for floating point values
remains in effect until explicitly changed by another call to setprecision
![Page 34: Programming in C++](https://reader035.fdocuments.us/reader035/viewer/2022062517/56812e95550346895d943b3c/html5/thumbnails/34.jpg)
34
What is exact output?
#include <iomanip.h>#include <iostream.h>
int main ( void){ float myNumber = 123.4587 ;
cout.setf ( ios::fixed , ios::floatfield ) ; // use decimal format cout.setf ( ios::showpoint ) ; // print decimal point
cout << “Number is ” << setprecision ( 3 ) << myNumber << endl ;
return 0 ;}
![Page 35: Programming in C++](https://reader035.fdocuments.us/reader035/viewer/2022062517/56812e95550346895d943b3c/html5/thumbnails/35.jpg)
35
OUTPUT
Number is 123.459
value is rounded if necessary to be displayed with exactly 3 places after the decimal point
![Page 36: Programming in C++](https://reader035.fdocuments.us/reader035/viewer/2022062517/56812e95550346895d943b3c/html5/thumbnails/36.jpg)
36
To Remember
To cause your program to output numbers that have a decimal point to a certain number of decimal places» cout.setf(ios::fixed);» cout.setf(ios::showpoint);» cout.precision(2);
Any output after these statements will have the precision as indicated
![Page 37: Programming in C++](https://reader035.fdocuments.us/reader035/viewer/2022062517/56812e95550346895d943b3c/html5/thumbnails/37.jpg)
37
setw(n) requires #include <iomanip.h> and appears in
an expression using insertion operator (<<)
affects only the very next item displayed
“set width” specifies n as the number of total columns in which to display a number or string (not char data). Parameter n is called the fieldwidth specification. The number of columns used is expanded if n is too narrow.
is useful to align columns of output
![Page 38: Programming in C++](https://reader035.fdocuments.us/reader035/viewer/2022062517/56812e95550346895d943b3c/html5/thumbnails/38.jpg)
38
What is exact output?
#include <iomanip.h>#include <iostream.h>int main ( void){ float myNumber = 123.4 ; float yourNumber = 3.14159 ;
cout.setf ( ios::fixed , ios::floatfield ) ; // use decimal format cout.setf ( ios::showpoint ) ; // print decimal point
cout << “Numbers are: ” << setprecision ( 4 ) << endl << setw ( 10 ) << myNumber << endl
<< setw ( 10 ) << yourNumber << endl ;
return 0 ;}
![Page 39: Programming in C++](https://reader035.fdocuments.us/reader035/viewer/2022062517/56812e95550346895d943b3c/html5/thumbnails/39.jpg)
39
OUTPUT
Numbers are: 123.4000 3.1416
each is displayed right-justified androunded if necessary and each islocated in a total of 10 columns with 4 places after the decimal point
![Page 40: Programming in C++](https://reader035.fdocuments.us/reader035/viewer/2022062517/56812e95550346895d943b3c/html5/thumbnails/40.jpg)
40
Using setf( )
setf( ) is a function associated with output streams. To call this function use this syntax
DesiredOutputStream.setf( ParameterList ) ;
setf( ) can be used to print the decimal point (even if there are trailing zeros) for floating values that are output, and to specify that the value be printed with a fixed position decimal point (rather than in scientific notation).
![Page 41: Programming in C++](https://reader035.fdocuments.us/reader035/viewer/2022062517/56812e95550346895d943b3c/html5/thumbnails/41.jpg)
41
cout.setf( ) statements
Use the following statements to specify that (for output sent to the cout stream) decimal format (not scientific notation) be used, and that a decimal point be included (even for floating values with 0 as fractional part).
cout.setf( ios :: fixed, ios :: floatfield ) ;
cout.setf( ios :: showpoint ) ;
![Page 42: Programming in C++](https://reader035.fdocuments.us/reader035/viewer/2022062517/56812e95550346895d943b3c/html5/thumbnails/42.jpg)
42
More examples
float x = 312.0 ; float y = 4.827 ; cout.setf ( ios::fixed , ios::floatfield ) ; cout.setf ( ios::showpoint ) ; OUTPUT
cout << setprecision ( 2 ) << setw ( 10 ) << x << endl ’’’’312.00
<< setw ( 10 ) << y << endl ; ’’’’’’4.83
cout << setprecision ( 1 ) << setw ( 10 ) << x << endl ’’’’’312.0
<< setw ( 10 ) << y << endl ; ’’’’’’’4.8
cout << setprecision ( 5 ) << setw ( 7 ) << x << endl 312.00000
<< setw ( 7 ) << y << endl ; 4.82700
x
312.0
y
4.827
![Page 43: Programming in C++](https://reader035.fdocuments.us/reader035/viewer/2022062517/56812e95550346895d943b3c/html5/thumbnails/43.jpg)
43
Program with several functions
main( ) function
Square( ) function
Cube( ) function
![Page 44: Programming in C++](https://reader035.fdocuments.us/reader035/viewer/2022062517/56812e95550346895d943b3c/html5/thumbnails/44.jpg)
44
Value-returning functions
#include <iostream.h>
int Square ( int ) ; // declares these 2 functionsint Cube ( int ) ;
int main ( void ){ 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 45: Programming in C++](https://reader035.fdocuments.us/reader035/viewer/2022062517/56812e95550346895d943b3c/html5/thumbnails/45.jpg)
45
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 46: Programming in C++](https://reader035.fdocuments.us/reader035/viewer/2022062517/56812e95550346895d943b3c/html5/thumbnails/46.jpg)
46
A void function call stands alone
#include <iostream.h>
void DisplayMessage ( int n ) ; // declares function
int main(void){ DisplayMessage( 15 ) ; //function call cout << “Good Bye“ << endl ;
return 0 ;}
![Page 47: Programming in C++](https://reader035.fdocuments.us/reader035/viewer/2022062517/56812e95550346895d943b3c/html5/thumbnails/47.jpg)
47
A void function does NOT return a value
void DisplayMessage ( int n ) // header and body here{ cout << “I have liked math for “ << n << “ years” << endl ;}