fig02_07.cpp (1 of 2)

43
2003 Prentice Hall, Inc. All rights Outline 1 fig02_07.cpp (1 of 2) 1 // Fig. 2.7: fig02_07.cpp 2 // Class average program with counter-controlled repetition. 3 #include <iostream> 4 5 using namespace std; 6 9 // function main begins program execution 10 int main() 11 { 12 int total; // sum of grades input by user 13 int gradeCounter; // number of grade to be entered next 14 int grade; // grade value 15 int average; // average of grades 16 17 // initialization phase 18 total = 0; // initialize total 19 gradeCounter = 1; // initialize loop counter 20

Upload: emerson-carlson

Post on 01-Jan-2016

34 views

Category:

Documents


0 download

DESCRIPTION

1 // Fig. 2.7: fig02_07.cpp 2 // Class average program with counter-controlled repetition. 3 #include 4 5 using namespace std; 6 9 // function main begins program execution 10 int main() 11 { - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: fig02_07.cpp (1 of 2)

2003 Prentice Hall, Inc.All rights reserved.

Outline1

fig02_07.cpp(1 of 2)

1 // Fig. 2.7: fig02_07.cpp2 // Class average program with counter-controlled repetition.3 #include <iostream>4 5 using namespace std;6 9 // function main begins program execution10 int main()11 {12 int total; // sum of grades input by user13 int gradeCounter; // number of grade to be entered next14 int grade; // grade value15 int average; // average of grades16 17 // initialization phase18 total = 0; // initialize total19 gradeCounter = 1; // initialize loop counter20

Page 2: fig02_07.cpp (1 of 2)

2003 Prentice Hall, Inc.All rights reserved.

Outline2

fig02_07.cpp(2 of 2)

fig02_07.cppoutput (1 of 1)

21 // processing phase22 while ( gradeCounter <= 10 ) { // loop 10 times23 cout << "Enter grade: "; // prompt for input24 cin >> grade; // read grade from user25 total = total + grade; // add grade to total26 gradeCounter = gradeCounter + 1; // increment counter27 }28 29 // termination phase30 average = total / 10; // integer division31 32 // display result33 cout << "Class average is " << average << endl; 34 35 return 0; // indicate program ended successfully36 37 } // end function main

Enter grade: 98

Enter grade: 76

Enter grade: 71

Enter grade: 87

Enter grade: 83

Enter grade: 90

Enter grade: 57

Enter grade: 79

Enter grade: 82

Enter grade: 94

Class average is 81

The counter gets incremented each time the loop executes. Eventually, the counter causes the loop to end.

Page 3: fig02_07.cpp (1 of 2)

2003 Prentice Hall, Inc.All rights reserved.

Outline3

fig02_09.cpp(1 of 3)

1 // Fig. 2.9: fig02_09.cpp2 // Class average program with sentinel-controlled repetition.3 #include <iostream>4 5 using namespace std;6 10 #include <iomanip> // parameterized stream manipulators11 12 using std::setprecision; // sets numeric output precision 13 14 // function main begins program execution15 int main()16 {17 int total; // sum of grades18 int gradeCounter; // number of grades entered19 int grade; // grade value20 21 double average; // number with decimal point for average22 23 // initialization phase24 total = 0; // initialize total25 gradeCounter = 0; // initialize loop counter

Data type double used to represent decimal numbers.

Page 4: fig02_07.cpp (1 of 2)

2003 Prentice Hall, Inc.All rights reserved.

Outline4

fig02_09.cpp(2 of 3)

26 27 // processing phase28 // get first grade from user29 cout << "Enter grade, -1 to end: "; // prompt for input30 cin >> grade; // read grade from user31 32 // loop until sentinel value read from user33 while ( grade != -1 ) { 34 total = total + grade; // add grade to total35 gradeCounter = gradeCounter + 1; // increment counter36 37 cout << "Enter grade, -1 to end: "; // prompt for input38 cin >> grade; // read next grade39 40 } // end while41 42 // termination phase43 // if user entered at least one grade ...44 if ( gradeCounter != 0 ) {45 46 // calculate average of all grades entered47 average = static_cast< double >( total ) / gradeCounter;48

static_cast<double>() treats total as a double temporarily (casting).

Required because dividing two integers truncates the remainder.

