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

Post on 19-Dec-2015

214 views 0 download

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

1

Lecture 6

Chapter 3

Numeric Types, Expressions, and Output

Dale/Weems/Headington

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

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

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

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

What is stored?

? float someFloat;

someFloat

someFloat = 12; // causes implicit type conversion

someFloat

12.0

7

What is stored?

? int someInt;

someInt

someInt = 4.8; // causes implicit type conversion

someInt

4

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

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

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

Values were rounded to 2 decimal places

12.34

hiCost

12.35

loCost

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

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

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

15

What is in a block?

{

0 or more statements here

}

16

Every C++ function has 2 parts

int main ( ) heading{

body block

return 0;

}

17

Shortest C++ Program

int main ( )

{

return 0;

}

type of returned value name of function

18

What is in a heading?

int main ( )

type of returned valuename of function

says no parameters

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

20

Where are functions?

located in libraries

OR

written by programmers

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

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 )

23

Program with Several Functions

main function

Square function

Cube function

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

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

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

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

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

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

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

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

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)

33

No I/O is built into C++

instead, a library provides input stream and output stream

Keyboard Screenexecutingprogram

istream ostream

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

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

36

Output Statements

SYNTAX (revised)

cout << ExpressionOrManipulator

<< ExpressionOrManipulator . . . ;

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