+ All Categories
Home > Documents > 10/25/2020 - Universiti Teknologi Malaysia

10/25/2020 - Universiti Teknologi Malaysia

Date post: 04-Nov-2021
Category:
Upload: others
View: 2 times
Download: 0 times
Share this document with a friend
22
10/25/2020 1 CHAPTER 3: PROBLEM SOLVING TECHNIQUES 1 2 Use the basic problem-solving techniques. Develop algorithms through the process of top-down. To use the logical operators to form a conditional expression in control statements. Chapter 3 Objectives 3 3.1 Introduction 3.2 Development Algorithms 3.3 Control Structures 3.4 Top-Down Design 3.5 Summary Chapter 3 Contents 4 Chapter 3 3.1 Introduction ____________________________________________________________________________________________________________________________________ _ Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education Before writing a program to solve a particular problem, we need to to really understand the problem and plan the solution approach carefully. Therefore, we need a solving technique or algorithm that can facilitate the development of structured computer program. Note: One problem can has more than a solving algorithm but it must has only a single definition so that everybody will has the same interpretation of the algorithm.
Transcript

10/25/2020

1

CHAPTER 3:

PROBLEM SOLVING TECHNIQUES

1

2

• Use the basic problem-solving techniques.

• Develop algorithms through the process of

top-down.

• To use the logical operators to form a

conditional expression in control statements.

Chapter 3 Objectives

3

3.1 Introduction

3.2 Development Algorithms

3.3 Control Structures

3.4 Top-Down Design

3.5 Summary

Chapter 3 Contents

4

Chapter 3 3.1 Introduction

_____________________________________________________________________________________________________________________________________

Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education

• Before writing a program to solve a particular problem, we

need to to really understand the problem and plan the

solution approach carefully.

• Therefore, we need a solving technique or algorithm that

can facilitate the development of structured computer

program.

• Note: One problem can has more than a solving algorithm

but it must has only a single definition so that everybody

will has the same interpretation of the algorithm.

10/25/2020

2

5

Chapter 3 3.1 Introduction

_____________________________________________________________________________________________________________________________________

I Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education. 2 http://www.tiem.utk.edu/~gross/c++man/

(Recall in Chapter 2: System Development Life Cycle)

• A step-by-step program plan is created during the design stage.

• Three tools in the problem solving techniques will be discussed:

Development algorithms

o Pseudo-code

o Flowchart

Top-down design

o Structured chart

Notations for planning

detailed algorithm

System

Requirements

6

3.1 Introduction

3.2 Development Algorithms

• Pseudo-code

• Flowchart

3.3 Control Structures

3.4 Top-Down Design

3.5 Summary

Chapter 3 Contents

7

Chapter 3

_____________________________________________________________________________________________________________________________________

Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education

• The solution to any computing problem will involves a

series of action in a specific order.

• Algorithm is a procedure for solving a problem in terms of

• the actions to be executed, and

• the order in which these actions are to be executed.

Introduction

3.2 Development Algorithms

8

Chapter 3 3.2 Development Algorithms

_____________________________________________________________________________________________________________________________________

Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education

Pseudo-code

• Pseudo-code is an artificial and informal English-like

language with limited vocabulary that helps us to develop and

describe algorithms.

• It defines the procedural logic of an algorithm in a simple,

easy-to-understand for its readers.

• The pseudo-code consists purely of characters that are not

executed on computers.

• A carefully prepared pseudo-code program may be easily

converted to a corresponding C program.

10/25/2020

3

9

1. Start

2. Get the first number, let say A

3. Get the second number, let say B

4. Calculate the result let say C, C= A * B

5. Display the result, C

6. End

Chapter 3 3.2 Development Algorithms

Example: Pseudo-code

Algorithm for multiplying two numbers.

10

Chapter 3

Write the pseudo-code program to calculate and

display the volume of a cylinder. Activity 3.1:

3.2 Development Algorithms

11

Chapter 3

Solution 3.1:

3.2 Development Algorithms

1. Start

2. Get the high, let say h

3. Get the radius, let say r