gradeCounter is an int, but it gets promoted to double.

Page 5: fig02_07.cpp (1 of 2)

2003 Prentice Hall, Inc.All rights reserved.

Outline5

fig02_09.cpp(3 of 3)

fig02_09.cppoutput (1 of 1)

49 // display average with two digits of precision50 cout << "Class average is " << average << endl;52 53 } // end if part of if/else54 55 else // if no grades were entered, output appropriate message56 cout << "No grades were entered" << endl;57 58 return 0; // indicate program ended successfully59 60 } // end function main

Enter grade, -1 to end: 75

Enter grade, -1 to end: 94

Enter grade, -1 to end: 97

Enter grade, -1 to end: 88

Enter grade, -1 to end: 70

Enter grade, -1 to end: 64

Enter grade, -1 to end: 83

Enter grade, -1 to end: 89

Enter grade, -1 to end: -1

Class average is 82.50

Page 6: fig02_07.cpp (1 of 2)

2003 Prentice Hall, Inc.All rights reserved.

Outline6

fig02_11.cpp(1 of 2)

1 // Fig. 2.11: fig02_11.cpp2 // Analysis of examination results.3 #include <iostream>4 5 using namespace std;6 // function main begins program execution10 int main()11 {12 // initialize variables in declarations13 int passes = 0; // number of passes14 int failures = 0; // number of failures15 int studentCounter = 1; // student counter16 int result; // one exam result17 18 // process 10 students using counter-controlled loop19 while ( studentCounter <= 10 ) {20 21 // prompt user for input and obtain value from user22 cout << "Enter result (1 = pass, 2 = fail): ";23 cin >> result;24

Page 7: fig02_07.cpp (1 of 2)

2003 Prentice Hall, Inc.All rights reserved.

Outline7

fig02_11.cpp(2 of 2)

25 // if result 1, increment passes; if/else nested in while26 if ( result == 1 ) // if/else nested in while27 passes = passes + 1; 28 29 else // if result not 1, increment failures 30 failures = failures + 1; 31 32 // increment studentCounter so loop eventually terminates33 studentCounter = studentCounter + 1; 34 35 } // end while36 37 // termination phase; display number of passes and failures38 cout << "Passed " << passes << endl; 39 cout << "Failed " << failures << endl;40 41 // if more than eight students passed, print "raise tuition"42 if ( passes > 8 )43 cout << "Raise tuition " << endl; 44 45 return 0; // successful termination46 47 } // end function main

Page 8: fig02_07.cpp (1 of 2)

2003 Prentice Hall, Inc.All rights reserved.

Outline8

fig02_11.cppoutput (1 of 1)

Enter result (1 = pass, 2 = fail): 1

Enter result (1 = pass, 2 = fail): 2

Enter result (1 = pass, 2 = fail): 2

Enter result (1 = pass, 2 = fail): 1

Enter result (1 = pass, 2 = fail): 1

Enter result (1 = pass, 2 = fail): 1

Enter result (1 = pass, 2 = fail): 2

Enter result (1 = pass, 2 = fail): 1

Enter result (1 = pass, 2 = fail): 1

Enter result (1 = pass, 2 = fail): 2

Passed 6

Failed 4

Enter result (1 = pass, 2 = fail): 1

Enter result (1 = pass, 2 = fail): 1

Enter result (1 = pass, 2 = fail): 1

Enter result (1 = pass, 2 = fail): 1

Enter result (1 = pass, 2 = fail): 2

Enter result (1 = pass, 2 = fail): 1

Enter result (1 = pass, 2 = fail): 1

Enter result (1 = pass, 2 = fail): 1

Enter result (1 = pass, 2 = fail): 1

Enter result (1 = pass, 2 = fail): 1

Passed 9

Failed 1

Raise tuition

Page 9: fig02_07.cpp (1 of 2)

2003 Prentice Hall, Inc.All rights reserved.

Outline9

fig02_16.cpp(1 of 1)

1 // Fig. 2.16: fig02_16.cpp2 // Counter-controlled repetition.3 #include <iostream>4 5 using namespace std;7 8 // function main begins program execution9 int main()10 {11 int counter = 1; // initialization12 13 while ( counter <= 10 ) { // repetition condition14 cout << counter << endl; // display counter15 ++counter; // increment16 17 } // end while 18 19 return 0; // indicate successful termination20 21 } // end function main

