Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 1 Lecture 6 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 Revising Lecture 5 l Write a floating point value in scientific notation to represent 1 million l What is the result of this C++ expression n ((12/5)*5) + (12%5)?? l What is the output? –int k=23; while (++k<26) {cout<<k<<endl;} l What is the result? n 8*25%3-4/5+6

4 4 Variable = Expression l first, Expression on right is evaluated l 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

5 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 ? ?

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

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

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

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

10 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;

11 11 Values were rounded to 2 decimal places 12.34 hiCost 12.35 loCost

12 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

13 13 Functions l every C program must have a function called main l program execution always begins with function main l any other functions are subprograms and must be called

14 14 Function Calls l one function calls another by using the name of the called function together with ( ) containing an argument list l a function call temporarily transfers control from the calling function to the called function

15 15 What is in a block? { 0 or more statements here }

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

17 17 Shortest C++ Program int main ( ) { return 0; } type of returned value name of function

18 18 What is in a heading? int main ( ) type of returned value name of function says no parameters

19 19 More About Functions l it is not considered good practice for the body block of function main to be long l function calls are used to do tasks l every C++ function has a return type l if the return type is not void, the function returns a value to the calling block

20 20 Where are functions? located in libraries OR written by programmers

21 21 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) log(2.0).693147 sqrt(x) sqrt(2.0) 1.41421 abs(i) abs(-6) 6

22 22 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 )

23 23 Program with Several Functions main function Square function Cube function

24 24 Program with Three Functions #include int Square( int ); // declares these functions int 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; }

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

26 26 Function Call l a function call temporarily transfers control to the called function’s code l when the function’s code has finished executing, control is transferred back to the calling block

27 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

28 28 A void function call stands alone #include void DisplayMessage ( int n ) ; // declares function int main( ) { DisplayMessage( 15 ) ; //function call cout << “Good Bye“ << endl ; return 0 ; }

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

30 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

31 31 << 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” ;

32 32 is header file l 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)

33 33 No I/O is built into C++ l instead, a library provides input stream and output stream KeyboardScreen executing program istreamostream

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

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

36 36 Output Statements SYNTAX (revised) cout << ExpressionOrManipulator << ExpressionOrManipulator... ;

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


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

Similar presentations


Ads by Google