Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 34

Lecture 6:

PROBLEM SOLVING TECHNIQUES

1
Lecture 6 Objectives

• Building Pseudo-code and Flowcharts that


require the use of control structures

• Solve examples
Lecture 6 Contents

6.1 Introduction
6.2 Control Structures
6.3 Summary
Lecture 6 6.1 Introduction

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


understand the problem and plan the solution.

• Therefore, a solving technique or algorithm is needed to


write a computer program.

• Note: One problem can have more than one solution i.e.
algorithms.

____________________________________________________________________________________________________________________________________

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

• The solution to any problem involves a series of actions 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.

• Algorithm can be developed using


• Pseudo-code
• Flowchart

____________________________________________________________________________________________________________________________________

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

6.1 Introduction
6.2 Control Structures
6.3 Summary
Lecture 6 6.2 Control Structures
Introduction

• Normally, statements in program are in sequential execution.


• However, a program may require any of the three control
structures to describe its flow of execution:

Control Structures

Sequential Structure Selective Structure Repetition Structure

____________________________________________________________________________________________________________________________________
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
Lecture 6 6.2 Control Structures
(a) Sequential Structure

•A series of steps or
statements performed in the 1. Start
order they are written in an 2. Statement_1
algorithm. 3. Statement_2
4. Statement_3
•Pseudo-code: mark the 
beginning and end of a block n. Statement_n-1
of statements. n+1. End
Lecture 6 6.2 Control Structures

•Multiple statements considered as one statement.

Statement simply means


command or instruction
statement_1

statement_2  statement

Figure: Flowchart in sequential structure


Lecture 6 6.2 Control Structures
(b) Selection Structure

•Selection allows you to choose between two or more


alternatives; i.e. 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.
Lecture 6 6.2 Control Structures

Selection Structure – Problem Examples.


Problem 4:
Problem 1:
Determine whether the speed limit exceeds 110
Determine whether
km per hour. If the speed exceeds 110, then
profit, return capital
fine = PKR300, otherwise fine = 0. Display fine.
or loss.
Proble
m
Problem 2: Determ 5:
ine wh
Determine whether a is abo ether t
ve 12
number is even or odd. age is years he age
ab old.
= PKR ove 12, the If the
20 n
PKR10 0, otherwis ticket
: 0. Disp e ticke
em 3 t he r lay ticke t=
b l he t .
Pro mine w ss than Your cup of tea.
r
Dete arks is le s than Sugar or no sugar???
m s
the If it is le fail”,
. “
60% en print “pass”.
h t
60, t ise prin
rw
othe
Lecture 6 6.2 Control Structures

Relational Operators Relational Expressions


•Used to compare numbers to
determine relative order • Boolean expressions – true
or false
•Operators:
• Examples:

12 > 5 is true
> Greater than
< Less than 7 <= 5 is false
>= Greater than or equal to
if x is 10, then
<= Less than or equal to
x == 10 is true
== Equal to
x != 8 is true
!= Not equal to
x == 8 is
false
Lecture 6 6.2 Control Structures

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


statements.

Selective Structures
(statement)

Single-selection Double-selection Multiple-selection

if if...else switch

_____________________________________________________________________________________________________________________________________
____________________________________________________________________________________________________________________________________

* Paul, D. and Harvey, D. (2013).


I C How
Deitel, to Program
H.M. 7/E.
and Deitel, P.JUnited
(2013).State of America:
C How Pearson
to Program Education,
7/E. United StateInc., publishing
of America: as Prentice
Pearson Hall
Education.
Lecture 6 6.2 Control Structures

if selection statement

• Pseudo-code: • If set condition is true,


Requires the execute the statement,
use of the otherwise false or do
keywords if. nothing

Algorithm: one choice (one-choice)


selection

: TRUE
n. if condition condition
condition
n.1 statement FALSE
n+1. end_if
: statement
Lecture 6 6.2 Control Structures

• Example: if
Suppose the passing grade on an exam is 60.
The pseudo-code statement:

Figure: Flowchart of the single-selection if statement


_____________________________________________________________________________________________________________________________________
____________________________________________________________________________________________________________________________________

* Paul, D. and Harvey, D. (2013).


I C How
Deitel, to Program
H.M. 7/E.
and Deitel, P.JUnited
(2013).State of America:
C How Pearson
to Program Education,
7/E. United StateInc., publishing
of America: as Prentice
Pearson Hall
Education.
Lecture 6 6.2 Control Structures

if...else selection statement

• Pseudo-code: Requires • If set condition is true, execute


the use of the keywords the first statement, else execute
if and else. second statement

Algorithm: two choices (two-choices)


selection
TRUE FALSE
: condition
condition
n. if condition
n.1 statement_1
n+1. else condition statement_1 statement_2
n+1.1 statement_2
n+2. end_if
:

_____________________________________________________________________________________________________________________________________
____________________________________________________________________________________________________________________________________

* Paul, D. and Harvey, D. (2013).


I C How
Deitel, to Program
H.M. 7/E.
and Deitel, P.JUnited
(2013).State of America:
C How Pearson
to Program Education,
7/E. United StateInc., publishing
of America: as Prentice
Pearson Hall
Education.
Lecture 6 6.2 Control Structures