4. Calculate the result let say V,

V = π * h * r2

5. Display the result, V

6. End

12

Chapter 3 3.2 Development Algorithms

_____________________________________________________________________________________________________________________________________

Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education

• A flowchart is a graph of geometrical shapes and connected

by lines to develop and represent an algorithm.

• Flowcharts clearly show how the control structures operate.

• 2 important elements in flowchart:

Flowchart

Geometrical shapes –

represent type of

statements in the

algorithm

Flowlines –

show the order in which

the statements of an

algorithm are executed.

10/25/2020

4

13

Chapter 3 3.2 Development Algorithms

Terminal: Used to indicates the start and end of a flowchart. Single flowline. Only one

“Start” and “Stop” terminal for each program. The end terminal for

function/subroutine must use “Return” instead of “Stop”.

Process: Used whenever data is being manipulated. One flowline enters and one

flowline exits.

Input/Output: Used whenever data is entered (input) or displayed (output). One

flowline enters and one flowline exits.

Decision: Used to represent operations in which there are two possible selections.

One flowline enters and two flowlines (labelled as “Yes” and “No”) exit.

Function / Subroutine: Used to identify an operation in a separate flowchart segment

(module). One flowline enters and one flowline exits.

On-page Connector: Used to connect remote flowchart portion on the same page.

One flowline enters and one flowline exits.

Off-page Connector: Used to connect remote flowchart portion on different pages.

One flowline enters and one flowline exits.

Comment: Used to add descriptions or clarification.

Flowline: Used to indicate the direction of flow of control.

Table: Some of typical flowchart symbols

14

Example: Flowchart

Algorithm for

multiplying two

numbers.

Start

End

Get first number,

A

Get second

number, B

Display the

Result C

Calculate Resut

C=A*B

Start Terminal:

Program start here

Stop Terminal:

Program end here

Input: Enter values

for A and B

Process

Output

Chapter 3 3.2 Development Algorithms

15

Example: Flowchart

Use of Comments

/Description

Chapter 3 3.2 Development Algorithms

Start

Read N,

M

No

Yes

Stop

N = The number of students

M = The number of subjects

16

Example: Flowchart

Use the connectors

of the same page

Chapter 3 3.2 Development Algorithms

Start

2

1

1

2

Stop

1 connection on the same

flowchart portion

2 connection on the

different flowchart portion

10/25/2020

5

17

Example: Flowchat - Use the connectors of the different page.

Chapter 3 3.2 Development Algorithms

Page 1 Page 2

Start

No

1

Yes 1

2

End

2

18

Example:

Function-Call

Note:

Module = Function

= Subroutine .

Chapter 3 3.2 Development Algorithms

Page 1

AVRG (result, n1, n2,n3)

Start

End

Read

n1, n2 , n3

Print

result

The details (how the function works)

we put in another flowchart.

This also known as Function-Definition

At this part,

we only know what

we want to do. But we

don’t know how to do it

This part also known as

Function-Call

Page 2

AVRG ( result,n1, n2,n3)

Return

sum = n1+ n2+n3

result = sum/3

End terminal

must be “Return”

Start terminal for a

Function is different.

Do not use “Start”

Body of a function is

the same with

normal flowchart

19

Chapter 3

Draw the flowchart design to calculate and display

the volume of a cylinder. Activity 3.2:

3.2 Development Algorithms

20

Chapter 3

Solution 3.2:

3.2 Development Algorithms

Start

End

Get first number,

A

Get second

number, B

Display the

Result C

Calculate Resut

C=A*B

Get the high, h

Get the radius, r

Calculate volume,

V= π*h*r2

Display the

result V

10/25/2020

6

21

Chapter 3

Convert the temperature entered by the user in the

unit of Fahrenheit to the unit of Celcius. The

conversion formula is as follows:

Fahrenheit=(Celcius*9/5)+32

a) Identify the input, process and output.

b) Write the pseudo-code design of the problem.

c) Draw the flowchart design of the problem.

Activity 3.3:

3.2 Development Algorithms

