of 68

• Author

elay-bee-ue
• Category

Documents

• view

232

0

Embed Size (px)

Transcript of Chap04 Iterative

• 8/2/2019 Chap04 Iterative

1/68

• 8/2/2019 Chap04 Iterative

2/68

Averaging

• 8/2/2019 Chap04 Iterative

3/68

Determining Average Magnitude

Suppose we want to calculate the average apparent brightnessof a list of five star magnitude values

Can we do it?

Yes, it would be easy

Suppose we want to calculate the average apparent brightnessof a list of five star magnitude values

Can we do it

Yes, it would be easy

Suppose we want to calculate the average apparent brightnessof a list of 8,479 stars visible from earth

Can we do it

Yes, but it would be gruesome without the use ofiteration

• 8/2/2019 Chap04 Iterative

4/68

C++ Iterative Constructs

Three constructs

while statement

for statement

do-while statement

• 8/2/2019 Chap04 Iterative

5/68

While Syntax

Logical expression that determineswhether the action is to be executed

while ( Expression) Action

Action to be iterativelyperformed until logical

expression is false

• 8/2/2019 Chap04 Iterative

6/68

While Semantics

Expression

Action

true fa lse

Expression isevaluated at the

start of eachiteration of the

loop

If Expression istrue, Action is

executed If Expression isfalse, program

executioncontinues w ith

next statement

• 8/2/2019 Chap04 Iterative

7/68

Computing an Average

int listSize = 4;

int numberProcessed = 0;

double sum = 0;

while (numberProcessed < listSize) {double value;

cin >> value;

sum += value;

++numberProcessed;

}

double average = sum / numberProcessed ;

cout

• 8/2/2019 Chap04 Iterative

8/68

Execution Trace

int listSize = 4;

int numberProcessed = 0;

double sum = 0;

while (numberProcessed < listSize) {double value;

cin >> value;

sum += value;

++numberProcessed;

}

double average = sum / numberProcessed ;

cout

• 8/2/2019 Chap04 Iterative

9/68

Execution Trace

int listSize = 4;

int numberProcessed = 0;

double sum = 0;

while (numberProcessed < listSize) {double value;

cin >> value;

sum += value;

++numberProcessed;

}

double average = sum / numberProcessed ;

cout

• 8/2/2019 Chap04 Iterative

10/68

Execution Trace

int listSize = 4;

int numberProcessed = 0;

double sum = 0;

while (numberProcessed < listSize) {double value;

cin >> value;

sum += value;

++numberProcessed;

}

double average = sum / numberProcessed ;

cout

• 8/2/2019 Chap04 Iterative

11/68

Execution Trace

int listSize = 4;

int numberProcessed = 0;

double sum = 0;

while (numberProcessed < listSize) {double value;

cin >> value;

sum += value;

++numberProcessed;

}

double average = sum / numberProcessed ;

cout

• 8/2/2019 Chap04 Iterative

12/68

Execution Trace

int listSize = 4;

int numberProcessed = 0;

double sum = 0;

while (numberProcessed < listSize) {double value;

cin >> value;

sum += value;

++numberProcessed;

}

double average = sum / numberProcessed ;

cout

• 8/2/2019 Chap04 Iterative

13/68

Execution Trace

int listSize = 4;

int numberProcessed = 0;

double sum = 0;

while (numberProcessed < listSize) {double value;

cin >> value;

sum += value;

++numberProcessed;

}

double average = sum / numberProcessed ;

cout

• 8/2/2019 Chap04 Iterative

14/68

Execution Trace

int listSize = 4;

int numberProcessed = 0;

double sum = 0;

while (numberProcessed < listSize) {double value;

cin >> value;

sum += value;

++numberProcessed;

}

double average = sum / numberProcessed ;

cout

• 8/2/2019 Chap04 Iterative

15/68

Execution Trace

int listSize = 4;

int numberProcessed = 0;

double sum = 0;

while (numberProcessed < listSize) {double value;

cin >> value;

sum += value;

++numberProcessed;

}

