Professional Documents
Culture Documents
12/21/16
Objectives
12/21/16
12/21/16
12/21/16
Instructions to post
office
12/21/16
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
Remember
12/21/16
10
Steps in Problem-solving
The design of any computer program
involves two major phases:
1.
2.
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
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
12/21/16
16
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
12/21/16
19
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
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
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.
12/21/16
28
Textual data
1.
2.
12/21/16
29
Numerical data
1.
2.
12/21/16
30
Boolean data
12/21/16
31
Data
Type
32
12/21/16
33
Problem 5
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
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
12/21/16
37
Example 2
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
12/21/16
39