_____________________________________________________________________________________________________________________________________

http://static6.depositphotos.com/1147032/632/v/450/depositphotos_6329864-Thermometer-vector-celsius-fahrenheit.jpg

22

Chapter 3

Solution 3.3:

3.2 Development Algorithms

1. Start

2. Get Fahrenheit,

let say F

3. Calculate the

celcius let say C,

C = (F-32)*5/9

4. Display the

result, C

5. End

start

Get, F

Calculate

C=(F-32)*5/9

Display C

end

F=fahrenheit

C=celcius

23

3.1 Introduction

3.2 Development Algorithms

3.3 Control Structures

• Introduction

• Sequential Structure,

• Selective Structure,

• Repetition Structure

3.4 Top-Down Design

3.5 Summary

Chapter 3 Contents

24

Chapter 3 3.3 Control Structures

• Normally, statements in program are in sequential execution.

• However, all programs could be written in three way of control

structures to describe it’s flow of execution:

• Programs produced with structured techniques were clearer,

easier to debug and modify.

_____________________________________________________________________________________________________________________________________

I Radzi, N.H.M, Hashim, S.Z.M. and Samsuri, P. (2001). Pengaturcaraan C. Malaysia: McGraw-Hill. 2 Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education

Control Structures

Sequential Structure Selective Structure Repetition Structure

Introduction

10/25/2020

7

25

Chapter 3 3.3 Control Structures

(a) Sequential Structure

• A series of steps or

statements that are

executed in the order they

are written in an algorithm.

• Pseudo-code: mark the

beginning and end of a

block of statements.

1. Start

2. Statement_1

3. Statement_2

4. Statement_3

n. Statement_n+1

n+1. End

26

Chapter 3 3.3 Control Structures

• Multiple statements considered as one statement.

statement_1

statement_2

Statement simply means

command or instruction

statement

Figure: Flowchart in sequential structure

27

Chapter 3 3.3 Control Structures

Start

End

Input

Length,

Width

Print

Area,

Perimeter

Calculate Area

Area=Length * Width

Calculate Perimeter

Perimeter=

2 * (Width+Length)

Input:

Length 5

Width 3

Process:

Area = 5 * 3 = 15

Process:

Perimeter =

2* (5+3) = 16

Output:

Area = 15

Perimeter = 16

Example:

Trace in sequential

structure

28

Chapter 3

Calculate the total salary for Ahmad based on the

number of days in a month. Everyday he spent 8

hours with pay RM3 per hour.

a) Identify the input, process and output.

b) Draw the sequential structure of the flowchart

design of the problem.

Activity 3.4:

_____________________________________________________________________________________________________________________________________

http://static6.depositphotos.com/1147032/632/v/450/depositphotos_6329864-Thermometer-vector-celsius-fahrenheit.jpg

3.3 Control Structures

10/25/2020

8

29

Chapter 3

Solution 3.4:

3.3 Control Structures

30

Chapter 3 3.3 Control Structures

(b) Selection Structure

• Selection allows you to choose between two or more

alternatives; that is it allows you to make decision.

• Decisions made by a computer must be very simple since

everything in the computer ultimately reduces to either true (1) or false (0).

• If complex decisions are required, it is the programmer’s job

to reduce them to a series of simple decisions that the

computer can handle.

31

Selection Structure – Problem Examples.

Chapter 3 3.3 Control Structures

Problem 1:

Determine whether

profit, return capital

or loss.

Problem 2:

Determine whether a

number is even or odd.

Problem 4:

Determine whether the speed limit exceeds 110

km per hour. If the speed exceeds 110, then

fine = RM300, otherwise fine = 0. Display fine.

Your cup of tea.

Sugar or no sugar???

32

Chapter 3 3.3 Control Structures

• C provides three types of selection structures in the form of

statements.

_____________________________________________________________________________________________________________________________________

* Paul, D. and Harvey, D. (2013). C How to Program 7/E. United State of America: Pearson Education, Inc., publishing as Prentice Hall

_____________________________________________________________________________________________________________________________________

I Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.

Selective Structures (statement)

Single-selection Double-selection Multiple-selection

if if...else switch

10/25/2020

9

33

(one-choice)

condition

statement

TRUE

FALSE

Chapter 3 3.3 Control Structures

• Pseudo-code:

Requires the use of the keywords if.

Algorithm: one choice

selection

:

n. if condition

n.1 statement

n+1. end_if

:

• If set condition is true,

execute the statement, otherwise false or do

nothing

condition

if selection statement

34

Chapter 3 3.3 Control Structures

_____________________________________________________________________________________________________________________________________

* Paul, D. and Harvey, D. (2013). C How to Program 7/E. United State of America: Pearson Education, Inc., publishing as Prentice Hall

_____________________________________________________________________________________________________________________________________

I Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.

• Example: if

Suppose the passing grade on an exam is 60.

The pseudo-code statement:

Figure: Flowchart of the single-selection if statement

35

Chapter 3 3.3 Control Structures

• Pseudo-code: Requires

the use of the keywords if and else.

if...else selection statement

_____________________________________________________________________________________________________________________________________

* Paul, D. and Harvey, D. (2013). C How to Program 7/E. United State of America: Pearson Education, Inc., publishing as Prentice Hall

_____________________________________________________________________________________________________________________________________

I Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.

Algorithm: two choices

selection

:

n. if condition

n.1 statement_1

n+1. else condition

n+1.1 statement_2

n+2. end_if

:

• If set condition is true, execute

the first statement, else execute

second statement

(two-choices)

condition

statement_2

FALSE TRUE

condition

statement_1

36

Chapter 3 3.3 Control Structures

_____________________________________________________________________________________________________________________________________

* Paul, D. and Harvey, D. (2013). C How to Program 7/E. United State of America: Pearson Education, Inc., publishing as Prentice Hall

_____________________________________________________________________________________________________________________________________

I Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.

• Example: if...else

Suppose the passing grade on an exam is 60.

The pseudo-code statement:

Figure: Flowchart of the double-selection if...else statement

10/25/2020

10

37

Chapter 3

Determine whether an input number is even or odd. If the number is even, print “This is even

number”. Else, print “This is odd number”.

a) Write the pseudo-code design of the problem.

b) What is the selective structure type of the

problem?

c) Draw the flowchart design of the problem.

(Hint: Use modulus 2 = 0 as even number)

Activity 3.5:

_____________________________________________________________________________________________________________________________________

http://static6.depositphotos.com/1147032/632/v/450/depositphotos_6329864-Thermometer-vector-celsius-fahrenheit.jpg

3.3 Control Structures

38

Chapter 3

Solution 3.5:

1. Start

2. Read n

3. if n modulus 2 = 0

3.1Print “This is an even number”

4. else

4.1Print “This is an odd number”

5. End if

6. End

Start

Get n

n % 2 == 0?

Print “this is an even number”

End

True False

Print “this is an odd number”

b) Double selection

3.3 Control Structures

a)

c)

n-number

39

Chapter 3 3.3 Control Structures

• The control statements can be combined in only two ways:

1) Stacking: connecting control statement in sequence.

2) Nesting:

• a connecting control statement can be within

another control statement.

Chapter 3 3.3 Control Structures

_____________________________________________________________________________________________________________________________________

* Paul, D. and Harvey, D. (2013). C How to Program 7/E. United State of America: Pearson Education, Inc., publishing as Prentice Hall

_____________________________________________________________________________________________________________________________________

I Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.

40

Stacked if

Chapter 3 3.3 Control Structures

Age > 21?

Print “Adult”

True

False

Print “kid/teenager”

Gender == ‘F’ ?

Print “Male”

False

True

Print “Female”

10/25/2020

11

41

it is an “one-choice” if

Chapter 3 3.3 Control Structures

• if within if

• Pseudo-code:

Nested if

Algorithm: nested if

:

n.if condition

:

n.m if condition

n.m.1 statement

:

n+1. end_if

:

test1

test2

statement

° FALSE

FALSE

TRUE