Page 10: fig02_07.cpp (1 of 2)

2003 Prentice Hall, Inc.All rights reserved.

Outline10

fig02_16.cppoutput (1 of 1)

1

2

3

4

5

6

7

8

9

10

Page 11: fig02_07.cpp (1 of 2)

2003 Prentice Hall, Inc.All rights reserved.

Outline11

fig02_17.cpp(1 of 1)

1 // Fig. 2.17: fig02_17.cpp2 // Counter-controlled repetition with the for structure.3 #include <iostream>4 5 using std::cout;6 using std::endl;7 8 // function main begins program execution9 int main()10 {11 // Initialization, repetition condition and incrementing 12 // are all included in the for structure header. 13 14 for ( int counter = 1; counter <= 10; counter++ )15 cout << counter << endl; 16 17 return 0; // indicate successful termination18 19 } // end function main

Page 12: fig02_07.cpp (1 of 2)

2003 Prentice Hall, Inc.All rights reserved.

Outline12

fig02_17.cppoutput (1 of 1)

1

2

3

4

5

6

7

8

9

10

Page 13: fig02_07.cpp (1 of 2)

2003 Prentice Hall, Inc. All rights reserved.

13

2.14for Repetition Structure

• for loops can usually be rewritten as while loopsinitialization;

while ( loopContinuationTest){

statement

increment;

}

• Initialization and increment– For multiple variables, use comma-separated lists

for (int i = 0, j = 0; j + i <= 10; j++, i++)

cout << j + i << endl;

Page 14: fig02_07.cpp (1 of 2)

2003 Prentice Hall, Inc.All rights reserved.

Outline14

fig02_20.cpp(1 of 1)

fig02_20.cppoutput (1 of 1)

1 // Fig. 2.20: fig02_20.cpp2 // Summation with for.3 #include <iostream>4 5 using std::cout;6 using std::endl;7 8 // function main begins program execution9 int main()10 {11 int sum = 0; // initialize sum12 13 // sum even integers from 2 through 10014 for ( int number = 2; number <= 100; number += 2 ) 15 sum += number; // add number to sum16 17 cout << "Sum is " << sum << endl; // output sum18 return 0; // successful termination19 20 } // end function main

Sum is 2550

Page 15: fig02_07.cpp (1 of 2)

2003 Prentice Hall, Inc. All rights reserved.

15

2.15 Examples Using the for Structure

• Program to calculate compound interest• A person invests $1000.00 in a savings account yielding 5 percent

interest. Assuming that all interest is left on deposit in the account, calculate and print the amount of money in the account at the end of each year for 10 years. Use the following formula for determining these amounts:

a = p(1+r)

• p is the original amount invested (i.e., the principal),r is the annual interest rate,n is the number of years anda is the amount on deposit at the end of the nth year

n

Page 16: fig02_07.cpp (1 of 2)

2003 Prentice Hall, Inc.All rights reserved.

Outline16

fig02_21.cpp(1 of 2)

1 // Fig. 2.21: fig02_21.cpp2 // Calculating compound interest.3 #include <iostream>4 using namespace std;9 #include <iomanip>15 #include <cmath> // enables program to use function pow16 17 // function main begins program execution18 int main()19 {20 double amount; // amount on deposit21 double principal = 1000.0; // starting principal22 double rate = .05; // interest rate23 24 // output table column heads25 cout << "Year" << “ Amount on deposit" << endl;26 30 // calculate amount on deposit for each of ten years31 for ( int year = 1; year <= 10; year++ ) {32 // calculate new amount for specified year34 amount = principal * pow( 1.0 + rate, year );35 36 // output one table row37 cout << < year << “ “<< amount << endl;39 } // end for 41 42 return 0; // indicate successful termination43 44 } // end function main

<cmath> header needed for the pow function (program will not compile without it).

Page 17: fig02_07.cpp (1 of 2)

2003 Prentice Hall, Inc.All rights reserved.

Outline17

fig02_21.cppoutput (1 of 1)

Year Amount on deposit

1 1050.00

2 1102.50

3 1157.63

4 1215.51

5 1276.28

6 1340.10

7 1407.10

