0% found this document useful (0 votes)
13 views

Topic 2 - Problem Solving Concepts

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Topic 2 - Problem Solving Concepts

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 47

PROBLEM

SOLVING
CONCEPT
Topic 2
S
1
1) To describe nature of
problems:
◦Concrete & Abstract

2) To learn about the types


of solution for a problem
◦Algorithmic & Heuristic
OBJECTIVE Solutions
S
3) To learn the steps
involved in building a
program

4) To learn problem solving


through algorithm
◦pseudo code & flow chart

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

• Abstract: they have no particular values to


be used in making the solution:
 Compute the area of a circle with a given radius

 Calculate the monthly payment for a given loan


amount with a specific loan duration and interest
rate.

• It requires an abstract/general solution which


can solve several problems when concrete
information is given.
6
ANALOGY: TO CALCULATE THE
AREA OF 3 DIFFERENT
RECTANGLES WITH GIVEN VALUES
4cm 8cm 5cm

2cm 2cm 3cm

Rect 1 Rect 2 Rect 3


Area = 2 * Area = 2 * Area = 3 *
4 8 5
__________ __________ __________
__________ __________ __________

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

 Well-defined steps/instructions for


carrying out a particular task .
 E.g. Calculating your bank account
balance
 Calculating the area of a circle
 Calculating total salary

 The solution will be the same each time


the algorithm is followed.

 Most computers use algorithmic types


of problem 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.

 A heuristic solution tells you how to


discover the instructions for yourself, or
at least where to look for them

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.

 The analysis starts by answering these questions:


 What would be considered the input data?
 What would be considered the output information?
 What are the formulas/processes you have to create to solve
this solution?
 Are there any special conditions/constraints?

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)

Input?  length, width

Process?  Perimeter = 2 x (length + width)

Output?  Perimeter
16
STEPS IN BUILDING A
PROGRAM
2. Develop an Algorithm

 Write the algorithm in pseudocode or


flowchart.

 Need to identify the basic steps clearly in the


correct order from what you found in the
analysis step.

 Typically, the steps follows similar pattern:


input, process and output.
50
 An algorithm is a clear step–
by-step sequence of
instructions to solve a problem

 Analogy: recipe (refer next

ALGORI slide).

THM  In computing, an algorithm is a


procedure (a finite set of a
well-defined instructions) for
accomplishing some task
which, given an initial state,
will terminate in a defined end-
state.

54
ALGORITH
M
 Baking a Cake

55
 2 ways to represent
algorithm:
a) Pseudo code

ALGORI using English-like statements


to list the steps to be taken
in the solution.
THM
b) Flowchart
using diagrams that employ
symbols to describe the
workflow of steps involved in
the solution.

56
 Pseudocode statements usually
perform input, output, and
processing.

 The input statements are to get


data needed to perform
computation e.g.:

PSEUDO
◦ READ studentName, studentMark
◦ GET length, width, height of a box

CODE
◦ OBTAIN loan_amount, interest_rate,
loan_duration

 The output statements are to


display information for the user
e.g.:
◦ DISPLAY finalGrade
◦ PRINT "The volume of the box is "
+theVolume
◦ PRINT "The loan monthly payment "
+monthlyPayment
57
 The processing statements
are to perform computation
e.g.:
o CALCULATE priceAverage AS
(price1 +price2 + price3) / 3
o COMPUTE volume AS length x

PSEUDO width x height


o DETERMINE BMI AS weight /

CODE (height x height)

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.

 The basic elements are as


follows:
FLOWCH ◦ Rounded Rectangle (start/end of a
program)
ART ◦ Parallelogram (program input and
output)

◦ Rectangle (processing)

◦ Diamond (decision)

◦ Arrow (execution flow) 60


FLOWCHART TO CALCULATE
THE PERIMETER OF A
RECTANGLE
Start

READ length,
width

Perimeter = 2 x
(length + width)

DISPLAY
perimeter