TRUE

Chapter 3 3.3 Control Structures

42

Chapter 3 3.3 Control Structures

• Example: Nested if

Print “Excellent” for the grades on an exam equal to 100 for

the passed students. Suppose the passing grade is 60.

The pseudo-code statement:

n. If student’s grade is greater than or

equal to 60

n.1 If student’s grade is equal to

100

n,1,1 Print “Excellent”

Grade≥60

Grade=100

Print “Excellent”

FALSE

FALSE

TRUE

TRUE

43

Chapter 3 3.3 Control Structures

• if...else within if

• The pseudo-code statement:

_____________________________________________________________________________________________________________________________________

* Paul, D. and Harvey, D. (2013). C How to Program 7/E. United State of America: Pearson Education, Inc., publishing as Prentice Hall

_____________________________________________________________________________________________________________________________________

I Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.

Nested if...else

Algorithm: nested if

:

n.if condition

:

n.m if condition

n.m.1 statement

:

n+1. end_if

:

44 ____________________________________________________________________________________________________________________________________

_

* Paul, D. and Harvey, D. (2013). C How to Program 7/E. United State of America: Pearson Education, Inc., publishing as Prentice Hall

Considered as one statement

Chapter 3 3.3 Control Structures

Nested if...else

x

statement

condition

statement

statement TRUE

TRUE

FALSE

FALSE

condition

10/25/2020

12

45

Chapter 3 3.3 Control Structures

• Example: Nested if...else

Print A for exam grades greater than or equal to 90,

B for grades greater than or equal to 80 (but less than 90),

C for grades greater than or equal to 70 (but less than 80),

D for grades greater than or equal to 60 (but less than 70), and

F for all other grades.

_____________________________________________________________________________________________________________________________________

* Paul, D. and Harvey, D. (2013). C How to Program 7/E. United State of America: Pearson Education, Inc., publishing as Prentice Hall

_____________________________________________________________________________________________________________________________________

I Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.

46

Chapter 3 3.3 Control Structures

_____________________________________________________________________________________________________________________________________

* Paul, D. and Harvey, D. (2013). C How to Program 7/E. United State of America: Pearson Education, Inc., publishing as Prentice Hall

_____________________________________________________________________________________________________________________________________

I Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.

• The pseudo-code statement:

47

n. If student’s grade >= 90

n.1 print “A”

n+1. else if student’s grade >= 80

n+1.1 print “B”

n+2. else if student’s grade >= 70

n+2.1 print “C”

n+3. else if student’s grade >= 60

n+3.1 print “D”

n+4. else

n+4.1 print “F”

48

Chapter 3 3.3 Control Structures

_____________________________________________________________________________________________________________________________________

* Paul, D. and Harvey, D. (2013). C How to Program 7/E. United State of America: Pearson Education, Inc., publishing as Prentice Hall

_____________________________________________________________________________________________________________________________________

I Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.

Activity 3.6a:

Print A for exam grades greater than or equal to 90,

B for grades greater than or equal to 80 (but less than 90),

C for grades greater than or equal to 70 (but less than 80),

D for grades greater than or equal to 60 (but less than 70),

and F for all other grades.

Draw the flowchart design of the problem.

10/25/2020

13

49

Chapter 3

Solution 3.6a:

3.3 Control Structures

grade≥90

TRUE

FALSE

grade≥80

FALSE

Print ”A”

TRUE

Print “B”

Print”F”

Grade≥60

FALSE

TRUE

Print “D”

grade≥70

FALSE

TRUE

Print “C”

50

Chapter 3 3.3 Control Structures

_____________________________________________________________________________________________________________________________________

I Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.

Activity 3.6b:

Based on Exercise 3.6a, redraw the flowchart design of

the problem by comparing the smaller value of grade and

print F.

51

Chapter 3

Solution 3.6b:

3.3 Control Structures

Grade<60

TRUE

FALSE

Grade<70

FALSE

Print “F”

TRUE

Print “D”

Print”A”

Grade<90

FALSE

TRUE

Print “B”

Grade<80

