You are on page 1of 39

Problem Solving

12/21/16

Objectives

Outline the steps in problem-solving


Decompose a simple problem into its
significant parts

12/21/16

How are instructions given


to the Computer?

Instructions are given to the


computer in the form of computer
programs

What is a computer program?

A set of instructions written in a


programming language, used to solve
a problem.

12/21/16

Consider the problem of giving


directions to someone in order to get
to the nearest post office starting
from point A.
A possible set of instructions could be:

12/21/16

Instructions to post
office

Proceed a mile or so down the road


until you reach the roundabout
Turn left at the roundabout and follow
the road until you see a green house
on the right hand-side
The post office is about the 3rd or 4th
building on the right after the green
house. Youll see the sign in front, you
cant miss it.

12/21/16

Post office cont.


Are the above instructions clear and
straight forward? If not, why?
Lets examine the first instruction:
Proceed a mile or so down the road
A mile or so is imprecise.

12/21/16

Another ambiguity is down the road.


Which way is down? To the right or
left of the person? Northwards or
southwards?
In the second instruction what if there
is more than one green house on the
right? The instruction should
precisely state which green house it
refers to. This will make the
instruction more precise.
12/21/16

In the 3rd instruction it might not be


difficult for a person to figure out
whether the post office is the 3 rd or 4th
building but it would be impossible for
a computer to execute such an
instruction.
Computers must be told exactly what
they must do in the correct sequence.
This set of instructions is called an
algorithm.
12/21/16

Algorithms
An algorithm is a sequence of precise
instructions for solving a problem.
Algorithms have 4 important attributes
1.
2.
3.

4.

12/21/16

They must be precise


They must be unambiguous
They must be finite (terminate after a
finite number of steps)
The instructions must be in a logical
sequence
9

Remember

Writing a program is simply a formal way


of giving instructions to the computer

The only difficulty in writing a program


is knowing how to solve the problem

A programmer must figure out the


solution to a problem before telling the
computer what to do

12/21/16

10

Steps in Problem-solving
The design of any computer program
involves two major phases:
1.
2.

The Problem-solving phase


The Implementation phase

12/21/16

11

Problem-solving phase
The problem-solving phase comprises
the following steps:
1. Define the problem
2. Find a solution to the problem
3. Evaluate alternative solutions
4. Represent the most efficient solution
as an algorithm
5. Test the algorithm for correctness
12/21/16

12

Implementation phase
The implementation phase
comprises the following steps:
1. Translate the algorithm into a
specific programming language
2. Execute the program on the
computer
3. Maintain the program
12/21/16

13

Defining the problem


Defining the problem is a way to help the
programmer understand what is
required. It involves decomposing the
problem into 3 key components:
1.
2.
3.
4.

The data given (the inputs)


The expected results (the output)
The tasks to be performed (processing)
The data that must be stored (Storage)

12/21/16

14

THE DEFINING
DIAGRAM

INPUT
What is
given

12/21/16

PROCESSI
NG
The tasks to
be
performed

OUTPUT
The
expected
result(s)

15

Defining Diagram cont.

Input can be easily identified by


certain keywords given, read or
accept.
Output is the end result required.
Keywords that help identify output
are print, display, produce,
output.

12/21/16

16

Defining Diagram cont.


What do I have to do to the inputs in
order to produce the desired
output?
The answer to this question is what
should be listed in the processing
section.

12/21/16

17

Problem 1
A program is required to read three(3)
numbers, calculate and print their total.
Step 1: Identify the inputs ( the data given)
Hint: the keyword read identifies the inputs.
3 numbers, lets say: num1, num2, num3

12/21/16

18

Step 2: Identify the outputs


Hint: the keyword print identifies the
output or product of three numbers
TOTAL

12/21/16

19

The final step is to list the processing


steps. Here, we list all the actions that
must be performed in order to get the
desired results.
What do we have to do with the 3
numbers to print their total?
1.
2.
3.

Get the 3 numbers


Calculate their sum
Print the total

12/21/16

20

Problem 1 Defining
Diagram
INPUT
PROCESSING
num1,num2, 1. Read/ get 3
num3
numbers
2. Find sum
3. Print Total

OUTPUT
TOTAL

