Professional Documents
Culture Documents
Problem Solving
Topic & Structure of the lesson
Design
Algorithmic problem:
Any problem whose solution can be expressed as a
set of executable instructions.
Algorithm:
A well defined computational procedure consisting of
a set of instructions, that takes some value or set of
values, as input, and produces some value or set of
values, as output.
Each step of an algorithm must be exact,
preciously and ambiguously described.
It must terminate, i.e. it contains a finite
number of steps.
It must be effective, i.e.., produce the correct
output.
It must be general, i.e.. to solve every
instance of the problem.
Processing
5 10
5 + 10 = 15
15
Input
Output
Let us assume we are interested in calculating the sum of 5
and 10.
PSPD Using C Slide 5 of 40
Problem Solving
Design
The word variable comes from the word vary, which means
that whatever you place within a variable can be changed.
Value1 Sum
Value2
+ addition
- subtraction
* multiplication
/ division
= assignment
() brackets for grouping calculations
A Pseudocode language is semiformal, English-
like language with a limited vocabulary that can be
used to design and describe algorithms.
The Sequence Control Structure:
The sequence control structure is a series of steps
or statements that are executed in the order in which
they are written in an algorithm.
For Example:
read taxable income
read filing status
compute income tax
IF (expression) THEN
:
:
ENDIF
Statements are
placed here.
PSPD Using C Slide 17 of 40
Decision Making
Design
Example:-
We are looking for a job which pays more than
RM4000.
Example of an
Expression
IF (Salary>4000) THEN
Say "I Will Take The Job!!"
ENDIF
For Example:
if a is greater than b then
print “A is greater”
else
print “B is greater”
end if
DOWHILE...ENDDO
FOR…NEXT
REPEAT...UNTIL
REPEAT
:
:
:
UNTIL (expression)
Group of An expression which determines
statements whether the loop will continue.
PSPD Using C Slide 33 of 40
Looping Constructs
Design
Example:
Dowhile (income is less than 50000)
print “Enter taxable income;should be
greater than or equal to 50000”
read income
Enddo
Another technique used in designing and representing
algorithms.
Alternative to pseudocoing
A pseudocode description is verbal, a flowchart is
graphical in nature.
Definition:
A flowchart is a graph consisting of geometrical shapes
that are connected by flow lines.
Pseudocode: Flowchart:
statement_1
Statement -1
statement_2
------------ Statement -2
statement_n
Statement -n
Pseudocode: Flowchart:
if condition
then-part
else false true
condition
else-part
end_if
else-part then-part
Pseudocode:
Flowchart:
if condition
Y
then-part true
end_if condition
N
then-part
false
Pseudocode: Flowchart:
while condition
loop-body
end-while T
Y
condition loop-body
F
N
• Problem Solving
• Pseudocodes
• Flowcharts
• input x
• while x <> 0 do
• if x = 1 then print “error”
• else n = (x * x)/(1 – x)
• print n, x
• endif
• input x
• endwhile
• total1 = 0: total2 = 0
• for days = 1 to 100
• input temperature
• if temperature < 20 then total1 = total1 + 1
• else total2 = total2 + 1
• endif
• next
• print total1, total2
PSPD Using C Slide 52 of 40
Writing algorithms using pseudocode
Design
• case temperature of
• 1: total1 = total1 + 1
• 2: total2 = total2 + 1
• endcase
• repeat
• input code
• if code > 999 and code < 2000 then books = books + 1
• else if code > 1999 and code < 3000 then maps = maps + 1
• else if code > 2999 and code < 4000 then mags = mags + 1
• else print “error in input”
• endif:endif:endif
• until code = 9999
• print books, maps, mags
• repeat
• input code
• x = INT(code/1000) * divides code by 1000 to give a
• case x of * number between 0 and 9
• 1: books = books + 1
• 2: maps = maps + 1
• 3: mags = mags + 1
• otherwise print “error”
• endcase
• until code = 9999
• print books, maps, mags