FALSE

TRUE

Print “C”

52

Relational Operators

• Used to compare numbers

to determine relative order

• Operators:

> Greater than

< Less than

>= Greater than or equal to

<= Less than or equal to

== Equal to

!= Not equal to

Relational Expressions

• Boolean expressions – true or false

• Examples:

12 > 5 is true

7 <= 5 is false

if x is 10, then

x == 10 is true

x != 8 is true

x == 8 is false

Chapter 3 3.3 Control Structures

10/25/2020

14

53

Logical Operators

• Used to create relational expressions from other relational

expressions

• Operators, meaning, and explanation:

&& AND New relational expression is true if both

expressions are true

|| OR New relational expression is true if

either expression is true

! NOT Reverses the value of an expression – true expression becomes false, and

false becomes true

Chapter 3 3.3 Control Structures

54

Examples: Logical operators

int x =12, y = 5, z = -4;

(x > y) && (y > z) true

(x > y) && (z > y) false

(x <= z) || (y == z) false

(x <= z) || (y != z) ?

!(x >= z) ?

(x <= z) || (y != z) true

!(x >= z) false

Chapter 3 3.3 Control Structures

55

Start

Stop

Read Num

Print

"Category A"

Yes

Num>0? No

Print

"Category B"

Category A

Trace the selection structure in the the following

flowchart. a) What is the output when the input, Num = 10?

b) What is the output when the input, Num = 0?

c) How to correct the flowchart?

Chapter 3 3.3 Control Structures

Activity 3.7:

56

Chapter 3 3.3 Control Structures

Solution 3.7:

a) Category A

b) Category B Category A

b) Corrected flowchart

Start

Stop

Read Num

Print

"Category A"

Yes

Num>0? No

Print

"Category B"

10/25/2020

15

57

Chapter 3 3.3 Control Structures

(c) Repetition Structure

• Most programs involve repetition / looping

• A loop specifies a block of one or more statements that are

repeatedly executed until a condition is satisfied.

• Usually the loop has two important parts:

1. An expression that is tested for a true / false,

2. A statement or block that is repeated as long as the expression is true

• Control statements: while, do...while

for, repeat...until

58

Chapter 3 3.3 Control Structures

• Two means of repetition / looping:

Repetition Structures (statement)

Counter-controlled Repetition

Sentinel-controlled Repetition

_____________________________________________________________________________________________________________________________________

I Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.

repeat...until

• 2 styles of repetition / looping:

59

Chapter 3 3.3 Control Structures

Counter-controlled repetition:

• Known in advance exactly how many times the loop will

be executed.

• A control variable is used to count the number of

repetitions.

o Must be initialized before entering loop

o It will increment or decrement each time a loop

repeats

_____________________________________________________________________________________________________________________________________

I Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.

60

x

initialization

condition

body of loop

increment

y

FALSE

TRUE

cnt=0

cnt<5

cnt=cnt+1

FALSE

TRUE

Start

Print “C++”

End

Chapter 3 3.3 Control Structures

Figure: (a) Flowchart of counter-controlled repetition with increment

counter (b) Example

(a) (b)

10/25/2020

16

61

Chapter 3 3.3 Control Structures

_____________________________________________________________________________________________________________________________________

I Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.

Sentinel-controlled

repetition:

• Unknown in advance

exactly how many times the

loop will be executed.

• Use sentinel values to

control repetition (indicate

end of the data).

Algorithm: Loop control

by sentinel value

1. Start

2. Set repeat = 1

3. while (repeat == 1)

3.1 Read no1

3.2 Read no2

3.4 Print no1 + no2

3.5 Read repeat

4. end_while

5. End

• Pseudo-code:

62

Chapter 3 3.3 Control Structures

Pre-test loop

Pseudo code: requires the use of the keywords while or

for

Algorithm: one choice

selection

:

n. while condition

n.1

statement

:

n+1. end_while

:

• while a set condition is true,

repeat statement (body of loop)

(one choice)

condition

body of loop

TRUE

FALSE

condition

63

