0% found this document useful (0 votes)
20 views

Unit 1

Here is the flowchart for the algorithm: Input marks If marks >= 90 then Output Grade = "A" Else If marks >= 80 then Output Grade = "B" Else If marks >= 70 then Output Grade = "C" Else If marks >= 60 then Output Grade = "D" Else Output Grade = "F" End This flowchart takes the marks as input from the user. It then checks the marks against different grade boundaries and outputs the corresponding grade based on the marks. It uses selection (if-else) to check the conditions and determine the appropriate grade.

Uploaded by

MAK- 47
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Unit 1

Here is the flowchart for the algorithm: Input marks If marks >= 90 then Output Grade = "A" Else If marks >= 80 then Output Grade = "B" Else If marks >= 70 then Output Grade = "C" Else If marks >= 60 then Output Grade = "D" Else Output Grade = "F" End This flowchart takes the marks as input from the user. It then checks the marks against different grade boundaries and outputs the corresponding grade based on the marks. It uses selection (if-else) to check the conditions and determine the appropriate grade.

Uploaded by

MAK- 47
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 47

Unit 1

Problem Solving
Design
program
Using algorithm
implemented

Detail design for The program is when the


solution algorithm design is
implemented by using
high-level programming
language
Understanding Algorithm
Algorithm
• An algorithm is the list of instructions that a computer needs to do to complete a task or to
solve problem.
• There are three points to consider when deciding whether an algorithm is successful or
not
• Accuracy – it must lead to the expected outcome
• Consistent – it must produce the same results each time it is run
• Efficiency – it must solve the problem in the shortest time using few computer resources
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
Draw flowchart of an algorithm to make a cup of coffee

Fill kettle with water Turn on kettle

Wait for kettle to boil Place coffee in cup

Pour water into cup Add milk and sugar

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.

Eg. mark = 89 89 mark


number = mark + 1
Write flowchart of an algorithm that asks the user to input two numbers and then output the
result of adding them together.

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

INPUT num IF condition


OUTPUT “Hello” Loop condition

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

INPUT num IF condition


OUTPUT “Hello” Loop condition

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

If the number MOD 2 is equal to 0 then Yes


Is
 output “even” Num MOD 2 = output “even”
Otherwise 0?
Output “ odd” No

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

A Student achieved 80 or more of the mark

B Student achieved 70 or more of the mark

C Student achieved 60 or more of the mark

U Student achieved less than 60 of mark


Pseudocode
 is a method of expressing an algorithm design.

Data types Data Structure


 INTEGER  Array
 REAL  String
 BOOLEAN
 CHARACTER

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

Print ( ‘Have a good day’)

Name = input ()
LenghtofJounery = int (input () )
Pseudocode

REPEAT 100 TIMES


Send “ $” To Display
ENDREPEAT
Pseudocode
Exercise
Take input 5 numbers and calculate Average of the input 5 numbers
Set total to 0

For count FROM 1 To 5 Do

Receive num FROM (INTEGER) Keyboard

Set total To total + num

END FOR

Set average to total / 5

Send “ Average of 5 numbers” & average To Display


Sorting and Searching Algorithm
• Whenever we want to find information, we carry out a search
• We are all on sorts of list and all these must be searched to find relevant information
• Sorting information is also important to facilitate efficient searches

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

Sort 4 2 6 1 3 in ascending order Item 4 and 5 are


2 4 1 3 6 swapped
using bubble sort
Sorting Algorithm

2 4 1 3 6 Item 1 and 2 are 2 1 3 4 6 Item 1 and 2 must be


correct order swapped
Item 2 and 3 must be 1 3 4 6 Item 2 and 3 are
2 4 2
1 3 6 swapped swapped

Item 2 and 3 are 1 4 All item are now in


2 1 4 3 6 2 3 6
swapped correct order
Item 3 and 4 must be
2 1 4 3 6 Pass 3
swapped

Item 3 and 4 are


2 1 3 4 6 swapped
Item 4 and 5 are
2 1 3 4 6
correct order

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

In this list the search item is the number 13

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.

mid = (start +end) DIV 2


This binary search algorithm will be used to search 13 in the
given 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.

position Product Ordered 1 2 3 4 5 6 7 8


code examin mid = (start +end)/2
ed ark11 asp11 bar13 dri15 mil19 rib10 str15 tor16
start =1
1 ark11 end = 8
str15 = or < or > dir15
2 asp11 mid = 4
3 bar13 5 6 7 8
start =5
4 dri15 1 mil19 rib10 str15 tor16 end = 8
mid = 6
5 mil19
str15 = or < or > rib15
6 rib10 2
7 str15 3 7 8 start =7
end = 8
8 tor16 str15 tor16
mid = 7
Efficiency of search algorithm
• Search algorithm can be compared by looking at If you want to find a particular item
the worst case and best case for each one in a list of 1000 items
Linear search
 The best case : The item is first in the list Attempt median
1 500
 The worst case : The item is last in the list or not in 2 250
the list 3 125
Binary Search 4 63
5 32
• The best case : The item is median in the list 6 16
• The worst case : The item is not in the list or 7 8
located at the point at which the final division could 8 4
be made 9 2
10 1
Exercise
• Display the stages of binary number , to find the number 13 in
the list
3 9 13 15 21 24 27 30 36 39 42 54 69
Decomposition and Abstraction
• Computational thinking : define and analyzing problems, creating structured solutions-
algorithm and coding the solution into a form that can be implemented by a computer.
 One of skill required for computational thinking is algorithm design
 Two other skills are decomposition and Abstraction
• Decomposition : breaking a problem down into smaller, more manageable parts which
are easier to solve
 Sub problem can be worked on by different team at the same time
 It is easier to spot and correct errors
• Abstraction : the process of removing or hiding unnecessary detail so that only the
important points remain
Example : Student mark system that calculate average mark

Input : Get data about student name and his/her marks

process : Calculate the average of input mark.

output : Display the student name and his/her average mark


Example
 Below is a structure diagram showing how a student mark system that
can calculate the average mark for student can be broken down into
smaller sub-systems.

Student Mark System

Get Data Calculate Average Mark Display Results

Output Output Student’s


Input Student’s Input Student’s
Student’s name Average Marks
name Marks
Input width
Input length

area = width * length

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

You might also like