ITPLA1-B22: INTRODUCTION TO PROGRAMMING
LESSON III: ANALYZING AND SOLVING PROBLEMS
1
LEARNING OUTCOMES
Read and analyse a problem to understand exactly what it is that
needs to be solved.
Describe how a computer processes data.
Name the steps to find an excellent and efficient solution for any
problem.
Define an algorithm and describe how it is used to solve a simple
problem.
2
PROBLEM SOLVING
Class discussion:
Write down the steps to take from waking up in the
morning until you get to class.
3
PROBLEM SOLVING
To solve a problem using a computer, it’s essential to understand
what the problem is?
Steps to understand a problem:
Read the problem carefully,
Understand what the problem entails,
Write down the steps to solve the problem.
These steps are called an algorithm
a set of instructions in a specific sequence used to solve a problem.
4
UNDERSTANDING THE PROBLEM
• When a programmer is faced
with a problem to solve, the
problem is read carefully and
maybe read a number of times
to be very sure that the problem
is understood.
• Delete all unnecessary
information from the problem
statement.
5
UNDERSTANDING THE PROBLEM
Example 3
The sum of two numbers must be calculated, but the problem statement doesn’t supply the
values. The algorithm can ask the user to supply the values of the numbers before
calculating the sum.
The most important aspect of solving a problem using a computer is writing the algorithm,
or steps, to solve it.
An algorithm must be written in such a way that is it unambiguous and precise. The
computer cannot think for itself – you need to tell the computer exactly what to do. You
should never assume that the computer will do something that you haven’t explicitly
specified in a step.
Another essential aspect of understanding a problem statement is understanding the
individual words in the statement.
6
DATA PROCESSING
An algorithm is divided into
three phases:
What data you have available to
solve the problem.
How you’re going to solve the
problem; what steps you’re going
to take.
What the required result is.
7
DATA PROCESSING
Both diagrams clearly indicate that the algorithm must
receive data (input) that must be processed to produce
meaningful results (output or information).
Input is processed to produce meaningful output.
Data is processed to produce meaningful information
8
DATA PROCESSING
Calculate the sum of two numbers where the problem statement does not
supply the values
Example 3:
Input: Not available in the problem statement. The user has to supply the numbers.
Processing: Add the two numbers to determine the sum.
Output: The sum as calculated in the processing phase and displayed on the computer
screen.
Note the following:
The value of the numbers must be available before the sum can be calculated.
It is impossible to display the sum before it has been calculated.
From this, it is clear that the steps in an algorithm must always be written in logical order.
9
THE PROBLEM-SOLVING APPROACH SUMMARY
Analyse the problem
Identify alternative ways to solve the problem
Select the most effective way to solve the problem
List all the steps
Evaluate the algorithm for accuracy
Write pseudocode
10
PROBLEM SOLVING APPROACH
Pseudocode is a way in which the algorithm steps are
written so that they can be followed easily and understood.
It is mostly given in simple English.
Not yet in a computer programming language, but it can be
easily converted into a computer language.
Planning is never done in a programming language as it may
be too technical for the planning stage.
11
PROBLEM-SOLVING APPROACH: ACTIVITY
Example:
Peter sells oranges, pears, guavas and apples. Last week he sold 50 apples, 100
pears, 80 oranges and some guavas. This week he sold twice as many items. How
many did he sell?
To solve the problem:
Ask the question to obtain the number of guavas sold last week.
Get the number of guavas.
Calculate the total number by adding the four numbers to determine the total number
sold last week.
Multiply last week’s total by 2 to obtain this week’s total.
12
PSEUDO-CODE
13
PSEUDO-CODE
• Pseudocode is much
easier to follow than the
program in a
programming language,
such as the Visual Basic
program
14
WHAT DOES PSEUDO-CODE ENTAIL
There are 6 main computer
operations:
A computer can perform
A computer can receive data.
arithmetic.
Read − from a file, database, or other
source
Use actual mathematical
Enter − via the keyboard, scanner or other
symbols or the words for the
symbol
input device
Add a number to a total
A computer can produce information.
Write − information to a file or a database Total = total + number
for instance +, -, *, /
Display − information to the screen Calculate and compute are 15
also used.
WHAT DOES PSEUDO-CODE ENTAIL
A computer can assign a value to a
variable.
Three different cases are identified:
A computer can compare two pieces of
To give variables an initial value information and select one of two
Initialise, set alternative actions.
To assign a value as a result of if condition then
processing some action
? = ? else
x = 5 + y alternative action
To keep information for later use endif
16
Save, store
WHAT DOES PSEUDO-CODE ENTAIL
A computer can repeat a group of
actions
while condition (is true)
some action
loop
for a number of times
some action
next number
17
WRITING ALGORITHMS
18
FLOWCHARTS
A flowchart is a schematic representation of an algorithm.
It illustrates the steps in a process and it consists of a number of
specific diagrams joined in a specific manner.
It graphically represents the program logic by using a series of
standard geometric symbols and connecting lines.
Different flowchart symbols are used for different aspects of the
process.
19
FLOWCHART SYMBOLS
20
ALGORITHM VS FLOWCHART
Write an algorithm to enter two numbers that are
not equal and display the bigger number.
21
EXERCISE
22