Chapter 3 3.3 Control Structures

Post-test loop

Pseudo code: requires the use of the keywords do...while or

repeat...until

Algorithm: one choice

selection

:

n. do

n.1

statement

:

n+1. while condition

:

• statement (body of loop)

will be repeated while a set condition is true

_____________________________________________________________________________________________________________________________________

I Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.

statement(s)

64

Letting the user control a loop

• Program can be written so that

user input determines loop

repetition

• Used when program processes

a list of items, and user knows

the number of items

• User is prompted before loop.

Their input is used to control

number of repetitions

Chapter 3 3.3 Control Structures

cnt=0

cnt<limit

cnt=cnt+1

FALSE

TRUE

Start

Print “C++”

End

Get limit

10/25/2020

17

65

Activity : trace output

What is the Output of the following flowchart when the input is Num= 4

Start

Stop

Print

Result

Result=Result + Count

Count=Count - 1

Initialize

Result=0

Count=Num

Count>0?

Read Num

No

Print Count

Yes

Variables (in memory):

Chapter 3 3.3 Control Structures

Num Result Count

Output on the screen 66

Activity : trace output

What is the Output of the following flowchart when the input is Num= 4

Start

Stop

Print

Result

Result=Result + Count

Count=Count - 1

Initialize

Result=0

Count=Num

Count>0?

Read Num

No

Print Count

Yes

Input:

Num <- 4

Enter a Number => 4 Count = 4

4 > 0 ? => YES

Count: 4

Count: 3

Count = 3

3 > 0 ? => YES

Count: 2

Count = 2

2 > 0 ? => YES

Count: 1

Count = 1

1 > 0 ? => YES

Variables (in memory):

Count: 0

Count = 0

0 > 0 ? => NO

Result: 10

Chapter 3 3.3 Control Structures

Output on the screen

Num Result Count Num Result Count

4

Num Result Count

4 0 4

Num Result Count

4 0 4

4 3

Num Result Count

4 0 4

4 3

7 2

Num Result Count

4 0 4

4 3

7 2

9 1

Num Result Count

4 0 4

4 3

7 2

9 1

10 0

67

3.1 Introduction

3.2 Development Algorithms

3.3 Control Structures

3.4 Top-Down Design

3.5 Summary

Chapter 3 Contents

68

Chapter 3

Introduction

3.4 Top-Down Design

• Structure chart / module chart / hierarchy chart is a graphic

depiction of the decomposition of a problem.

o Illustrates the partitioning of a problem into sub-problems

and shows the hierarchical relationships among the parts.

• It is a tool to aid in software design - aid the programmer in

dividing and conquering a large software problem, that is,

recursively breaking a problem down into parts that are small

enough to be understood by a human brain.

• The process is called top-down design, or functional

decomposition.

10/25/2020

18

69

• NOT a flowchart.

• It has nothing to do with the logical sequence of tasks.

• It does NOT show the order in which tasks are performed.

• It does NOT illustrate an algorithm

Chapter 3 3.4 Top-Down Design

70

Structured software follows rules:

① Modules are arranged hierarchically.

② There is only one root (i.e., top level) module.

③ Execution begins with the root module.

④ Program control must enter a module at its entry point

and leave at its exit point.

⑤ Control returns to the calling module when the lower

level module completes execution.

Chapter 3 3.4 Top-Down Design

71

Top- a box

representing the

entire problem

Bottom- a number of boxes

representing the less

complicated sub-problems

Left-right on the chart is irrelevant

Chapter 3 3.4 Top-Down Design

72

When designing structured software or program,

THREE basic constructs are represented :

1) Sequence - items are executed from top to bottom.

2) Repetition - a set of operations is repeated.

3) Condition - a set of operations are executed only if a certain condition or CASE statement applies.

Chapter 3 3.4 Top-Down Design

10/25/2020

19

73

_____________________________________________________________________________________________________________________________________

http://faculty.simpson.edu/lydia.sinapova/www/cmsc150/cmsc150-02/Assignments/HW07-Fig01.jpg

