Topic 2 - Problem Solving Concepts
Topic 2 - Problem Solving Concepts
SOLVING
CONCEPT
Topic 2
S
1
1) To describe nature of
problems:
◦Concrete & Abstract
2
The most important skill
for a computer scientist is
problem-solving:
the ability to formulate problems,
think creatively about solutions,
and express a solution clearly and
accurately.
INTRODUCTION
Programming involves
problem solving (design)
translating the solutions into a
computer program using a
programming language (a.k.a .
coding)
3
4
NATURE OF PROBLEM
5
NATURE OF PROBLEM
Concrete
Problem
7
3 different Java
ANALOGY 2: TO CALCULATE
THE AREA OF RECTANGLES
Find general solution:
_______
Rect area = Width * Length
_______
_______
_______
Rect 1 = 2 * 4
1 Java Concrete
program for 3 Rect 2 = 2 * 8 information
rectangles
calculation Rect 3 = 3 * 5
Abstrac
t
Proble
m 8
TYPES OF PROBLEM
SOLUTIONS
Algorithmic Solutions
9
TYPES OF PROBLEM
SOLUTIONS
Heuristic Solutions
◦ Solutions that can’t be reached by
following a direct set of steps.
Created by using reasoning built
upon knowledge and experience and
by trial and error. E.g. :
- Determining which road to take to
reach UUM
- Deciding which group to choose
for a subject
◦ The solution may not be the same
each time the algorithm is executed.
◦ Artificial intelligence deals with
10
heuristic types of problems.
ALGORITHMIC VS
HEURISTIC SOLUTIONS
Algorithmic solution for driving to
someone’s house:
Take the Highway to Kedah. Take the Alor Star Utara
exit and drive 4 miles. Turn left at the grocery store,
and then take the first left. Drive along the way and
you will find the house on the left, at Lot 2.
Heuristic solution for getting to
someone’s house:
Find the last letter we mailed to you. Drive to the town
in the written address. When you get to town, ask
someone where our house is. Everyone knows us—
someone will be glad to help you. If you can’t find
anyone, call us and we’ll come to get you.
11
ALGORITHMIC VS
HEURISTIC SOLUTIONS
An algorithmic solution gives you the
instructions directly.
Algorithmic
Solutions 12
1 2 3 4 5
Analyze the Develop an Test an Code the Test the
problem algorithm algorithm program program
(by
programme
r and the
user)
STEPS IN BUILDING A
PROGRAM
47
STEPS IN BUILDING A
PROGRAM
1. Analyze the Problem
Clearly understand the problem
what input are to be used
the desired output
the procedure that will produce the result
the constraints to be considered.
48
STEPS IN BUILDING A
PROGRAM
1. Analyze the Problem
48
EXAMPLE
Write a JAVA program that will calculate the
perimeter of a rectangle based on the length
and width given by user. The program will
display the calculated perimeter. The perimeter
is calculated by using this formula:
Perimeter = 2 x (length + width)
Output? Perimeter
16
STEPS IN BUILDING A
PROGRAM
2. Develop an Algorithm
ALGORI slide).
54
ALGORITH
M
Baking a Cake
55
2 ways to represent
algorithm:
a) Pseudo code
56
Pseudocode statements usually
perform input, output, and
processing.
PSEUDO
◦ READ studentName, studentMark
◦ GET length, width, height of a box
CODE
◦ OBTAIN loan_amount, interest_rate,
loan_duration
58
PSEUDOCODE TO
CALCULATE THE
PERIMETER OF A
RECTANGLE
Start
READ length, width
CALCULATE perimeter AS 2 x (length +
width)
Display perimeter
End
59
A graphical way of
depicting a problem in terms
of its inputs, outputs, and
processes.
◦ Rectangle (processing)
◦ Diamond (decision)
READ length,
width
Perimeter = 2 x
(length + width)
DISPLAY
perimeter
End
61
STEPS IN BUILDING A
PROGRAM
3. Test an Algorithm:
51
TEST ALGORITHM TO
CALCULATE THE
PERIMETER OF A
RECTANGLE
Start
READ length, width (20,10)
CALCULATE perimeter = 2 x (length +
width) 2 x (20+10)
DISPLAY perimeter 60
End
59
STEPS IN BUILDING A
PROGRAM
4. Code the program:
52
HELLO WORLD
PSEUDOCODE & JAVA
CODE
Start
PRINT “Hello World!”
End Start
53
TODAY’S TAKE
AWAY
Two types of
problems nature
Types of solution
for a problem
Steps in building
a program
32
EXERCISE 1
Write pseudo code and draw
a flowchart for a program
which calculates the carry
marks for a subject.
33
EXERCISE 2
Write pseudo code and draw a
flowchart for a program that:
34
EXERCISE 3
Write pseudo code and draw a
flowchart for a program that:
35
EXERCISE 4
Write pseudo code and draw a
flowchart for a program that:
36
EXERCISE 5
Write pseudo code and draw a
flowchart for a program that:
37
EXERCISE 6
Write pseudo code and draw a
flowchart for a program that:
38
EXERCISE 7
Write pseudo code and draw a
flowchart for a program that:
39
EXERCISE 8
Write pseudo code and draw a
flowchart for a program that:
40
EXERCISE 9
You would like to develop a simple
ATM Machine for withdrawing money.
The user of this ATM Machine should
provide the total balance in the
account before withdrawal and the
total withdrawal amount. At then
end, the ATM machine will display
the total account before withdrawal,
withdrawal amount and balance after
withdrawal.
42
43
ADDITIONAL
EXERCISES
EXERCISE 1
Write pseudo code and draw a
flowchart for a program that:
44
EXERCISE 2
Create an algorithm
(pseudo and a flowchart)
that converts a numeric
grade to a pass/no pass
grade.
45
EXERCISE 3
Create an algorithm
(pseudo and a flowchart)
that will output the largest
number among three
numbers entered by the
user.
46
EXERCISE 4
Create an algorithm
(pseudo and a flowchart)
that compute the GRADE
which is the average of 4
different Marks, then
displays “FAIL” if the
GRADE is less than 50.
Otherwise, “PASS” is
printed.
47