Week 2
Problem Solving Concepts
STIA1113 : Programming I
Lecture Outlines
• Introduction to Problem Solving
• Types of Problems
• Problem Solving with Computers
• Difficulties with Problem Solving
Introduction
• 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.
• Programming involves
– problem solving and
– translating the solutions into a computer program
using a programming language (a.k.a . coding)
• A programmer skill: thinking ability to produce the
solution and knowledge about the programming
language
Nature of problem
• A problem in computer science is the statement of
specifications that requires a solution
• Problem can be concrete or abstract
• Concrete : they have known values to use in making the
solution e.g.:
i) Compute the area of a circle when its radius is 5.5 cm
ii) Ali drove to Abu’s house at 60 km/h. Abu’s house is 80 km away.
Ali arrived at Abu’s house at 3:22 p.m. What time did Ali leave?.
• The solution is designed to solve a single specific problem
Nature of problem
• Abstract : they have no particular values to be
used in
making the solution e.g.:
i. Compute the area of a circle with a given radius
ii. Ali drove to Abu’s house at 60 km/h. Abu’s house
is 80 km away. What time should Ali leave if he
wants to arrive at a certain particular time?.
iii. Calculate the monthly payment for a given loan
amount with a specific loan duration and interest
rate.
• Requires an abstract/general solution which can
solve several problems when concrete
information is given.
Nature of problem
• Abstract problems are the more common
problem (we’ll focus more on solving abstract
problems)
• Abstract problems are the types that computer
scientists are faced with every day
Type of Problem Solutions
• Steps involved in following a set of
instructions.
- Ex. Calculating your checkbook balance
Algorithm • The solution will be the same each time the
Solution algorithm is followed.
• Most computers use
algorithmic types of problems.
• 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.
Heuristic • The results may not produce the same
Solutions results each time the algorithm is
executed.
• Artificial intelligence deals with heuristic
types of problems.
Steps in Problem Solving in
FINIS
Everyday Life H
Identify the Identify Define the
problem alternative ways steps
1 3 5
2 4 6
Understand Select the Your text
the problem best way here
START
6 Steps in Problem Solving in
Everyday Life
1. Identify the problem.
– If you don’t know what the problem is, you
can’t create a solution for it.
– Ex. Calculate Payroll for 10 Employees
– Ex. Provide directions
6 Steps in Problem Solving in
Everyday Life
2. Understand the problem.
– Must understand what is involved in
the problem before finding the
solution
– Perform
• Research - to form your own knowledge
base
• Observations by
– Watching current activity
– Reviewing past activities
• Ex. Giving instructions to someone
on how to find an address.
6 Steps in Problem Solving in
Everyday Life
3. Identify alternative ways to solve the
problem.
– Rarely does a problem have only one solution.
– Look at
• Efficiency
• Speed
• Accuracy Don’t just find one solution and stop there.
• Acceptability Rarely is it the best solution…especially for
novice programmers…
6 Steps in Problem Solving in
Everyday Life
4. Select the best way to solve the problem
from the list of alternative solutions.
– Establish criteria to help when performing
evaluations on each alternative
– Include pros and cons for each solution
– Select the best solution based
on the research
6 Steps in Problem Solving in
Everyday Life
5. Define the steps that enable you
to solve the problem using the
selected solution.
i. Create a numbered, step-by-step
set of instructions
ii. Instructions must be
included in the knowledge base…
So, what’s a knowledge base?
6 Steps in Problem Solving in
Everyday Life
6. Test the solution.
• Are the results accurate?
• Does it satisfy the needs of the
client?
• Is it acceptable to the client?
• Does the solution solve the
original problem?
Problem Solving with Computers
Definitions:
Solution instructions followed to
produce best result (in step 5)
Result outcome, computer-assisted
answer
Program instructions for solution coded
using a computer language
Steps in Building a Program
01 Analyze the problem.
02 Develop an algorithm.
03 Write the program or coding.
04 Test/ debug the program.
Steps in Building a Program
1. Analyze the problem
Must clearly understand the problem,
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?
Steps in Building a Program
2. Develop an algorithm
Write the algorithm in pseudocode or flowchart
• need to identify the basic steps clearly and correctly based on
the findings from analysis
• typically, the steps follows a familiar pattern: input, processing
and output
Verify the correctness, consistency and ambiguity of the
algorithm (hand trace the values of the variables in
every steps of your algorithm)
• hand tracing how the values of the variables are changing in
every steps of the algorithm
Steps in Building a Program
3. Coding
Transform the proposed algorithm into coding by using a specific
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 etc to facilitate the coding
tasks.
Implement the program to the user environment such as Android.
Steps in Building a Program
4. Test or debug the program.
Test to verify that the program meets the requirements
by creating test cases
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
Algorithm
An algorithm is a clear step–by-step
sequence of instructions to solve a problem
analogy: recipe (refer next slide)
In mathematics and computing, an
algorithm is a procedure (a finite set of
well-defined instructions) for
accomplishing some task which, given an
initial state, will terminate in a defined
end-state. (source:Wikipedia)
Algorithm: Baking a Cake
Algorithm
comes from the 9th century Persian mathematician
Abu Abdullah Muhammad ibn Musa al-Khwarizmi.
2 ways to represent algorithm:
a) pseudo code
using English-like statements to list the steps to be
taken in the solution.
b) flowchart
using diagrams that employ symbols to describe the
workflow of steps involved in the solution.
Pseudocode
• Pseudocode statements usually perform input, output, and
processing (eg. arithmetic operations)
• The input statements are to get data needed to perform
computation e.g.:
– Input student name, student mark
– Input length, width, height of a box
– Input loan amount, interest rate, loan duration
• The output statements are to display information for the user
e.g.:
– Output "Enter Student Name: "
– Output final grade
– Output "The volume of the box is ", theVolume
– Output "The loan montly payment = ", monthlyPayment
Pseudocode
• The processing statements are to perform computation
e.g.:
– priceAverage = (price1 +price2 + price3 + price4 +
price5) / 5
– theVolume = length x width x height
– BMI = weight / (height x height)
• These statements represent arithmetic assignment
statements. The right side of the statement is the
arithmetic expression using standard mathematical
notation.
• The left side is where the result of the computation is
stored for further processing.
Pseudocode to calculate the perimeter of a
rectangle
• Start
– Output "Enter rectangle length: "
– Input length
– Output "Enter rectangle width: "
– Input width
– Calculate perimeter = 2 x(length + width)
– Output perimeter
• End
Flowchart
• a graphical way of depicting a problem in terms of its
inputs, outputs, and processes.
• the basic elements are as follows:
– Rounded Rectangle (start/end of a program)
– Parallelogram (program input and output)
– Rectangle (processing)
– Diamond (decision)
– Arrow (execution flow)
Flowchart to calculate the perimeter of a
rectangle.
Start
Input length,
width
Perimeter = 2 x
(length + width)
Output
perimeter
Stop