Unit 1
Unit 1
Problem Solving
Design
program
Using algorithm
implemented
Stir
data
result
data Algorithm/program
Input data
Take input data and store for calculation
Calculate data 1a ( error ) start with char
First Number (space )
Output / Display result
firstNumber
How to store data in algorithm or program first_number
Variable
The identifier (name) given to a memory location
used to store data.
The value can be changed during program execution.
Values are assigned variable using the = operator.
2 7
5 algorithm
Input firstnum
Input secondernum
sum = firstnum + secondnum
Output sum
Flowcharts
Flowchart can be used to show an algorithm as a diagram.
Each step in the algorithm is represented by a symbol.
Symbols are linked together with arrow showing the order in which steps are completed
num =1
res = num +1
Write flowchart of an algorithm that asks the user to input two
numbers and then output the result of adding them together.
Input firstnum
Input secondernum
Input firstnum
sum = firstnum + secondnum
Output sum
Input secondnum
sum = firstnum +
secondnum
output sum
Exercise
• Develop an algorithm using a
flowchart that asks the user to enter
their height (in meter) and weight (in
kg) and displays their BMI. The Input height
formula for calculating BMI is weight/
height2
Input weight
bmi = weight /
(height * height)
output bmi
Arithmetic Operator
Variable and Constant
• Variables play an important role in algorithm.
• The value stored by a variable can change during the execution of program
Constant
• A constant is the opposite of a variable
• It is a container that hold a value that cannot change during the execution of program .
• PI = 3.14
• It is important to choose descriptive(meaningful) identifier.
mark = 45
mark = 50
Constant
PI = 3.14
PI = 3 (error)
• Develop an algorithm using a flowchart
that asks the user to enter width and
height of rectangle and find the area of
rectangle. Input width
Input height
area = width *
height
output area
• Develop an algorithm using a flowchart
that asks the user to enter radius of circle
and find the area of circle
Input radius
Sequence
PI = 3.14
area = PI *
radius* radius
output area
•The programming language make use of three basic programming constructs ( Sequence , selection and
repetition).
•Combining these constructs provides the programmer with the tools required to solve logical problems
Sequence
Sequence is indicated by the order in which the code is written,
usually top to bottom.
Selection
• Selection used to determine a different set of steps to execute
based on a decision (condition).
• Code branches and follows a different sequence based on
decision made
• Develop an algorithm using a flowchart that asks
the user to enter radius of circle. If the radius is
positive, find the area of circle. If not, output error
message Input radius
Enter radius
Assign PI to 3.14
If the input value of radius greater than 0 then Sequence
Calculate area by multiply PI and the square PI = 3.14
of radius
output area
Otherwise area = PI *
Output “ Invalid radius value” radius* radius
output area
Flowcharts
Flowchart can be used to show an algorithm as a diagram.
Each step in the algorithm is represented by a symbol.
Symbols are linked together with arrow showing the order in which steps are completed
num =1
res = num +1
• Develop an algorithm using a flowchart
that asks the user to enter radius of circle
and find the area of circle Input radius
Enter radius
Assign PI to 3.14
If the input value of radius greater than 0 then PI = 3.14 Sequence
Calculate area by multiply PI and the square
of radius
output area
Is Yes
Otherwise area = PI *
radius > 0? radius* radius
Output “ Error”
No
Input data -3
output “Error” output area
• Develop an algorithm using a flowchart
that asks the user to enter number and
check the number is even or odd. Input num
Enter number
output “odd”
Input data 4
Exercise
A bus company sets fares for different
groups of passengers. The fares are:
a child fare for passengers 15 years
Input age
old and younger
a senior fare for passengers 65
years old and older
a full fare for all other passengers. Yes
Is age <=15? Output “Child “
Construct a flowchart of an algorithm that
will determine the fare for one passenger
when an age is input. No
Yes
Is age>=65? Output “Senior “
No
output “Full”
Exercise
Write flow chart of algorithm that required to take as input values the number of marks a student
achieved available in an examination. It should the output the grade obtained based on the grade
boundaries in table.
Grade Condition
Counter = 0
Mystring = “Hello world
Sum = Score + 10
Size = LENGTH (word)
ArrayClass[1] = ‘Ann’
ArrayValues = [1,2,3,4,5]
Pseudocode Send “Result” & res To Display
Name = input ()
LenghtofJounery = int (input () )
Pseudocode
END FOR
Sorting
Sorts enable a data set to be sorted into ascending order or descending order.
Ascending order : this is arranging item from smallest to largest ( 1,2,3)
Descending order : this is arranging item from largest to smallest (3,2,1)
Sorting algorithm include
Bubble sort
Merge sort
Sorting Algorithm
Pass 1
Bubble sort (Ascending Order)
1. Start at the beginning of the list 4 2 6 1 3 Item 1 and 2 must be
swapped
2. Compare the values in the position 1 and Item 1 and 2 are
position 2 in the list – if they are not in 2 4 6 1 3 swapped
ascending order then swap them
Item 2 and 3 already
3. Compare the values in the position 2 and 2 4 6 1 3
in ascending order
position 3 in the list and swap if
Item 3 and 4 must be
necessary 2 4 6 1 3 swapped
4. Continue to the end of the list
Item 3 and 4 are
5. If there have been any swaps repeat step 2 4 1 6 3 swapped
1 to 4 Item 4 and 5 must be
2 4 1 6 3 swapped
Pass 2
Sorting Algorithm
Merge sort 8 4 2 6 1 3 6 7
• Divide a list into two smaller
lists and then divide these until
8 4 2 6 1 3 6 7
the size of each list is one.
• Merge the lists to produce the
8 4 2 6 1 3 6 7
final sorted list
8 4 2 6 1 3 6 7
4 8 2 6 1 3 6 7
2 4 6 8 1 3 6 7
1 2 3 4 6 6 7 8
The efficiency of Sorting Algorithm
The bubble sort algorithm use brute force because it
starts at the beginning and complete the same task
over and over again until it has found a solution
The merge sort uses the divide and conquer method
because it repeatedly breaks down the problem into
smaller sub-problem, solves those and then combines
the solutions.
The graph shows that bubble sort is far slower at
sorting list of more than 1000 items, but for smaller list
the time difference is too small to be of important
As bubble sort algorithm is easier to code, it could be
beneficial to use if form smaller lists of less than 1000
items
Exercise
• The following list would be sorted into descending order using merge sort and bubble sort
48, 20,9,17,13,21,28,60
Linear Search
• It simply starts at the beginning of the list and goes through item by item, until it
find the item or reaches the end of the list without finding it.
• Linear search is sequential because it moves through the list item by item
• Linear search
1. Start at the first item in the list
2. Compare the item with the search item
3. If they are the same , then stop
4. If they are not, then move to the next item
5. Repeat 2 to 4 until the end of the list is reached
24 13 3 31 3 45 60 69 27
Binary Search
• Binary search uses a divide and conquer method
• In a binary search the middle or median item in a list is repeatedly selected to reduce the
size of the list to be searched
• To use this method, the list must be sorted into ascending or descending ordered.
• It will not work on an unsorted list.
position Product 1 2 3 4 5 6 7 8 9
code mid = (start +end)DIV2
3 13 24 27 31 39 45 60 69
1 3 start =1
2 13 end = 9
13 = or < or > 31
3 24 mid = 5
1 2 3 4
4 27 start =1
3 13 24 27 end = 4
5 31 mid = 2
6 39
13 = or < or > 13
7 45
8 60 The number is found
9 69
This binary search algorithm will be used to search the product
list for the product code str15.
perimeter = (2*
width)+ (2* length)
output area
output perimeter
Exercise
Input weight
Yes
Is weight >=5? Output “Large “
No
Yes
Is weight>=4? Output “medium
“
No
output
“Small”
Input hour
Input minute
second = (hour*
3600) + (minute*60)
output second
Exercise
Input att
Yes
Is att >=5? Output “Large “
No
Yes
Is weight>=4? Output “medium
“
No
output
“Small”
Exercise
• The following list would be sorted into descending order using merge sort and bubble sort
48, 20,9,17,13,21,28,60