Chapter 3 3.4 Top-Down Design

Figure: Example of ATM Structure chart

75

3.1 Introduction

3.2 Development Algorithms

3.3 Control Structures

3.4 Top-Down Design

3.5 Summary

Chapter 3 Contents

76

Chapter 3 3.4 Summary

_____________________________________________________________________________________________________________________________________

Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education

• Before writing a program to solve a particular problem, we

must have a thorough understanding of the problem and

carefully planned a solution approach to solve it.

• A solution to any computing problem involves executing a

series of actions in specific order called algorithm.

• We have learnt that structured programming produces

program that:

• easier to understand,

• easier to test, debug and modify, and

• even prove correct in mathematical sense.

77

Chapter 3 3.4 Summary

_____________________________________________________________________________________________________________________________________

Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education

• Three forms of control statement are needed:

• Sequence

• Selection

• Repetition

• All these control statements can be combined in only two

ways:

• Stacking

• Nesting

10/25/2020

20

78

Chapter 3 3.4 Summary

_____________________________________________________________________________________________________________________________________

Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education. pg. 167

79

Self-Reviews

Chapter 3

80

Chapter 3 Self-Review

Fill in the blanks in each of the following questions.

a) A procedure for solving a problem in terms of the

actions to be executed and the order of the

execution is called __________.

b) All programs can be written in terms of three

types of control statement including

__________, __________ and __________.

c) When it is unknown in advance how many times

a set of statements will be repeated, a(n)

__________ value can be used to terminate the

repetition.

Exercise 3.1:

81

Chapter 3 Self-Review

Fill in the blanks in each of the following questions.

a) A procedure for solving a problem in terms of the

actions to be executed and the order of the

execution is called algorithm.

b) All programs can be written in terms of three

types of control statement including

sequence, selection and repetition.

c) When it is unknown in advance how many times

a set of statements will be repeated, a(n)

sentinel value can be used to terminate the

repetition.

Solution 3.1:

10/25/2020

21

82

Chapter 3 Self-Review

A class of ten students took a quiz. The grades

(integers in the range 0 to 100) for this quiz are

available to you. Determine and display the class

average on the quiz.

a) What is the type of control repetition?

b) Write the pseudo-code to solve the problem.

c) Draw the flowchart of the solution.

Exercise 3.2:

_____________________________________________________________________________________________________________________________________

Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education. pg. 81

83

Chapter 3 Self-Review

Solution 3.2:

_____________________________________________________________________________________________________________________________________

Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education. pg. 85

a) counter-controlled repetition

b) Pseudo-code:

84

Chapter 3 Self-Review

Solution 3.2:

_____________________________________________________________________________________________________________________________________

Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education. pg. 108

c) Flowchart: cnt=0

Total=0

Cnt < num

Total = total + G

cnt=cnt+1

FALSE

TRUE

Start

Get G

End

Average = total/cnt

Average

Get num

86

Chapter 3 Self-Review

A class of students took a quiz. Some of the grades

(integers in the range 0 to 100) for this quiz are

available to you. Determine and, display the class

average with number of students on the quiz.

a) What is the type of control repetition?

b) Write the pseudo-code to solve the problem.

c) Draw the flowchart of the solution.

Exercise 3.3:

_____________________________________________________________________________________________________________________________________

Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education. pg. 108

10/25/2020

22

87

Chapter 3 Self-Review

Solution 3.3:

_____________________________________________________________________________________________________________________________________

Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education. pg. 85

a) sentinel-controlled repetition

b) Pseudo-code:

88

Chapter 3 Self-Review

Solution 3.3:

_____________________________________________________________________________________________________________________________________

Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education. pg. 108

c) Flowchart:

cnt=0

Total=0

G != 999

Total = total + G

cnt=cnt+1

FALSE

TRUE

Start

Get G

End

Average = total/cnt

Average

cnt=0

Total=0

Repeat ==1

Total = total + G

cnt=cnt+1

FALSE

TRUE

Start

Get

Repeat

End

Average = total/cnt

Average

Get G

Get G

Get

Repeat


Recommended