double average = sum / numberProcessed ;

cout

• 8/2/2019 Chap04 Iterative

16/68

Execution Trace

int listSize = 4;

int numberProcessed = 0;

double sum = 0;

while (numberProcessed < listSize) {double value;

cin >> value;

sum += value;

++numberProcessed;

}

double average = sum / numberProcessed ;

cout

• 8/2/2019 Chap04 Iterative

17/68

Execution Trace

int listSize = 4;

int numberProcessed = 0;

double sum = 0;

while (numberProcessed < listSize) {double value;

cin >> value;

sum += value;

++numberProcessed;

}

double average = sum / numberProcessed ;

cout

• 8/2/2019 Chap04 Iterative

18/68

Execution Trace

int listSize = 4;

int numberProcessed = 0;

double sum = 0;

while (numberProcessed < listSize) {double value;

cin >> value;

sum += value;

++numberProcessed;

}

double average = sum / numberProcessed ;

cout

• 8/2/2019 Chap04 Iterative

19/68

Execution Trace

int listSize = 4;

int numberProcessed = 0;

double sum = 0;

while (numberProcessed < listSize) {double value;

cin >> value;

sum += value;

++numberProcessed;

}

double average = sum / numberProcessed ;

cout

• 8/2/2019 Chap04 Iterative

20/68

Execution Trace

int listSize = 4;

int numberProcessed = 0;

double sum = 0;

while (numberProcessed < listSize) {double value;

cin >> value;

sum += value;

++numberProcessed;

}

double average = sum / numberProcessed ;

cout

• 8/2/2019 Chap04 Iterative

21/68

Execution Trace

int listSize = 4;

int numberProcessed = 0;

double sum = 0;

while (numberProcessed < listSize) {double value;

cin >> value;

sum += value;

++numberProcessed;

}

double average = sum / numberProcessed ;

cout

• 8/2/2019 Chap04 Iterative

22/68

Execution Trace

int listSize = 4;

int numberProcessed = 0;

double sum = 0;

while (numberProcessed < listSize) {double value;

cin >> value;

sum += value;

++numberProcessed;

}

double average = sum / numberProcessed ;

cout

• 8/2/2019 Chap04 Iterative

23/68

Execution Trace

int listSize = 4;

int numberProcessed = 0;

double sum = 0;

while (numberProcessed < listSize) {double value;

cin >> value;

sum += value;

++numberProcessed;

}

double average = sum / numberProcessed ;

cout

• 8/2/2019 Chap04 Iterative

24/68

Execution Trace

int listSize = 4;

int numberProcessed = 0;

double sum = 0;

while (numberProcessed < listSize) {double value;

cin >> value;

sum += value;

++numberProcessed;

}

double average = sum / numberProcessed ;

cout

• 8/2/2019 Chap04 Iterative

25/68

Execution Trace

int listSize = 4;

int numberProcessed = 0;

double sum = 0;

while (numberProcessed < listSize) {double value;

cin >> value;

sum += value;

++numberProcessed;

}

double average = sum / numberProcessed ;

cout

• 8/2/2019 Chap04 Iterative

26/68

Execution Trace

int listSize = 4;

int numberProcessed = 0;

double sum = 0;

while (numberProcessed < listSize) {double value;

cin >> value;

sum += value;

++numberProcessed;

}

double average = sum / numberProcessed ;

cout

• 8/2/2019 Chap04 Iterative

27/68

Execution Trace

int listSize = 4;

int numberProcessed = 0;

double sum = 0;

while (numberProcessed < listSize) {double value;

cin >> value;

sum += value;

++numberProcessed;

}

double average = sum / numberProcessed ;

cout

• 8/2/2019 Chap04 Iterative

28/68

Execution Trace

int listSize = 4;

int numberProcessed = 0;

double sum = 0;

while (numberProcessed < listSize) {double value;

cin >> value;

sum += value;

++numberProcessed;

}

double average = sum / numberProcessed ;

cout

• 8/2/2019 Chap04 Iterative

29/68

Execution Trace