End
61
STEPS IN BUILDING A
PROGRAM
3. Test an Algorithm:

 Pretend that you are the computer and follow the


steps of your algorithm explicitly while keeping
track of how the variables are changing.

 Hand trace the values of the variables in every


steps of your algorithm to verify its correctness.

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:

 Develop the program by coding the algorithm in a


programming language, such as Java.

 Need to have knowledge in the programming


language in terms of the language syntax.

 Can use IDEs such as Eclipse, NetBeans, JGRASP,


etc to facilitate the coding tasks.

52
HELLO WORLD
PSEUDOCODE & JAVA
CODE
Start
PRINT “Hello World!”
End Start

public class Hello PRINT


“Hello
{ World”
public static void main(String[] args)
End
{
// display a greeting in the console
window
System.out.println("Hello
World!");
}
} 62
ALGORITHM & JAVA CODE
TO CALCULATE THE
PERIMETER OF A
Start RECTANGLE
READ length, width Start

CALCULATE perimeter = 2 x (length + width)


DISPLAY perimeter
End Input length,
width

public class CalculatePerimeter


{
public static void main(String[] args) Perimeter = 2 x
(length + width)
{
Scanner scan = new Scanner (System.in);
System.out.println("Insert the length");
int length = scan.nextInt(); Output
System.out.println("Insert the width"); perimeter
int width = scan.nextInt();
int perimeter = 2 * (length + width);

System.out.println("The perimeter of End


rectangle is"+perimeter); 59
}
STEPS IN BUILDING A
PROGRAM
5. Test/Debug Program:

 Test to verify that the program meets the


requirements by creating test cases. (Compare the
hand-traced value with the output from the code)

 Test can avoid bugs such as logic errors.

 Program also need to be maintained by modifying it


if the problem domain or requirements changes or
problem arises.

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.

The formula used to calculate


the carry marks is:
carryMarks = midSem +
assignment + quiz

33
EXERCISE 2
 Write pseudo code and draw a
flowchart for a program that:

reads the length, width and


height of a box, calculate and
display its volume. The volume
is calculated by multiplying the
length, width and height .

34
EXERCISE 3
 Write pseudo code and draw a
flowchart for a program that:

calculates and displays a circle


area based on its radius. The
formula to calculate area is πr2
where r = radius and π = 3.142.

35
EXERCISE 4
 Write pseudo code and draw a
flowchart for a program that:

asks the user to input an


amount in US Dollar (USD), read
it and then covert to Malaysian
Ringgit (RM). Assume that
USD1 is equivalent to RM4.20.
Display RM value calculated by
your program.

36
EXERCISE 5
 Write pseudo code and draw a
flowchart for a program that:

reads three test scores for a


student. Find the average score
achieved for that student.

37
EXERCISE 6
 Write pseudo code and draw a
flowchart for a program that:

calculates the total price needs


to be paid by visitors Legolands
Theme Park. Assume that the
ticket price is RM50 per person
(adult and children).

38
EXERCISE 7
 Write pseudo code and draw a
flowchart for a program that:

calculates the payment for


parking a car in a car park
based on hours parked. Each
hour is charged RM1.50. The
program should calculate and
display the total payment that
needs to be paid when a user
parks the car.

39
EXERCISE 8
 Write pseudo code and draw a
flowchart for a program that:

calculates total payment for


grocery bought by customers. It
reads the name of item, its
price and quantity. Then the
item name and total price for
the item are displayed.

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.

 Write pseudo code and draw a


flowchart
41
EXERCISE 10
 Write pseudo code and draw a
flowchart for a program that:

reads the actual price of a


Bonia handbag and its discount
percentage. It calculates and
print the price of handbag that
needs to be paid.

42
43
ADDITIONAL
EXERCISES
EXERCISE 1
 Write pseudo code and draw a
flowchart for a program that:

computes the sum of two


numbers. If the sum is below or
equal to twenty, two numbers
will be entered again. If the
sum is above 20, it will display
the sum..

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

You might also like