8 1477.46

9 1551.33

10 1628.89

Page 18: fig02_07.cpp (1 of 2)

2003 Prentice Hall, Inc.All rights reserved.

Outline18

fig02_24.cpp(1 of 1)

fig02_24.cppoutput (1 of 1)

1 // Fig. 2.24: fig02_24.cpp2 // Using the do/while repetition structure.3 #include <iostream>4 5 using namespace std;7 8 // function main begins program execution9 int main()10 {11 int counter = 1; // initialize counter12 13 do { 14 cout << counter << " "; // display counter15 } while ( ++counter <= 10 ); // end do/while 16 17 cout << endl;18 19 return 0; // indicate successful termination20 21 } // end function main

1 2 3 4 5 6 7 8 9 10

Notice the preincrement in loop-continuation test.

Page 19: fig02_07.cpp (1 of 2)

2003 Prentice Hall, Inc. All rights reserved.

19

2.18break and continue Statements

• break statement– Immediate exit from while, for, do/while, switch– Program continues with first statement after structure

• Common uses– Escape early from a loop

– Skip the remainder of switch

Page 20: fig02_07.cpp (1 of 2)

2003 Prentice Hall, Inc.All rights reserved.

Outline20

fig02_26.cpp(1 of 2)

1 // Fig. 2.26: fig02_26.cpp2 // Using the break statement in a for structure.3 #include <iostream>4 5 using namespace std;8 // function main begins program execution9 int main()10 {12 int x; // x declared here so it can be used after the loop13 14 // loop 10 times15 for ( x = 1; x <= 10; x++ ) {17 // if x is 5, terminate loop18 if ( x == 5 )19 break; // break loop only if x is 520 21 cout << x << " "; // display value of x23 } // end for 24 25 cout << "\nBroke out of loop when x became " << x << endl;27 return 0; // indicate successful termination29 } // end function main

Exits for structure when break executed.

1 2 3 4

Broke out of loop when x became 5

Page 21: fig02_07.cpp (1 of 2)

2003 Prentice Hall, Inc. All rights reserved.

21

2.18break and continue Statements

• continue statement– Used in while, for, do/while– Skips remainder of loop body

– Proceeds with next iteration of loop

• while and do/while structure– Loop-continuation test evaluated immediately after the continue statement

• for structure– Increment expression executed

– Next, loop-continuation test evaluated

Page 22: fig02_07.cpp (1 of 2)

2003 Prentice Hall, Inc.All rights reserved.

Outline22

fig02_27.cpp(1 of 2)

1 // Fig. 2.27: fig02_27.cpp2 // Using the continue statement in a for structure.3 #include <iostream>4 5 using namespace std;8 // function main begins program execution9 int main()10 {11 // loop 10 times12 for ( int x = 1; x <= 10; x++ ) {13 14 // if x is 5, continue with next iteration of loop15 if ( x == 5 )16 continue; // skip remaining code in loop body17 18 cout << x << " "; // display value of x20 } // end for structure21 22 cout << "\nUsed continue to skip printing the value 5" 23 << endl;25 return 0; // indicate successful termination27 } // end function main

Skips to next iteration of the loop.

1 2 3 4 6 7 8 9 10

Used continue to skip printing the value 5

Page 23: fig02_07.cpp (1 of 2)

2003 Prentice Hall, Inc.All rights reserved.

Outline23

fig03_03.cpp(1 of 2)

1 // Fig. 3.3: fig03_03.cpp2 // Creating and using a programmer-defined function.3 #include <iostream>4 5 using namespace std;7 8 int square( int ); // function prototype9 10 int main()11 {12 // loop 10 times and calculate and output 13 // square of x each time14 for ( int x = 1; x <= 10; x++ ) 15 cout << square( x ) << " "; // function call16 17 cout << endl;18 19 return 0; // indicates successful termination20 21 } // end main22

Parentheses () cause function to be called. When done, it returns the result.

Function prototype: specifies data types of arguments and return values. square expects and int, and returns an int.

Page 24: fig02_07.cpp (1 of 2)

2003 Prentice Hall, Inc.All rights reserved.

Outline24

fig03_03.cpp(2 of 2)

fig03_03.cppoutput (1 of 1)

