PROGRAMMING I
ALGORITHM CONSTRUCTION
AND SOLUTION
STRUCTURED
PROGRAMMING ALGORITHM
3. Algorithm
• The algorithm is the abstract idea of
solving a problem.
• An algorithm is a step-by-step
instructions that will transform input into
output.
• It can be represent using pseudo code or
flow chart.
1
From Algorithms to Programs
Problem
Problem Analysis
Algorithm
C Program
Algorithm in everyday’s life
How to make a mug of hot coffee ??
2
Algorithm in everyday’s life
How to make a mug of hot coffee:
1. Start
2. Boil water
3. Prepare a mug
4. Put a tea spoon of coffee & sugar
5. Pour hot water
6. Stir
7. End
3. Algorithm
can be described using three c o n t r o l
s t r u c t u r e s (Bohm and Jacopini-1966);
•A Sequence
– is a series of statements that execute one after
another
•A Selection – (branch)
– statement is used to determine which of two
different statements to execute depending on certain
conditions
•A Looping – (repetition)
– statement is used to repeat statements while certain
conditions are met
3
Planning the Algorithm
1. Do Problem Analysis
Identify
Input:
Process :
Output :
2. Transfer to pseudo code or flow chart
3. a. Must start with a start
b. Must close with an end
TECHNIQUES TO REPRESENT
THE ALGORITHM
1.Pseudo code
2.Flow Chart
Pseudo code Flow chart
• Artificial, informal • Graphical representation
language used to of an algorithm
develop algorithms
• Similar to everyday • Special-purpose symbols
English connected by arrows
(flow lines)
4
TECHNIQUES TO REPRESENT
THE ALGORITHM
Example of Pseudocode & Flow chart
Calculate the area of a circle
Pseudo code Flow chart
Start: start
Input radius
Calculate circle area radius
circle area = 3.142 x radius
x radius circle area = 3.142 x radius x radius
Print the circle area
End: circle area
end
4 Pseudo Code
•Artificial, informal language used to develop
algorithms.
•Similar to everyday English.
5
4 Pseudo Code
Pseudocode Format
1
start
statement 1 2
statement 2
The statement
refers to any
input, output &
end process
involved
3
Example 1
Problem statement:
Calculate the area of a rectangle
width
height
6
Problem Analysis:
Input: width, height
Process: area = width x height
Output: area of rectangle
Pseudo Code – Example 1
1)START
#include<stdio.h>
int main (void)
{
2) READ (DECLARE)
VARIABLES
float width, height, area;
3) READ (INPUT)
scanf width, height;
4) COMPUTE
area = width*height;
5) PRINT (Result Output)
printf area;
6) STOP
}
7
Example 2
Problem statement:
Find the average of three numbers input
by user.
Example 2
Problem Analysis:
Input: number1, number2, number3
Process:
average = (number1 + number2 +
number3) / 3
Output: average
8
Pseudo Code – Example 2
1)START
#include<stdio.h>
int main (void)
{
2) READ (DECLARE)
VARIABLES
float number1, number2, number3,average;
3) READ (INPUT)
scanf number1, number2, number3;
4) COMPUTE
average = (number1 + number2 + number3) /3;
5) PRINT (Result Output)
printf average;
6) STOP
}
Example 3
Problem statement:
Determine the total cost of durians, given
the number of kilos of durians purchased
and the cost of durians per kilo.
9
Example 3
Problem Analysis:
Input: Number of kilos of durians,
Cost of durians per kilo
Process:
Total cost = Number of kilos of durians ×
Cost of durians per kilo
Output:
Total cost of durians
Pseudo Code – Example 3
start
1. Input number_of_kilos_of_durians,
cost_of_durian_per_kilo.
2. Compute total cost of durian:
total_cost = number_of_kilos_of_
durians * cost_of_durian_per_kilo;
3. Print total_cost of durian
end
10
Example 4
Problem statement:
MZ Corporation gives a bonus to Faris based
on his sales. The bonus is 5 percent of the
sales. Calculate Faris's bonus.
Example 4
Problem Analysis:
Input: Faris's sales, Bonus rate of 5%
Process: Bonus = Sales x Bonus rate 5%
Output: Faris's bonus
11
Pseudo Code – Example 4
start
1. Input sales, bonus_rate
2. Compute bonus:
bonus = sales * bonus_rate
3. Print bonus
end
EXERCISE
12
Exercise 1
Problem statement:
Kedai Koperasi UM offers 15% discount for
each revision book. Calculate and display the
price that customer should pay.
Prepare IPO analysis, pseudo code & flow
chart
Problem Analysis:
Input: Price
Process: Discount = Price x 15%
New Price = Price - Discount
Output: New Price
13
IPO Chart:
Input Processing Output
Processing items:
price
OR
Problem Analysis:
Input: Price
Process: New Price = Price x 85%
Output: New Price
14
Pseudo Code
start
1. Price
2. Compute Discount:
Discount = Price * 0.15
3. Compute New_Price:
New_Price = Price – Discount
4. Print New_Price
end
Flow chart
start
Price
Discount = Price x 15%
New_Price = Price - Discount
New_Price
end
15
SUMMARY
1. Identify input, process and output from a given
problem.
2. Define algorithm.
3. Solve problem using pseudo code.
4. Solve problem using flow chart.
Approach in Problem
Solving
Control Structure
16
Control Structure
LEARNING OUTCOME:
At the end of this topic, students should
be able to:
1. Explain the purpose of each control structure.
2. Apply appropriate control structure in
Problem solving.
Control Structure
Three (3) control structures
A Sequence
is a series of statements that execute one after another
A Selection – (branch)
statement is used to determine which of two different
statements to execute depending on certain conditions
A Looping – (repetition)
statement is used to repeat statements while certain
conditions are met
17
1. Sequence
Control Structures
SEQUENCE SELECTION LOOPING
1. Sequence
(Analogy)
What do you do after waking up in the
morning?
Brush teeth
Wash face
Take a bath
Comb hair
Smile in front of mirror
Having breakfast
Go to class
18
1. Sequence
• The simplest programs consist just
sequence of statements :
– no loops, no selections amongst
alternative actions.
1. Sequence
• Instruction in sequence programming
are executed sequentially one by
one.
• The sequence structure directs the
computer to process the instructions,
one after another, in the order listed in
the program.
first instruction → last instruction.
19
Example 1
Problem statement:
Find the product of two numbers.
Example 1
IPO Analysis:
Input: num1, num2
Process: product = num1 x num2
Output: product
20
Example 1 – Pseudo Code
1.START
#include<stdio.h>
int main (void)
{
2. READ (DECLARE)
VARIABLES
float num1, num2;
3. READ (INPUT)
scanf num1, num2;
4. COMPUTE
product = num1 * num2;
5. PRINT (Result Output)
printf product;
6) STOP
}
Example 1 - Flow chart
start
num1, num2
product = num1 * num2
product
end
21
Example 2
Problem statement:
Find a volume for a cone.
1
Formula :
3
Example 2
IPO Analysis:
Input: radius, height
Process:
volume = (1/3) x 3.142 x radius x
radius x height
Output: volume
22
Example 2 – Pseudo Code
1.START
#include<stdio.h>
int main (void)
{
2. READ (DECLARE)
CONSTANT
float Pi=3.142;
VARIABLES
float radius, height, volume;
3. READ (INPUT)
scanf radius, height;
4. COMPUTE
volume = (1/3) * Pi * radius * radius * height;
5. PRINT (Result Output)
printf volume;
6) STOP
}
Flow chart - Example 2
start
radius , height
volume = (1/3) * Pi * radius * radius * height
volume
end
23