• 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


_____________________________________________________________________________________________________________________________________
____________________________________________________________________________________________________________________________________

* Paul, D. and Harvey, D. (2013).


I C How
Deitel, to Program
H.M. 7/E.
and Deitel, P.JUnited
(2013).State of America:
C How Pearson
to Program Education,
7/E. United StateInc., publishing
of America: as Prentice
Pearson Hall
Education.
Chapter
Lecture 63 6.2 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.

_____________________________________________________________________________________________________________________________________
____________________________________________________________________________________________________________________________________

* Paul, D. and Harvey, D. (2013).


I C How
Deitel, to Program
H.M. 7/E.
and Deitel, P.JUnited
(2013).State of America:
C How Pearson
to Program Education,
7/E. United StateInc., publishing
of America: as Prentice
Pearson Hall
Education.
Lecture 6 6.2 Control Structures

Stacked if

False
Age > 18?

True Print “kid”


Print
“Adult”

Gender == True
‘F’ ?

False Print “Female”


Print
“Male”
Chapter
Lecture 63 6.2 Control Structures

Nested if

• if within if FALSE
• Pseudo-code: test1
TRUE

Algorithm: nested if FALSE °


: test2
n.if condition
TRUE
:
n.m if condition statement
n.m.1 statement
:
n+1. end_if
: it is an
“one-choice” if
Lecture 6 6.2 Control Structures

• Example:
Lecture 6 6.2 Control Structures

Nested if...else
• if...else within if
• The pseudo-code statement:

Algorithm: nested if
:
n.if condition
:
n.m if condition
n.m.1 statement
:
n+1. end_if
:

_____________________________________________________________________________________________________________________________________
_____________________________________________________________________________________________________________________________________

* Paul, D. and Harvey, D. (2013).


I C How
Deitel, to Program
H.M. 7/E.
and Deitel, P.JUnited
(2013).State of America:
C How Pearson
to Program Education,
7/E. United StateInc., publishing
of America: as Prentice
Pearson Hall
Education.
Lecture 6 6.2 Control Structures

Nested if...else
x

FALSE
condition
TRUE

statement

statement TRUE Considered as


condition one statement
FALSE
statement

____________________________________________________________________________________________________________________________________

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

• Example: Nested if...else

_____________________________________________________________________________________________________________________________________
_____________________________________________________________________________________________________________________________________

* Paul, D. and Harvey, D. (2013).


I C How
Deitel, to Program
H.M. 7/E.
and Deitel, P.JUnited
(2013).State of America:
C How Pearson
to Program Education,
7/E. United StateInc., publishing
of America: as Prentice
Pearson Hall
Education.
Lecture 6 6.2 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).


I C How
Deitel, to Program
H.M. 7/E.
and Deitel, P.JUnited
(2013).State of America:
C How Pearson
to Program Education,
7/E. United StateInc., publishing
of America: as Prentice
Pearson Hall
Education.
Lecture 6 6.2 Control Structures

• The pseudo-code of the algorithm:

_____________________________________________________________________________________________________________________________________
____________________________________________________________________________________________________________________________________

* Paul, D. and Harvey, D. (2013).


I C How
Deitel, to Program
H.M. 7/E.
and Deitel, P.JUnited
(2013).State of America:
C How Pearson
to Program Education,
7/E. United StateInc., publishing
of America: as Prentice
Pearson Hall
Education.
Lecture 6 6.2 Control Structures

Activity 6.1:

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 of the algorithm.

_____________________________________________________________________________________________________________________________________
____________________________________________________________________________________________________________________________________

* Paul, D. and Harvey, D. (2013).


I C How
Deitel, to Program
H.M. 7/E.
and Deitel, P.JUnited
(2013).State of America:
C How Pearson
to Program Education,
7/E. United StateInc., publishing
of America: as Prentice
Pearson Hall
Education.
Lecture 6 3.3 Control Structures
TRUE
grade≥90 Print ”A”
Solution 6.1:
FALSE

TRUE
grade≥80 Print “B”

FALSE

TRUE
grade≥70 Print “C”

FALSE

TRUE
Grade≥60 Print “D”

FALSE

Print”F”
Lecture 6 6.2 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
Lecture 6 6.2 Control Structures

x Start

initialization cnt=0

FALSE
FALSE
condition cnt<5
TRUE TRUE

body of loop Print “C++”

increment cnt=cnt+1

y End
(a) (b)

Figure: (a) Flowchart of repetition structure with example


Lecture 6 6.2 Control Structures
(c) Repetition Structure

• Example Loop
Lecture 6 Contents

6.1 Introduction
6.2 Control Structures
6.3 Summary
Lecture 6 6.3 Summary

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

• The solution to any problem involves executing a series of


actions in specific order called algorithm.

• Algorithm can be described using Pseudo-code or


Flowchart

____________________________________________________________________________________________________________________________________

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

• Three forms of control statement are needed:


• Sequence
• Selection
• Repetition

• Selection control statements can be combined in two ways:


• Stacking
• Nesting

____________________________________________________________________________________________________________________________________

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

You might also like