23 // square function definition returns square of an integer 24 int square( int y ) // y is a copy of argument to function25 { 26 return y * y; // returns square of y as an int 27 28 } // end function square

1 4 9 16 25 36 49 64 81 100

Definition of square. y is a copy of the argument passed. Returns y * y, or y squared.

Page 25: fig02_07.cpp (1 of 2)

2003 Prentice Hall, Inc.All rights reserved.

Outline25

fig03_04.cpp(1 of 2)

1 // Fig. 3.4: fig03_04.cpp2 // Finding the maximum of three floating-point numbers.3 #include <iostream>4 5 using namespace std;8 9 double maximum( double, double, double ); // function prototype10 11 int main()12 {13 double number1;14 double number2;15 double number3;16 17 cout << "Enter three floating-point numbers: ";18 cin >> number1 >> number2 >> number3;19 20 // number1, number2 and number3 are arguments to 21 // the maximum function call22 cout << "Maximum is: " 23 << maximum( number1, number2, number3 ) << endl;24 25 return 0; // indicates successful termination

Function maximum takes 3 arguments (all double) and returns a double.

Page 26: fig02_07.cpp (1 of 2)

2003 Prentice Hall, Inc.All rights reserved.

Outline26

fig03_04.cpp(2 of 2)

fig03_04.cppoutput (1 of 1)

26 27 } // end main28 29 // function maximum definition; 30 // x, y and z are parameters 31 double maximum( double x, double y, double z ) 32 { 33 double max = x; // assume x is largest 34 35 if ( y > max ) // if y is larger, 36 max = y; // assign y to max 37 38 if ( z > max ) // if z is larger, 39 max = z; // assign z to max 40 41 return max; // max is largest value 42 43 } // end function maximum

Enter three floating-point numbers: 99.32 37.3 27.1928

Maximum is: 99.32

Enter three floating-point numbers: 1.1 3.333 2.22

Maximum is: 3.333

Enter three floating-point numbers: 27.9 14.31 88.99

Maximum is: 88.99

Comma separated list for multiple parameters.

Page 27: fig02_07.cpp (1 of 2)

2003 Prentice Hall, Inc. All rights reserved.

27

3.6 Function Prototypes

• Function prototype contains– Function name

– Parameters (number and data type)

– Return type (void if returns nothing)

– Only needed if function definition after function call

• Prototype must match function definition– Function prototype

double maximum( double, double, double );

– Definitiondouble maximum( double x, double y, double z )

{

}

Page 28: fig02_07.cpp (1 of 2)

2003 Prentice Hall, Inc. All rights reserved.

28

3.8 Random Number Generation

• Next– Program to show distribution of rand()– Simulate 6000 rolls of a die

– Print number of 1’s, 2’s, 3’s, etc. rolled

– Should be roughly 1000 of each

Page 29: fig02_07.cpp (1 of 2)

2003 Prentice Hall, Inc.All rights reserved.

Outline29

fig03_08.cpp(1 of 3)

1 // Fig. 3.8: fig03_08.cpp2 // Roll a six-sided die 6000 times.3 #include <iostream>4 5 using namespace std;7 8 #include <iomanip>9 12 #include <cstdlib> // contains function prototype for rand13 14 int main()15 {16 int frequency1 = 0;17 int frequency2 = 0;18 int frequency3 = 0;19 int frequency4 = 0;20 int frequency5 = 0;21 int frequency6 = 0;22 int face; // represents one roll of the die23

Page 30: fig02_07.cpp (1 of 2)

2003 Prentice Hall, Inc.All rights reserved.

Outline30

fig03_08.cpp(2 of 3)

24 // loop 6000 times and summarize results25 for ( int roll = 1; roll <= 6000; roll++ ) {26 face = 1 + rand() % 6; // random number from 1 to 627 28 // determine face value and increment appropriate counter29 switch ( face ) {30 31 case 1: // rolled 132 ++frequency1;33 break;34 35 case 2: // rolled 236 ++frequency2;37 break;38 39 case 3: // rolled 340 ++frequency3;41 break;42 43 case 4: // rolled 444 ++frequency4;45 break;46 47 case 5: // rolled 548 ++frequency5;49 break;

Page 31: fig02_07.cpp (1 of 2)

2003 Prentice Hall, Inc.All rights reserved.