At this stage we are not concerned with how


each action is performed, that will be done
when writing the algorithm
12/21/16

21

Problem 2
Given 3 integers representing the age
of 3 boys respectively, write a
program to find their average age
and also determine the age of the
oldest boy.

12/21/16

22

Problem 2 Defining
Diagram
INPUT
3 integers:
age1, age2,
age3

12/21/16

PROCESSING
1. Read/ get 3
integers
2. Find average
of the three
integers
3. Find highest
age
4. Print average,
highest age

OUTPUT
Average
age
Highest
age

23

Problem 3
The cost of a new car is the sum of the
wholesale cost, the local sales tax and
the dealers percentage mark-up.
Assuming the dealers markup is 10
percent of the wholesale cost and the
sales tax is 6 percent, design a
program to read a car ID (an integer
value) and the wholesale cost of the car
(a real value) and print the car ID and
the cost to the consumer.
12/21/16

24

Problem 3 defining
diagram
Input
wholesale
cost
sales tax
dealers
mark-up
car ID

12/21/16

Processing
1. Get wholesale
cost, sales tax,
mark-up, car ID
2. Calculate cost to
consumer
3. Print car ID,
cost to
consumer

Output
car ID
Cost to
consumer

25

Problem 4

Read the price of an item and its


type. A 5% discount is to be given on
all books. If the item is a CD then
there will be a 10% discount. All
other items are to get a 2% discount.
Calculate and print both the
discount as well as the discounted
price.

12/21/16

26

Problem 4 Defining
diagram
Inpu Processing
t
Price 1. Read price and type
type 2. Determine and
calculate discount
3. Calculate discounted
price
4. Print discount and
discounted price
12/21/16

Output
Discount
Discount
price

27

Data types

1.
2.
3.

There are 3 main data types:


Textual data
Numerical data
Boolean data

12/21/16

28

Textual data

1.
2.

May include letters, symbols and


numbers.
Character e.g A
String a group of characters e.g
Sam Jones

12/21/16

29

Numerical data

1.

2.

The are two main types:


Integers numbers without a
decimal part. E.g. 45, 875, 0, -65
Real numbers numbers with a
decimal part. E.g. 45.0, 875.843

12/21/16

30

Boolean data

Data that must be true or false, e.g.


whether a person is married

12/21/16

31

Data

Type

the grade a student got in a


course e.g. A, B, C
the time Usain Bolt takes to run
a 100m race (in seconds)
The number of books in a
persons bag
Whether or not a piece of
luggage is overweight
The cost of a soda at the Tuck
shop
A persons name
A
persons
middle
intial
e.g
R
12/21/16

32

Constants and Variables

Constant an area of storage whose


value cannot change e.g. pi, the
number of days in a week etc
Variable - an area of storage whose
value can change

12/21/16

33

Problem 5

Write a program that allows the user to


enter the radius of a circle. It should
calculate and display the area of the circle
and its circumference.
Input Processing
Output
Radi 1. Read radius
Area
us
2. Calculate the area circumfere
nce
3. Calculate
circumference
4. Print the area and
circumference

12/21/16

34

Naming variables

Variable name:
Should reveal the type of data it
contains
Should only contain letters, numbers
and underscores
Should begin with letter
Must not contain any spaces e.g.
monthly_salary

12/21/16

35

Finding the best solution

Try to solve the problem manually


Example 1: write a program that reads the price of an item as
well as the quantity purchased, and displays the total cost

Input
Price
quantity

12/21/16

Processing
1. Read price,
quantity
2. Calculate the
cost
3. Print the cost

Output
Total
cost

36

Example 1 - Solution

Read the price and quantity


Multiply the price by quantity and
store the result in cost
Print cost

12/21/16

37

Example 2

Write a program that takes the marks of


20 students and prints the highest mark.

Input
Processing
Marks of 1.Read marks
20
2.Determine the
students
highest mark
3.Print the highest
mark

Output
Highest
mark

12/21/16

38

Example 2 solution

Read the list of marks


Set the first mark as the maximum
mark
For each of the remaining marks:
Compare it to the maximum
If it is higher than the maximum, set it
as the new maximum

Print the maximum

12/21/16

39

You might also like