Download presentation
Presentation is loading. Please wait.
1
1 Lecture 7 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington
2
2 Chapter 3 Topics Constants of Type int and float l Evaluating Arithmetic Expressions l Implicit Type Coercion and Explicit Type Conversion l Calling a Value-Returning Function l Using Function Arguments l Using C++ Library Functions in Expressions l Calling a Void Function l C++ Manipulators to Format Output String Operations length, find, substr
3
3 Revision of Lecture 6 l Value=Expression; When does automatic type coercion occur? l How do we explicitly convert 4.58 to integer? l F(x,y) = 5x-3y-2; What parameters are passed and what value is returned? l Can a subprogram (function except main) call another function?
4
4 Revision of Lecture 6 l If yes, why should we avoid it? l What are the rules for top down C++ program design? l void my_salary(tax); n How do we call this function in our program: l float tax(base_salary); n How do we call this function? l Why do we use Output manipulators?
5
5 Output Statements SYNTAX (revised) cout << ExpressionOrManipulator << ExpressionOrManipulator... ;
6
6 Using Manipulators Fixed and Showpoint l use the following statement 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 << fixed << showpoint ;
7
7 setprecision(n) l requires #include and appears in an expression using insertion operator (<<) l if fixed has already been specified, argument n determines the number of places displayed after the decimal point for floating point values l remains in effect until explicitly changed by another call to setprecision
8
8 What is exact output? #include // for setw( ) and setprecision( ) #include using namespace std; int main ( ) { float myNumber = 123.4587 ; cout << fixed << showpoint ; // use decimal format // print decimal points cout << “Number is ” << setprecision ( 3 ) << myNumber << endl ; return 0 ; }
9
9 OUTPUT Number is 123.459 value is rounded if necessary to be displayed with exactly 3 places after the decimal point
10
10 Manipulator setw l “set width” lets us control how many character positions the next data item should occupy when it is output l setw is only for formatting numbers and strings, not char type data
11
11 setw(n) l requires #include and appears in an expression using insertion operator (<<) l argument n is called the fieldwidth specification, and determines the number of character positions in which to display a right-justified number or string (not char data). The number of positions used is expanded if n is too narrow l “set width” affects only the very next item displayed, and is useful to align columns of output
12
12 What is exact output? #include // for setw( ) #include using namespace std; int main ( ) { int myNumber = 123 ; int yourNumber = 5 ; cout << setw ( 10 ) << “Mine” << setw ( 10 ) << “Yours” << endl; << setw ( 10 ) << myNumber << setw ( 10 ) << yourNumber << endl ; return 0 ; }
13
13 OUTPUT 12345678901234567890 Mine Yours 123 5 each is displayed right-justified and each is located in a total of 10 positions position
14
14 What is exact output? #include // for setw( ) and setprecision( ) #include using namespace std; int main ( ) { float myNumber = 123.4 ; float yourNumber = 3.14159 ; cout << fixed << showpoint ; // use decimal format // print decimal points cout << “Numbers are: ” << setprecision ( 4 ) << endl << setw ( 10 ) << myNumber << endl << setw ( 10 ) << yourNumber << endl ; return 0 ; }
15
15 OUTPUT Numbers are: 123.4000 3.1416 each is displayed right-justified and rounded if necessary and each is located in a total of 10 positions with 4 places after the decimal point 12345678901234567890
16
16 float x = 312.0 ; float y = 4.827 ; cout << fixed << showpoint ; OUTPUT cout << setprecision ( 2 ) << setw ( 10 ) << x << endl ’’’’ 3 1 2.00 << setw ( 10 ) << y << endl ; ’’’’’’ 4.83 cout << setprecision ( 1 ) << setw ( 10 ) << x << endl ’’’’’ 3 1 2.0 << setw ( 10 ) << y << endl ; ’’’’’’’ 4.8 cout << setprecision ( 5 ) << setw ( 7 ) << x << endl 3 1 2.00000 << setw ( 7 ) << y << endl ; 4.82700 More Examples x 312.0 y 4.827
17
17 HEADER MANIPULATOR ARGUMENT EFFECT FILE TYPE showpoint none displays decimal point fixed none suppresses scientific notation setprecision(n) int sets precision to n digits setw(n) int sets fieldwidth to n positions endl none terminates output line
18
18 length Function function length returns an unsigned integer value that equals the number of characters currently in the string function size returns the same value as function length you must use dot notation in the call to function length or size
19
19 find Function function find returns an unsigned integer value that is the beginning position for the first occurrence of a particular substring within the string the substring argument can be a string constant, a string expression, or a char value if the substring was not found, function find returns the special value string::npos
20
20 substr Function function substr returns a particular substring of a string l the first argument is an unsigned integer that specifies a starting position within the string l the second argument is an unsigned integer that specifies the length of the desired substring l positions of characters within a string are numbered starting from 0, not from 1
21
21 What is exact output? #include #include // for functions length, find, substr using namespace std; int main ( ) { string stateName = “Mississippi” ; cout << stateName.length( ) << endl; cout << stateName.find(“is”) << endl; cout << stateName.substr( 0, 4 ) << endl; cout << stateName.substr( 4, 2 ) << endl; cout << stateName.substr( 9, 5 ) << endl; return 0 ; }
22
22 What is exact output? #include #include // for functions length, find, substr using namespace std; int main ( ) { string stateName = “Mississippi” ; cout << stateName.length( ) << endl; // value 11 cout << stateName.find(“is”) << endl;// value 1 cout << stateName.substr( 0, 4 ) << endl;// value “Miss” cout << stateName.substr( 4, 2 ) << endl;// value “is” cout << stateName.substr( 9, 5 ) << endl;// value “pi” return 0 ; }
23
23 Map Measurement Case Study You want a program to determine walking distances between 4 sights in the city. Your city map legend says one inch on the map equals 1/4 mile in the city. You use the measured distances between 4 sights on the map. Display the walking distances (rounded to the nearest tenth) between each of the 4 sights.
24
24 // *************************************************** // Walk program // This program computes the mileage (rounded to nearest // tenth of mile) for each of 4 distances, given map // measurements on map with scale of 1 in = 0.25 mile // *************************************************** #include // for cout, endl #include // For setprecision using namespace std; float RoundToNearestTenth( float ); // declare function const float SCALE = 0.25; // Map scale (mi. per inch) C++ Program
25
25 C++ Code Continued const float DISTANCE1 = 1.5; // First map distance const float DISTANCE2 = 2.3; // Second map distance const float DISTANCE3 = 5.9; // Third map distance const float DISTANCE4 = 4.0; // Fourth map distance int main( ) { float totMiles; // Total of rounded miles float miles;// One rounded mileage cout << fixed << showpoint // Set output format << setprecision(1); totMiles = 0.0;// Initialize total miles
26
26 // Compute miles for each distance on map miles = RoundToNearestTenth( DISTANCE1 * SCALE ); cout << DISTANCE1 << “ inches on map is “ << miles << “ miles in city.” << endl; totMiles = totMiles + miles; miles = RoundToNearestTenth( DISTANCE2 * SCALE ); cout << DISTANCE2 << “ inches on map is “ << miles << “ miles in city.” << endl; totMiles = totMiles + miles;
27
27 // Compute miles for other distances on map miles = RoundToNearestTenth( DISTANCE3 * SCALE ); cout << DISTANCE3 << “ inches on map is “ << miles << “ miles in city.” << endl; totMiles = totMiles + miles; miles = RoundToNearestTenth( DISTANCE4 * SCALE ); cout << DISTANCE4 << “ inches on map is “ << miles << “ miles in city.” << endl; totMiles = totMiles + miles;
28
28 cout << endl << “Total walking mileage is “ << totMiles << “ miles.” << endl; return 0 ;// Successful completion } // *************************************************** float RoundToNearestTenth ( /* in */ float floatValue) // Function returns floatValue rounded to nearest tenth. { return float(int(floatValue * 10.0 + 0.5)) / 10.0; }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.