Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.

Similar presentations


Presentation on theme: "1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington."— Presentation transcript:

1 1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington

2 2 C++ Simple Data Types simple types integralfloating char short int long bool enum float double long double unsigned

3 3 Some C++ Operators Precedence OperatorDescription Higher ( )Function call +Positive - Negative *Multiplication / Division % Modulus (remainder) +Addition - Subtraction Lower = Assignment

4 4 Associativity l left to right Associativity means that in an expression having 2 operators with the same priority, the left operator is applied first l in C++ the binary operators *, /, %, +, - are all left associative l expression 9 - 5 - 1 means ( 9 - 5 ) - 1 4 - 1 3

5 5 7 * 10 - 5 % 3 * 4 + 9 means (7 * 10) - 5 % 3 * 4 + 9 70 - 5 % 3 * 4 + 9 70 - (5 % 3) * 4 + 9 70 - 2 * 4 + 9 70 - ( 2 * 4 ) + 9 70 - 8 + 9 ( 70 - 8 ) + 9 62 + 9 71 Evaluate the Expression

6 6 What value is stored? float a; float b; a = 8.5; b = 9.37; a = b; a b a b 8.5 9.37 ? ?

7 7 What is stored? ? float someFloat; someFloat someFloat = 12; // causes implicit type conversion someFloat 12.0

8 8 What is stored? ? int someInt; someInt someInt = 4.8; // causes implicit type conversion someInt 4

9 9 Type Casting

10 10 Type Casting is Explicit Conversion of Type int(4.8) has value4 float(5)has value5.0 float(7/4)has value1.0 float(7) / float(4)has value1.75

11 11 Some Expressions int age; EXAMPLEVALUE age = 8 8 - age- 8 5 + 813 5 / 8 0 6.0 / 5.01.2 float ( 4 / 8 )0.0 float ( 4 ) / 80.5 cout << “How old are you?” cout cin >> agecin cout << agecout

12 12 Every C++ function has 2 parts int main ( ) heading { body block return 0; }

13 13 HEADER FILE FUNCTION EXAMPLE VALUE OF CALL fabs(x) fabs(-6.4) 6.4 pow(x,y) pow(2.0,3.0) 8.0 sqrt(x) sqrt(100.0) 10.0 setprecision(n) setprecision(3) log(x) n.log log(2.0).693147 sqrt(x) sqrt(2.0) 1.41421 abs(i) abs(-6) 6

14 14 Write C++ Expressions for The square root of b 2 - 4ac sqrt ( b * b - 4.0 * a * c ) The square root of the average of myAge and yourAge sqrt ( ( myAge + yourAge ) / 2 )

15 15 << is a binary operator << is called the output or insertion operator << is left associative EXPRESSIONHAS VALUE cout << age cout STATEMENT cout << “You are “ << age << “ years old\n” ;

16 16 Insertion Operator ( << ) l the insertion operator << takes 2 operands l the left operand is a stream expression, such as cout l the right operand is an expression of simple type, or a string, or a manipulator

17 17 I/O Manipulators

18 18 Manipulators l manipulators are used only in input and output statements l endl, fixed, showpoint, setw, and setprecision are manipulators that can be used to control output format l endl is use to terminate the current output line, and create blank lines in output

19 19 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) l cout << fixed << showpoint;

20 20 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

21 21 What is exact output? #include // for setw( ) and setprecision( ) #include using namespace std; int main ( ) { float myNumber = 123.4587 ; cout << fixed; // use decimal format cout << showpoint; // print decimal points cout << “Number is ” << setprecision ( 3 ) << myNumber << endl ; return 0 ; }

22 22 OUTPUT Number is 123.459 value is rounded if necessary to be displayed with exactly 3 places after the decimal point

23 23 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

24 24 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

25 25 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 ; }

26 26 OUTPUT 12345678901234567890 Mine Yours 123 5 each is displayed right-justified and each is located in a total of 10 positions position

27 27 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; // use decimal format cout << showpoint; // print decimal points cout << “Numbers are: ” << setprecision ( 4 ) << endl << setw ( 10 ) << myNumber << endl << setw ( 10 ) << yourNumber << endl ; return 0 ; }

28 28 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

29 29 float x = 312.0 ; float y = 4.827 ; cout << fixed; cout << showpoint; 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

30 30 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

31 31 setw Manipulator cout << setw(5) << someInt; l controls # columns next data item occupies l for numbers, strings, not char l the # columns is called the FIELD l output right justified

32 32 setw Manipulator int ans = 33, num = 7132; cout << setw(4) << ans << setw(5) << num << setw(4) << "Hi";__33_7132__Hi 12 cout << setw(2) << ans << setw(4) << num << setw(2) << "Hi";337132Hi cout << setw(6) << ans << setw(3) << "Hi" << setw(5) << num;____33_Hi_7132 1234

33 33 setw Manipulator int ans = 33, num = 7132; cout << setw(7) << "Hi" << setw(4) << num;_____Hi7132 12345 cout << setw(1) << ans << setw(5) << num;33_7132 cout << "Hi" << setw(5) << ans << num;Hi___337132 123

34 34 setw With float Values float x = 4.85; cout << setw(4) << x << endl4.85 << setw(6) << x << endl 4.85 << setw(3) << x << endl;4.85 cout << 123456789.5;1.234567E+08 cout << 95.0; 95 l Fixes the last two problems: cout << fixed << showpoint;

35 35 String Functions

36 36 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

37 37 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

38 38 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

39 39 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 ; }

40 40 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 int p=0; int len=4; cout << stateName.substr( p,len ) << endl;// value “Miss” cout << stateName.substr( 4, 2 ) << endl;// value “is” cout << stateName.substr( 9, 5 ) << endl;// value “pi” return 0 ; }

41 41 getline( ) Function l Because the extraction operator stops reading at the first trailing whitespace, >> cannot be used to input a string with blanks in it l use getline function with 2 arguments to overcome this obstacle l First argument is an input stream variable, and second argument is a string variable EXAMPLE string message ; getline (cin, message ) ;

42 42


Download ppt "1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington."

Similar presentations


Ads by Google