int listSize = 4;

int numberProcessed = 0;

double sum = 0;

while (numberProcessed < listSize) {double value;

cin >> value;

sum += value;

++numberProcessed;

}

double average = sum / numberProcessed ;

cout

• 8/2/2019 Chap04 Iterative

30/68

Execution Trace

int listSize = 4;

int numberProcessed = 0;

double sum = 0;

while (numberProcessed < listSize) {double value;

cin >> value;

sum += value;

++numberProcessed;

}

double average = sum / numberProcessed ;

cout

• 8/2/2019 Chap04 Iterative

31/68

Execution Trace

int listSize = 4;

int numberProcessed = 0;

double sum = 0;

while (numberProcessed < listSize) {double value;

cin >> value;

sum += value;

++numberProcessed;

}

double average = sum / numberProcessed ;

cout

• 8/2/2019 Chap04 Iterative

32/68

Execution Trace

int listSize = 4;

int numberProcessed = 0;

double sum = 0;

while (numberProcessed < listSize) {double value;

cin >> value;

sum += value;

++numberProcessed;

}

double average = sum / numberProcessed ;

cout

• 8/2/2019 Chap04 Iterative

33/68

Execution Trace

int listSize = 4;

int numberProcessed = 0;

double sum = 0;

while (numberProcessed < listSize) {double value;

cin >> value;

sum += value;

++numberProcessed;

}

double average = sum / numberProcessed ;

cout

• 8/2/2019 Chap04 Iterative

34/68

Execution Trace

int listSize = 4;

int numberProcessed = 0;

double sum = 0;

while (numberProcessed < listSize) {double value;

cin >> value;

sum += value;

++numberProcessed;

}

double average = sum / numberProcessed ;

cout

• 8/2/2019 Chap04 Iterative

35/68

Power of Two Table

const int TableSize = 20;

int i = 0;

long Entry = 1;

cout

• 8/2/2019 Chap04 Iterative

36/68

Better Way of Averagingint numberProcessed = 0;

double sum = 0;

double value;

while ( cin >> value ) {

sum += value;++numberProcessed;

}

double average = sum / numberProcessed ;

cout

• 8/2/2019 Chap04 Iterative

37/68

Even Better Way of Averagingint numberProcessed = 0;double sum = 0;

double value;

while ( cin >> value ) {

sum += value;

++numberProcessed;

}