Outline31

fig03_08.cpp(3 of 3)

50 51 case 6: // rolled 652 ++frequency6;53 break;54 55 default: // invalid value 56 cout << "Program should never get here!";57 58 } // end switch 59 60 } // end for 61 62 // display results in tabular format63 cout << "Face" << "Frequency"64 << "\n 1" << frequency165 << "\n 2" << frequency266 << "\n 3" << frequency367 << "\n 4" << frequency468 << "\n 5" << frequency569 << "\n 6" << frequency6 << endl;70 71 return 0; // indicates successful termination72 73 } // end main

Default case included even though it should never be reached. This is a matter of good coding style

Page 32: fig02_07.cpp (1 of 2)

2003 Prentice Hall, Inc.All rights reserved.

Outline32

fig03_08.cppoutput (1 of 1)

Face Frequency

1 1003

2 1017

3 983

4 994

5 1004

6 999

Page 33: fig02_07.cpp (1 of 2)

2003 Prentice Hall, Inc.All rights reserved.

Outline33

fig03_12.cpp(1 of 5)

1 // Fig. 3.12: fig03_12.cpp2 // A scoping example.3 #include <iostream>4 5 using std::cout;6 using std::endl;7 8 void useLocal( void ); // function prototype9 void useStaticLocal( void ); // function prototype10 void useGlobal( void ); // function prototype11 12 int x = 1; // global variable13 14 int main()15 {16 int x = 5; // local variable to main17 18 cout << "local x in main's outer scope is " << x << endl;19 20 { // start new scope 21 22 int x = 7; 23 24 cout << "local x in main's inner scope is " << x << endl;25 26 } // end new scope

Declared outside of function; global variable with file scope.

Local variable with function scope.

Create a new block, giving x block scope. When the block ends, this x is destroyed.

Page 34: fig02_07.cpp (1 of 2)

2003 Prentice Hall, Inc.All rights reserved.

Outline34

fig03_12.cpp(2 of 5)

27 28 cout << "local x in main's outer scope is " << x << endl;29 30 useLocal(); // useLocal has local x31 useStaticLocal(); // useStaticLocal has static local x32 useGlobal(); // useGlobal uses global x33 useLocal(); // useLocal reinitializes its local x34 useStaticLocal(); // static local x retains its prior value35 useGlobal(); // global x also retains its value36 37 cout << "\nlocal x in main is " << x << endl;38 39 return 0; // indicates successful termination40 41 } // end main42

Page 35: fig02_07.cpp (1 of 2)

2003 Prentice Hall, Inc.All rights reserved.

Outline35

fig03_12.cpp(3 of 5)

43 // useLocal reinitializes local variable x during each call44 void useLocal( void )45 {46 int x = 25; // initialized each time useLocal is called47 48 cout << endl << "local x is " << x 49 << " on entering useLocal" << endl;50 ++x;51 cout << "local x is " << x 52 << " on exiting useLocal" << endl;53 54 } // end function useLocal55

Automatic variable (local variable of function). This is destroyed when the function exits, and reinitialized when the function begins.

Page 36: fig02_07.cpp (1 of 2)

2003 Prentice Hall, Inc.All rights reserved.

Outline36

fig03_12.cpp(4 of 5)

56 // useStaticLocal initializes static local variable x only the57 // first time the function is called; value of x is saved58 // between calls to this function59 void useStaticLocal( void )60 {61 // initialized only first time useStaticLocal is called62 static int x = 50; 63 64 cout << endl << "local static x is " << x 65 << " on entering useStaticLocal" << endl;66 ++x; 67 cout << "local static x is " << x 68 << " on exiting useStaticLocal" << endl;69 70 } // end function useStaticLocal71

Static local variable of function; it is initialized only once, and retains its value between function calls.

Page 37: fig02_07.cpp (1 of 2)

2003 Prentice Hall, Inc.All rights reserved.

Outline37

fig03_12.cpp(5 of 5)

fig03_12.cppoutput (1 of 2)

72 // useGlobal modifies global variable x during each call73 void useGlobal( void )74 {75 cout << endl << "global x is " << x 76 << " on entering useGlobal" << endl;77 x *= 10;78 cout << "global x is " << x 79 << " on exiting useGlobal" << endl;80 81 } // end function useGlobal

