zhen jiang dept. of computer science west chester university west chester, pa 19383

32
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA 19383 [email protected] CSC530 Data Structures - LOOP 07/04/22 1

Upload: julio

Post on 19-Jan-2016

59 views

Category:

Documents


0 download

DESCRIPTION

CSC530 Data Structures - LOOP. Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA 19383 [email protected]. Loop. Price is right. Sample execution (click on this link to try) Each button in the above sample REPEAT …?. While loop Format & Logic. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Zhen Jiang Dept. of Computer Science  West Chester University West Chester, PA 19383

Zhen Jiang

Dept. of Computer Science

West Chester University

West Chester, PA 19383

[email protected]

CSC530 Data Structures - LOOP

04/21/23 1

Page 2: Zhen Jiang Dept. of Computer Science  West Chester University West Chester, PA 19383

Price is right. Sample execution (click on this link to

try) Each button in the above sample

REPEAT …?

Loop

04/21/23 2

Page 3: Zhen Jiang Dept. of Computer Science  West Chester University West Chester, PA 19383

While loop Format & Logic

04/21/23 3

Page 4: Zhen Jiang Dept. of Computer Science  West Chester University West Chester, PA 19383

404/21/23

<initialization>;while (<test>) {

<body>;}

Page 5: Zhen Jiang Dept. of Computer Science  West Chester University West Chester, PA 19383

Do-while loop Format Logic

04/21/23 5

Page 6: Zhen Jiang Dept. of Computer Science  West Chester University West Chester, PA 19383

6

How does this differ from the while loop?

The controlled <statement(s)> will always execute the first time, regardless of whether the <test> is true or false.

04/21/23

Page 7: Zhen Jiang Dept. of Computer Science  West Chester University West Chester, PA 19383

For loop Format Logic

04/21/23 7

Page 8: Zhen Jiang Dept. of Computer Science  West Chester University West Chester, PA 19383

8

for (<init>; <test>; <update>) { <body>;

}

04/21/23

Page 9: Zhen Jiang Dept. of Computer Science  West Chester University West Chester, PA 19383

04/21/23 9

Summary

Body first, and thenevent change/update

Page 10: Zhen Jiang Dept. of Computer Science  West Chester University West Chester, PA 19383

Development process

04/21/23 10

Page 11: Zhen Jiang Dept. of Computer Science  West Chester University West Chester, PA 19383

04/21/23 11

Page 12: Zhen Jiang Dept. of Computer Science  West Chester University West Chester, PA 19383

Controlling Number of Loop Iterations If the number of iterations is known

before the loop starts, the loop is called a count-controlled loop. Counter =0, counter++, counter

<number Counter = 1, counter++, counter

<=number Use for loop for an easy

development.

04/21/23 12

Page 13: Zhen Jiang Dept. of Computer Science  West Chester University West Chester, PA 19383

04/21/23 13

Page 14: Zhen Jiang Dept. of Computer Science  West Chester University West Chester, PA 19383

04/21/23 14

Page 15: Zhen Jiang Dept. of Computer Science  West Chester University West Chester, PA 19383

15

Code:for (int i = 1; i <= 4; i++) { cout << i << " squared is " << (i * i) << endl;}

Output:1 squared is 12 squared is 43 squared is 94 squared is 16

04/21/23

Page 16: Zhen Jiang Dept. of Computer Science  West Chester University West Chester, PA 19383

16

Code:cin >> n; // try 6!for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { cout << “*”; } cout <<endl;}

Output:************************************

04/21/23

Page 17: Zhen Jiang Dept. of Computer Science  West Chester University West Chester, PA 19383

17

Code: cin >> n; // try 5!

for (int i = 1; i <= n; i++) { for (int j = 1; j <= 10; j++) { cout <<(i * j) << " "; } cout << endl; }

Output:1 2 3 4 5 6 7 8 9 102 4 6 8 10 12 14 16 18 203 6 9 12 15 18 21 24 27 304 8 12 16 20 24 28 32 36 405 10 15 20 25 30 35 40 45 50

04/21/23

Page 18: Zhen Jiang Dept. of Computer Science  West Chester University West Chester, PA 19383

18

Code:cin >> n; // try 6!for (i = 1; i<=n; i++) cout << “*”;cout <<endl;for (i = 1; i <= n-2; i++) {