if ( numberProcessed > 0 ) {

double average = sum / numberProcessed ;

cout

• 8/2/2019 Chap04 Iterative

38/68

The For Statement

Syntax

for(ForInit; ForExpression; PostExpression)

ActionExample

for (int i = 0; i < 3; ++i) {

cout

• 8/2/2019 Chap04 Iterative

39/68

ForExpr

Action

true fa lse

ForInit

PostExpr

Evaluated onceat the beginning

of the forstatements's

executionThe ForExpr is

evaluated at thestart of each

iteration of theloop

If ForExpr istrue, Action is

executed

After the Actionhas completed,

the

PostExpressionis evaluated

If ForExpr isfalse, program

executioncontinues with

next statement

After evaluating the

PostExpression, the nextiteration of the loop starts

• 8/2/2019 Chap04 Iterative

40/68

Execution Trace

for (int i = 0; i < 3; ++i) {

cout

• 8/2/2019 Chap04 Iterative

41/68

Execution Trace

for (int i = 0; i < 3; ++i) {

cout

• 8/2/2019 Chap04 Iterative

42/68

Execution Trace

for (int i = 0; i < 3; ++i) {

cout

• 8/2/2019 Chap04 Iterative

43/68

Execution Trace

for (int i = 0; i < 3; ++i) {

cout

• 8/2/2019 Chap04 Iterative

44/68

Execution Trace

for (int i = 0; i < 3; ++i) {

cout

• 8/2/2019 Chap04 Iterative

45/68

Execution Trace

for (int i = 0; i < 3; ++i) {

cout

• 8/2/2019 Chap04 Iterative

46/68

Execution Trace

for (int i = 0; i < 3; ++i) {

cout

• 8/2/2019 Chap04 Iterative

47/68

Execution Trace

for (int i = 0; i < 3; ++i) {

cout

• 8/2/2019 Chap04 Iterative

48/68

Execution Trace

for (int i = 0; i < 3; ++i) {

cout

• 8/2/2019 Chap04 Iterative

49/68

Execution Trace

for (int i = 0; i < 3; ++i) {

cout

• 8/2/2019 Chap04 Iterative

50/68

Execution Trace

for (int i = 0; i < 3; ++i) {

cout

• 8/2/2019 Chap04 Iterative

51/68

Execution Trace

for (int i = 0; i < 3; ++i) {

cout

• 8/2/2019 Chap04 Iterative

52/68

Execution Trace

for (int i = 0; i < 3; ++i) {

cout

• 8/2/2019 Chap04 Iterative

53/68

Execution Trace

for (int i = 0; i < 3; ++i) {

cout

• 8/2/2019 Chap04 Iterative

54/68

Execution Trace

for (int i = 0; i < 3; ++i) {

cout

• 8/2/2019 Chap04 Iterative

55/68

Table Revisiting

const int TableSize = 20;

long Entry = 1;

cout

• 8/2/2019 Chap04 Iterative

56/68

Table Revisiting

const int TableSize = 20;

long Entry = 1;

cout

• 8/2/2019 Chap04 Iterative

57/68

Displaying a Diagonal

SimpleWindow W("One diagonal", 5.5, 2.25);

W.Open();

for (int j = 1; j

• 8/2/2019 Chap04 Iterative

58/68

Sample Display

• 8/2/2019 Chap04 Iterative

59/68

Displaying Three Diagonals

SimpleWindow W("Three diagonals", 6.5, 2.25);

W.Open();

for (int i = 1; i

• 8/2/2019 Chap04 Iterative

60/68

Sample Display

int Co nter1 0

• 8/2/2019 Chap04 Iterative

61/68

int Counter1 = 0;int Counter2 = 0;int Counter3 = 0;int Counter4 = 0;int Counter5 = 0;

++Counter1;

for (int i = 1; i

• 8/2/2019 Chap04 Iterative

62/68

For Into While

Observation

The for statement is equivalent to

{

ForInit;

while (ForExpression) {

Action;

PostExpression;

}

}

• 8/2/2019 Chap04 Iterative

63/68

Counting Characters

int NumberOfNonBlanks = 0;

int NumberOfUpperCase = 0;

char c;

while (cin >> c) {++NumberOfNonBlanks;

if ((c >= 'A') && (c

• 8/2/2019 Chap04 Iterative

64/68

Counting All Characters

char c;

int NumberOfCharacters = 0;

int NumberOfLines = 0;

while ( cin.get(c) ) {

++NumberOfCharacters;

if (c == '\n') {

++NumberOfLines

}

}

cout

• 8/2/2019 Chap04 Iterative

65/68

#include #include using namespace std;int main() {

ifstream fin("mydata.txt");int ValuesProcessed = 0;float ValueSum = 0;float Value;while ( fin >> Value ) {

ValueSum += Value;++ValuesProcessed;

}if (ValuesProcessed > 0) {

ofstream fout("average.txt");float Average = ValueSum / ValuesProcessed;fout

• 8/2/2019 Chap04 Iterative

66/68

Iteration Dos

Key Points

Make sure there is a statement that will eventually terminatethe iteration criterion

The loop must stop!

Make sure that initialization of loop counters or iterators isproperly performed

Have a clear purpose for the loop

Document the purpose of the loop

Document how the body of the loop advances thepurpose of the loop

• 8/2/2019 Chap04 Iterative

67/68

The Do-While Statement

Syntax

doAction

while (Expression)

Semantics

ExecuteAction

IfExpressionis true thenexecuteActionagain

Repeat this process untilExpressionevaluates to

falseActionis either a singlestatement or a group ofstatements within braces

Action

true

false

Expression

• 8/2/2019 Chap04 Iterative

68/68