CHAPTER 2
Problem
Solving
Basic concept in computer
system
• Hardware and Software
Basic concept in
programming
• About Programming
• Application in Programming
How to THINK like a
PROGRAMMER?
Programming
is about
problem
solving;
thinking
logically and
breaking down
a problem into
steps to find a
solution
How to breaking down a
problem into steps to find
a solution?
INPU PROCES OUTP
T S UT
Weight, w Calculate BMI
= 50kg BMI = ??
Height, h = BMI =
160cm
Example
1
Calculate Ali’s BMI given his Weight, w =
50kg and Height, h = 160cm
Let’s Test Your
Understanding
Determine a student’s
final grade and
indicate whether it is
passing or failing. The
final grade is
calculated based on
the average of four
marks. IfQuestion:
the average
mark is below than
Define Input, Process and
50,Output
the from
gradethisis FAIL
problem
and vice versa.
HOW TO DESCRIBE THE
PROCESS BEFORE
TRANSLATE INTO
PROGRAMMING
LANGUAGE?
WHAT WILL
YOU LEARN
IN
THIS
CHAPTER?
1 PSEUDOCODE
2 ALGORITHM
3 FLOWCHART
WHAT IS THE
DIFFERENCE?
PSEUDOCODE ALGORITHM FLOWCHART
Informal
high-level Step by step Pictorial
description sequence of representatio
of an solving a n of an
algorithm. given algorithm
Pseudocode problem
is very
similar to
everyday
PSEUDOCODE
&
ALGORITHM
Example
Calculate Ali’s BMI given his Weight, w =
INPU
50kg and Height,PROCES
h = 160cm OUTP
T S UT
Weight, w Calculate BMI
= 50kg BMI = ??
Height, h = BMI =
160cm
Solution
Pseudoco Algorith
de m
1. Input Ali’s Weight (w) 1. STEP 1: Input w
and Height (h) and h
2. Calculate BMI, Weight 2. STEP 2: BMI = w /
(kg) divided by squared h²
Let’s Test Your
Understanding
Write an algorithm to
determine a student’s
final grade and
indicate whether it is
passing or failing. The
final grade is
calculated based on
the average of four
marks. IfQuestion:
the average
mark
Writeis below than
PSEUDOCODE and
50, the grade for
ALGORITHM is this
FAIL
problem
and vice versa.
SOLUTION
PSEUDOCODE
1. Input a set of 4 ALGORITHM
1. STEP 1: M1, M2, M3,
M4
marks
2. Calculate their 2. STEP 2: GRADE =
average by (M1+M2+M3+M4) / 4
summing and 3. STEP 3: If (GRADE <
dividing by 4 50) then
3. If average is below print
50 “FAIL”
4. Print “FAIL” Else
5. Else print
6. Print “PASS” “PASS”
FLOWCHART
&
ALGORITHM
FLOWCHART
• (Dictionary) A pictorial representation of a
sequence of operations in a computer program
• (Technical) A graphical representation of the
sequences of operations in an information
system or program. Information system flowcharts
show how data flows from source documents
through the computer to final distribution to users.
Program flowcharts show the sequence of
instructions in a single program or
subroutine. Different symbols are used to draw
each type of flowchart.
• Shows logic of an algorithm
Name Symbol Use in Flowchart
FLOWCHART Oval Denotes the beginning or end of the program
SYMBOLS Parallelogram Denotes an input operation
Rectangle Denotes a process to be carried out
e.g. addition, subtraction, division etc.
Diamond Denotes a decision (or branch) to be made.
The program should continue along one of
two routes. (e.g. IF/THEN/ELSE)
Hybrid Denotes an output operation
Flow line Denotes the direction of logic flow in the program
Example Solution
Flowchar
Calculate Ali’s Footnote:
Always begin t
BMI given his flowchart with
“Start” and
STA
RT
Weight, w = “End”
INPUT INPUT
50kg and Height, Name
W and
H
h = 160cm flowchart
symbols BMI = w / h² PROCE
Inpu Weight, w involve in this SS
= 50kg problem? PRINT
t Height, h = BMI OUTPU
VALUE T
160cm
Proc Calculate
END
BMI
ess BMI =
Algorith
Out BMI m 1: Input w
1. STEP
put = ?? and h
2. STEP 2: BMI = w
/ h²
Let’s Test Your
Understanding
Write an algorithm to
determine a student’s
final grade and
indicate whether it is
passing or failing. The
final grade is
calculated based on
the average of four
marks. IfQuestion:
the average
markWriteisFLOWCHART
below than and
50, the grade for
ALGORITHM is this
FAIL
problem
and vice versa.
SOLUTION
FLOWCHART ALGORITHM
1. STEP 1: M1, M2, M3, M4
STA
RT
2. STEP 2: GRADE =
(M1+M2+M3+M4) / 4
INPUT
M1, M2, 3. STEP 3: If (GRADE < 50)
M3, M4
GRADE = (M1 +
then
M2 + M3 + print
M4) / 4
“FAIL”
YES NO
GRADE Else
< 50?
print
Print Print “PASS”
“FAIL” “PASS”
END
Exampl
e 1 algorithm and
Write a pseudocode,
draw a flowchart to convert the length
in feet (ft) to centimeter (cm)
PSEUDOCODE ALGORITHM FLOWCHART
1. Input the
length in feet 1. STEP 1: Input L STA
(ft) RT
(ft)
2. STEP 2: Lcm = L
2. Calculate the x 30 INPUT L
(ft)
length in 3. STEP 3: Print Lcm
centimeter Lcm = L x 30
(cm) (multiply
with 30) PRINT
3. Display length Lcm
in centimeter
END
(cm)
Example
2
Write a pseudocode, algorithm and
draw a flowchart that will read a two
sides of a rectangle and calculate its
PSEUDOCODE
area ALGORITHM FLOWCHART
1. Input the 1. STEP 1: Input L
Length (L) and W
STA
RT
and Width (W) 2. STEP 2: A = L x INPUT
2. Calculate the W L and
W
area, (A) of 3. STEP 3: Print A
rectangle A=LxW
(multiply with L
and W) PRINT
A
3. Display area
of rectangle END
Example HINT:
3
Write a pseudocode, algorithm
and draw a flowchart that will ax 2 bx c 0
calculate the roots of a FLOWCHART
PSEUDOCODE ALGORITHM
quadratic equation STA
1. Input RT
coefficients 1. STEP 1: Input INPUT
(a, b, c) of the a, b, c a, b, c
quadratic 2. STEP 2: d = d = sqrt(b x b
– 4 x (a x c))
equation sqrt(b x b – 4
2. Calculate x (a x c)) 𝑥1 =−𝑏+𝑑/2𝑎
3. Calculate 3. STEP 3:
and 𝑥1 =−𝑏−𝑑/2𝑎
4. STEP 4:
4. Display value 5. STEP 5: Print , PRINT
,
and
END