cout << “*”; for (int j = 1; j <= n-2; j++)

cout <<“ ”; cout <<“*” <<endl;}for (i = 1; i<=n; i++) cout <<“*”;cout << endl;

Output:******* ** ** ** *******

04/21/23

Page 19: Zhen Jiang Dept. of Computer Science  West Chester University West Chester, PA 19383

19

Code:cin >> n; // try 6!for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { cout << "*"; } cout <<endl;}

Output:*********************

04/21/23

Page 20: Zhen Jiang Dept. of Computer Science  West Chester University West Chester, PA 19383

20

Code:cin >> n; // try 6!for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { cout << i; } cout << endl;}

Output:122333444455555666666

04/21/23

Page 21: Zhen Jiang Dept. of Computer Science  West Chester University West Chester, PA 19383

21

Code:cin >> n; // try 5!for (int i = 1; i <= n; i++) { for (int j = 1; j <= (n - i); j++) { cout << " "; } for (int k = 1; k <= i; k++) { cout << i; } cout << endl;}

Output: 1 22 333 44445555504/21/23

Page 22: Zhen Jiang Dept. of Computer Science  West Chester University West Chester, PA 19383

Otherwise (unknown or unclear), the loop is called a event-controlled loop. Use a while loop or a do-while loop for an

easy checkpoint development. Asking the user before each iteration if it is

time to end the loop is called the ask-before-iterating technique.

Appropriate status update (or event initializing) for a sequence of iterations

04/21/23 22

Controlling Event of Loop Iterations

Page 23: Zhen Jiang Dept. of Computer Science  West Chester University West Chester, PA 19383

04/21/23 23

Page 24: Zhen Jiang Dept. of Computer Science  West Chester University West Chester, PA 19383

24

Finds and prints a number's first factor other than 1:

cin >> n; // try 91int f = 2;while (n % f != 0) { f++;}cout << "First factor:“ << f <<endl;

Sample run:First factor:7

04/21/23

Page 25: Zhen Jiang Dept. of Computer Science  West Chester University West Chester, PA 19383

25

Write a program that will repeatedly prompt the user to type a number until the user types a non-negative number, then square it.

Example log:

Type a non-negative integer: -5

Invalid number, try again: -1

Invalid number, try again: -235

Invalid number, try again: -87

Invalid number, try again: 11

11 squared is 121

04/21/23

Page 26: Zhen Jiang Dept. of Computer Science  West Chester University West Chester, PA 19383

26

cout << "Type a non-negative integer: ";cin >> n;

while (n < 0) { cout << "Invalid number, try again: "; cin >> n;}

int square = n * n;cout << n << " squared is " << square<<endl;

Notice that the number variable had to be declared outside the while loop in order to remain in scope.

04/21/23

Page 27: Zhen Jiang Dept. of Computer Science  West Chester University West Chester, PA 19383

27

Write a class named DigitSum that reads an integer from the user and prints the sum of the digits of that number. You may assume that the number is non-negative.

Example:

Enter a nonnegative number: 29107prints out 19 (i.e.,2+9+1+0+7 )

Hint: Use the % operator to extract the last digit of a number. If we do this repeatedly, when should we stop?04/21/23

Page 28: Zhen Jiang Dept. of Computer Science  West Chester University West Chester, PA 19383

28

cin >> n; int sum = 0; while (n > 0) { sum += n % 10; // add last digit to sum n = n / 10; // remove last digit } cout << “sum = “ << sum << endl;

04/21/23

Page 29: Zhen Jiang Dept. of Computer Science  West Chester University West Chester, PA 19383

29

Write a program named CountFactors that reads in an integer and displays its number of factors.

For example, if the user enters 60, CountFactors displays 12 because 1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30, and 60 are all factors of 60.

cin >> n; int sum = 0, k = ?; while ( ) {

} cout << “sum = “ << sum <<endl;

04/21/23

Page 30: Zhen Jiang Dept. of Computer Science  West Chester University West Chester, PA 19383

Exercise

 population  TV purchase 1+2+4+8+...1+2+3+4+...+99

04/21/23 30

Page 31: Zhen Jiang Dept. of Computer Science  West Chester University West Chester, PA 19383

04/21/23 31

Page 32: Zhen Jiang Dept. of Computer Science  West Chester University West Chester, PA 19383

Solution

04/21/23 32