local x in main's outer scope is 5

local x in main's inner scope is 7

local x in main's outer scope is 5

 

local x is 25 on entering useLocal

local x is 26 on exiting useLocal

 

local static x is 50 on entering useStaticLocal

local static x is 51 on exiting useStaticLocal

global x is 1 on entering useGlobal

global x is 10 on exiting useGlobal

This function does not declare any variables. It uses the global x declared in the beginning of the program.

Page 38: fig02_07.cpp (1 of 2)

2003 Prentice Hall, Inc.All rights reserved.

Outline38

fig03_12.cppoutput (2 of 2)

 

local x is 25 on entering useLocal

local x is 26 on exiting useLocal

 

local static x is 51 on entering useStaticLocal

local static x is 52 on exiting useStaticLocal

 

global x is 10 on entering useGlobal

global x is 100 on exiting useGlobal

 

local x in main is 5

Page 39: fig02_07.cpp (1 of 2)

2003 Prentice Hall, Inc.All rights reserved.

Outline39

fig03_18.cpp(1 of 2)

1 // Fig. 3.18: fig03_18.cpp2 // Functions that take no arguments.3 #include <iostream>4 5 using std::cout;6 using std::endl;7 8 void function1(); // function prototype9 void function2( void ); // function prototype10 11 int main()12 {13 function1(); // call function1 with no arguments14 function2(); // call function2 with no arguments15 16 return 0; // indicates successful termination17 18 } // end main19

Page 40: fig02_07.cpp (1 of 2)

2003 Prentice Hall, Inc.All rights reserved.

Outline40

fig03_18.cpp(2 of 2)

fig03_18.cppoutput (1 of 1)

20 // function1 uses an empty parameter list to specify that 21 // the function receives no arguments 22 void function1()23 {24 cout << "function1 takes no arguments" << endl;25 26 } // end function127 28 // function2 uses a void parameter list to specify that 29 // the function receives no arguments 30 void function2( void )31 {32 cout << "function2 also takes no arguments" << endl;33 34 } // end function2

function1 takes no arguments

function2 also takes no arguments

Page 41: fig02_07.cpp (1 of 2)

2003 Prentice Hall, Inc.All rights reserved.

Outline41

fig03_20.cpp(1 of 2)

1 // Fig. 3.20: fig03_20.cpp2 // Comparing pass-by-value and pass-by-reference3 // with references.4 #include <iostream>5 6 using std::cout;7 using std::endl;8 9 int squareByValue( int ); // function prototype10 void squareByReference( int & ); // function prototype11 12 int main()13 {14 int x = 2;15 int z = 4;16 17 // demonstrate squareByValue18 cout << "x = " << x << " before squareByValue\n";19 cout << "Value returned by squareByValue: "20 << squareByValue( x ) << endl; 21 cout << "x = " << x << " after squareByValue\n" << endl;22

Notice the & operator, indicating pass-by-reference.

Page 42: fig02_07.cpp (1 of 2)

2003 Prentice Hall, Inc.All rights reserved.

Outline42

fig03_20.cpp(2 of 2)

23 // demonstrate squareByReference24 cout << "z = " << z << " before squareByReference" << endl;25 squareByReference( z );26 cout << "z = " << z << " after squareByReference" << endl;27 28 return 0; // indicates successful termination29 } // end main30 31 // squareByValue multiplies number by itself, stores the 32 // result in number and returns the new value of number 33 int squareByValue( int number ) 34 { 35 return number *= number; // caller's argument not modified36 37 } // end function squareByValue 38 39 // squareByReference multiplies numberRef by itself and 40 // stores the result in the variable to which numberRef 41 // refers in function main 42 void squareByReference( int &numberRef ) 43 { 44 numberRef *= numberRef; // caller's argument modified45 46 } // end function squareByReference

Changes number, but original parameter (x) is not modified.

Changes numberRef, an alias for the original parameter. Thus, z is changed.

Page 43: fig02_07.cpp (1 of 2)

2003 Prentice Hall, Inc.All rights reserved.

Outline43

fig03_20.cppoutput (1 of 1)

x = 2 before squareByValue

Value returned by squareByValue: 4

x = 2 after squareByValue

 

z = 4 before squareByReference

z = 16 after squareByReference