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

Algorithms Grade 12

computer notes

Uploaded by

Mario
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Algorithms Grade 12

computer notes

Uploaded by

Mario
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 83

ST CLEMENT’S SECONDARY

SCHOOL

COMPUTER STUDIES SENIOR


GRADE 12

MARIO@2024
2

Algorithm planning
and design
MARIO@2024
3

ALGORITHM

▪An algorithm is a list set of instructions, used to


solve problems or perform tasks, based on the
understanding of available alternatives.

MARIO@2024
4

PROGRAMMING AND PROBLEM SOLVING


▪ Overall guidelines on how to develop a computer program
1. Identify a problem or opportunity
2. Design possible solutions
3. Using test data, dry-run the designs on the piece of paper.
4. Convert the actual designs into actual program (through
incremental approach)
5. Using test data, test the program for syntax and logical
errors.
6. Install the program and develop a work plan for review
and maintenance of the system.
MARIO@2024
5

STAGES FOR PROGRAM DEVELOPMENT

1. Problem recognition
2. Definition
3. Design
4. Program coding
5. Program testing
6. Implementation

MARIO@2024
6

EXERCISE

1. Define an algorithm
2. Explain the first two guidelines on how to develop a
system.
3. list the stages on how to develop a system

MARIO@2024
7

PROGRAM
DEVELOPMENT LIFE
CYCLE

MARIO@2024
Problem Problem 8

recognition recognition
Problem
definition
Program
design
KEY Program
▪ The solid arrows coding
show the next step.
▪ The broken arrows Program
show the previous testing
step.
Program
MARIO@2024
implementation
9

PROBLEM RECOGNITION

▪Program recognition refers to identifying a


problem that need that need to be solved or
an opportunity that exist.

MARIO@2024
10

PROBLEM DEFINITION

▪Problem definition also referred as problem


analysis, the programmer carefully analyses
the problem in order to determine input,
processing and output requirements.

MARIO@2024
11

PROGRAM DESIGN

▪Program design refers to creating a problem-


solving tool or approach known as an
algorithm.
▪Algorithms can be created using
Pseudocodes, flowcharts, decision tables and
hierarchical charts.

MARIO@2024
pseudocode Flowchart 12

START
BEGIN
SET PI = 3 .142
Read radius
WRITE “ Enter radius of the
sphere”: PI = 3. 142
READ radius Volume = 4/3 *PI* radius3
Volume = 4/3 * PI * radius 3
Print Volume
WRITE Volume
END
MARIO@2024
Stop
13

PROGRAM CODING

▪Coding refers to implementing an algorithm


into a computer program using a particular
programming language.
▪Programming languages such Pascal, C, C++,
Visual Basic, Python and Java.

MARIO@2024
14

PASCAL PROGRAMMING SAMPLE


PROGRAM BallSize (input, output);
Const PI =3. 142;
Var radius, volume : real;
Begin
Write (`Enter radius`);
ReadIn (radius);
Volume : = 4/3*radius*radius*radius;
WriteIn (`The volume is`, volume)
End.
MARIO@2024
PROGRAM TESTING 15

▪During and after coding, the program has to be


tested in order to detect and correct errors (bugs) in
programming.
▪There are two types of errors syntax errors and logic
errors.
▪Syntax errors are as a result of improper use of
programming language. Such as wrong spelling of
keywords.
▪Logic error are caused by poor design. A program
may run but gives unexpected results or terminate
abnormally.
MARIO@2024
16

IMPLEMENTATION

▪Implementation refers to the installation of a


new program ready for use.
▪It requires review of installed program and
training of the users on how to use and
maintain the program.

MARIO@2024
17

EXERCISE
1.Explain the following system developments stages.
(a)Problem recognition
(b)Program design
(c)Implementation
2. At what stage of system development errors can
be detected?
3. State two error and explain each error.

MARIO@2024
18

LARGE SYSTEMS DESIGN


MARIO@2024
19

LARGE SYSTEMS DESIGN


▪Large systems are decomposed into subsystems
that provide related set of services.
▪The initial design process of identifying these
subsystems and establishing a framework for
subsystem control and communication is called
architectural design.
▪Architectural design represents the structure of data
and program components that are required to build
a computer-based system.
MARIO@2024
20

MODULAR DESIGN OF LARGE SYSTEMS


▪A system is considered to be modular if it consists of
well-defined, manageable components with well-
defined interfaces among the units.
▪The organisation of modules can be represented
by a tree-like structure referred to as hierarchical
diagram.
▪To design large modular systems, algorithm design
tools are used. Such as decision tables, flow charts
and pseudo codes.
MARIO@2024
21

HIERARCHICAL DIAGRAM
Library Management
System

New Member Circulation New Books

Borrow Return

MARIO@2024
22

QUALITIES OF GOOD SYSTEM DESIGN

▪Correctness
▪Verifiability
▪Completeness
▪Traceability
▪Efficiency
▪Simplicity
▪Design documentation
MARIO@2024
23

EXERCISE

1. State two tools used in designing large


systems.
2. List four qualities of a large system.

MARIO@2024
24

ALGORITHM DESIGN
TOOLS
MARIO@2024
25

ALGORITHM DESIGN TOOLS

▪Pseudocode
▪Flowchart
▪Structure charts
▪Decision tables

MARIO@2024
26

1. PSEUDOCODE

▪Pseudocode is a set of structured English


statements used to express the logic of a
program.
▪The input and output instructions are defined
using words such as READ for input and WRITE
for output.

MARIO@2024
27

EXAMPLE OF A PSEUDOCODE
1. Write the pseudocode to calculate the area of the
circle.
BEGIN
SET PI = 3.142
WRITE “Enter radius of a circle”:
READ Radius
Area = PI*radius𝟐
WRITE Area
END.
MARIO@2024
28

ADVANTAGES OF PSEUDOCODE
▪It is ease to create
▪It requires simple syntax to write
▪It reduces time spent in coding, testing and
modifying a system
▪Statements are easily translated to high level
language.
▪Pseudocode implements structured concepts
in a better way
MARIO@2024
29

DISADVANTAGES OF PSEUDOCODE
▪Pseudocode is not visual hence difficult to
understand complex logic.
▪Pseudocode varies widely because there is no
accepted standard.
▪It can be quite lengthy for complex problems.

MARIO@2024
30

EXERCISE

1. What is pseudocode?
2. Write the pseudocode to calculate the area
of the rectangle.

MARIO@2024
31

2. FLOWCHART
▪A flowchart is a graphical representation of an
algorithm.
▪It uses standard set of symbols to show the
operations and decisions to be followed by a
computer in solving a problem.
▪Simple instructions are written within boxes that are
connected using arrows to indicate the flow of
input, processing and output operations.
MARIO@2024
SYMBOLS
Shows the start and the end of the
32

Start/stop
algorithm
Read x,y Represents input and output

Sum=x+y Represents processing operation

Represents the decision that evaluates


X<y? true/false
Off page connector that shows that the
flowchart continues to the next page
MARIO@2024 Shows the flow of processing logic
33

Predefine
d process

MARIO@2024
Start 34

Read radius EXAMPLE

PI = 3.142
Area = PI* radius𝟐

Write Area

stop
MARIO@2024
35

ADVANTAGES OF FLOWCHART

▪Are better way of communicating system


logic.
▪The problem can be analysed effectively.
▪Graphical representation of design serves as a
good program documentation.
▪It makes it easier to debug and maintain.

MARIO@2024
36

DISADVANTAGES OF FLOWCHART

▪For complex program logic, it becomes


complex.
▪If alterations are required the flow chart may
require redrawing.
▪Flow chart symbols cannot be typed hence, its
time consuming.
▪It takes a lot of space on paper.
MARIO@2024
EXERCISE 37

1. State the use of the following symbols for the


project

A B C D

E
MARIO@2024
EXERCISE 38
1. The following algorithm in Pseudocode is used to calculate
the area of a triangle given base (b) and height (h).Rewrite
using flowchart
START
enter b
enter h
IF (b < 0) or (h < 0) THEN
WRITE input error
ELSE
calculate Area = 𝟏Τ𝟐 *b*h
WRITE Area
ENDIF
MARIO@2024 STOP
39

3. HIERARCHICAL CHART

▪It is a tree-like diagram used to describe the


logical grouping of the system modules or
functions.
▪The topmost rectangle represents the system
while the lower levels represent modules,
subprogram or procedures.

MARIO@2024
40

HIERARCHICAL CHART FOR BANKING SYSTEM

Banking System

New Account transactions Manage loans

Withdraw Deposit

MARIO@2024
4. DECISION TABLES41

▪Decision tables contain rules used to describe


complex applications that involve alternative
decisions.
▪They represent several conditions in the tabular
formats that need to be considered before taking
action.
▪To create a decision table, a programmer must
identify all possible conditions to be addressed,
determine actions for the conditions and maximum
possible rules.
MARIO@2024
Key DECISION TABLE 42

Y = Yes
N = No
I = immaterial

Rule 1 Rule 1 Rule 1


Salaried-employee N N Y
Hours-worked>40 Y N I
pay Overtime-rate Hour-rate Salary
MARIO@2024
Manage customers 5. ACTIVITY DIAGRAMS
43

IF option VALID

Option = add
Add Module
Option = edit
Add Module

Option = edit
delete Module
Option = del
ELSE
Error Routine

ENDIF
MARIO@2024
Exit to main menu
44

EXERCISE

MARIO@2024
45

DESIGN AND
INTERPRETATION OF
ALGORITHM
MARIO@2024
46

DESIGN AND INTERPRETATION OF ALGORITHM

▪Design of algorithm is the practice of


specifying details needed for actual computer
program implementation in terms of input,
processing, output and storage management.

MARIO@2024
47

GUIDELINES ON HOW O MINIMISE ERRORS


DURING ALGORITHM DESIGN
1. Perform dry-run
2. Make sure each algorithm has a finite series
of concrete steps.
3. Ensure no ambiguity as to which step is to be
performed next.
4. Make sure each algorithm has a single start
and termination point.
MARIO@2024
DESIGN INTERPRET AND TEST ALGORITHM 48

CONTROL STRUCTURES
1. Sequence control structure
2. Selection control structure
3. Looping control structure

MARIO@2024
49

SEQUENCE CONTROL

▪In control structure, a computer executes


program statements sequentially from the start
to the end.

MARIO@2024
50

SEQUENCE CONTROL STRUCTURE

Program: Payroll
Begin
Enter days worked; Direction of execution
Enter rate per day;
Grosspay = rate * days;
print Grosspay;
End
MARIO@2024
51

EXAMPLE

1. Write an algorithm that would prompt the


user to enter the length and width of a
rectangle. The program calculates and
displays the area and perimeter.
▪Interpretation: Required inputs length (L) and
Width (w).
▪Output area and perimeter
MARIO@2024
Flowchart SOLUTION 52

Start Pseudocode
BEGIN
Read L, W WRITE “Enter length and
width”
Area = L * W READ L, W
P = 2 *(L + W) Area = L * W
Perimeter = 2*(L+W)
Write Area, Perimeter WRITE Area
WRIRE Perimeter
Stop END
MARIO@2024
53

EXERCISE

1. Draw a flowchart for a program that would


be used to prompt the user to enter two
numbers, x and y. The program calculates
and then displays the product on the
screen.
2. Write a pseudocode for the flowchart you
have written in question (1)
MARIO@2024
SELECTION CONTROL 54

▪ In selection control, the execution of statements


depends on the condition that returns true or false.
▪ The condition must be Boolean expression such as
Marks > 40)
▪ There are four types of selection control that are
used in structured programming.
(i) If
(ii) If…….else
(iii) Nested If
(v) Switch/case
MARIO@2024
55

IF SELECTION
▪If…… is used when only one option is
available.
Syntax
IF <condition> THEN
statements
ENDIF

MARIO@2024
EXAMPLE 56

1. Write the flowchart and pseudocode that


determines whether a person should be
allowed to voter using if selection. If the person
is less than 18 years old, the program exits
without doing anything.

MARIO@2024
Flowchart SOLUTION 57

Start Pseudocode
BEGIN
Read Age WRITE “Enter your Age”
READ Age
Is Age IF Age > =18
NO
>= 18? WRITE “Vote”
END
YES
Vote
MARIO@2024
58

IF …….ELSE SELECTION
▪If …….Else selection is suitable when there are two
available options.
▪Syntax
IF <condition> THEN
statements;
ELSE
Statements;
ENDIF

MARIO@2024
59

EXAMPLE

1. Write a flow chart and a pseudocode for the


voting program. If the age is >18 years display
“vote” otherwise “Not eligible to vote”.

MARIO@2024
Start SOLUTION 60

Pseudocode
Read Age BEGIN
WRITE “Enter your Age”
READ Age
IF Age > =18
Is Age NO
WRITE “Vote”
>= 18?
Not eligible ELSE
to vote WRITE “ Not eligible to
YES vote”
Vote ENDIF
END
Stop
MARIO@2024
61

EXERCISE

1. Draw a flowchart for a program that reads


two numbers and displays the numbers in
descending order.

MARIO@2024
NESTED IF SELECTION 62

▪Nested If selection is used where several options


have to be considered to make a selection.
Format/syntax
IF <condition> THEN
Statements;
ELSE
IF <condition> THEN
Statements;
ELSE
Statements
ENDIF
ENDIF
MARIO@2024
63

EXAMPLE
1. Draw a flowchart for the program that allows the
user to key in the current date and date of birth for
several people. Depending on the calculated age,
the program classifies each person into categories.
Age (years) Category
Below 5 baby
6 - 12 adolescent
13 - 19 Teenager
20 - 34 Youth
MARIO@2024 Above 35 Adult
Start SOLUTION 64

DOB, ToDate

Is NO Is NO Is NO Is NO Is
Age Age Age Age
NO
Age
<5? <13? <20? <35? >34?
YES YES YES YES YES

baby adolescent Teenager Youth Adult

WRITE Category
MARIO@2024
Stop
65

EXERCISE

1. Using flowchart, formulate an algorithm for a


program that would be used to compare three
numbers A, B and C and display the largest of
the tree numbers.

MARIO@2024
SWITCH SELECTION 66

▪ Most structured programming languages provide an


alternative to the nested if known as switch
selection.
▪ The main advantage of the switch selection is the
reduced code and time. The pseudocode
SWITCH (score) of
80…………………100: Grade = A
70…………………79: Grade = B
60…………………69: Grade = C
50…………………59: Grade = D
MARIO@2024
40…………………49: Grade = E
LOOPING (ITERATION) CONTROL STRUCTURE 67

▪It is also known as repetition.


▪The condition is tested until the termination
condition is met, otherwise the loop runs
infinitely.
▪The main looping control structures
1. While
2. Repeat…………….until
3. For loop
MARIO@2024
1. WHILE LOOP 68

▪It is a control structure that first evaluates the


condition before executing the body of the
loop.
▪It executes statements within the body of the
loop zero or more times.

PSEUDOCODE
WHILE <condition> DO
statements
END WHILE
MARIO@2024
69

FLOWCHART

Yes
Condition? statements

No
MARIO@2024
70

EXAMPLE

1. At NATSAVE bank, a customer is only allowed


to withdraw in multiples of K10 if the minimum
account balance is over K50 otherwise a
message “insufficient funds” is displayed. In a
single transaction, the minimum withdraw
amount is K10. Draw a flowchart for a
program that would be used to enforce this
condition.
MARIO@2024
71

Interpretation
Once a client enters the amount to withdraw,
the algorithm first uses the modulus (%) to divide
the amount by 10. if the remainder is not 0, the
algorithm displays a message “try again” and
returns to the input screen

MARIO@2024
Start FLOWCHART 72

Read Amount
bal

no Amount must be divisible


Try again Amount
%10=0? by 100

Yes

bal =50? Insufficient funds

Yes
bal =bal - amount

Write Receipt

MARIO@2024
Stop
73

REPEAT…….UNTIL
▪ It is a post-test loop that allows the statements within
he loop to be executed at least once. This is
because the condition is tested at the end of the
loop.

REPEAT
Statements
UNTIL <condition>
MARIO@2024
74

statements

true
Condition?

MARIO@2024
false
75

EXAMPLE
The figure below shows an algorithm for computing
the sum of the first 50 natural numbers. Explain step by
step using a trace table how the flowchart
implements repeat until loop.

MARIO@2024
Start 76

Set sum = 0
Set n = 0

n = n +1

sum = sum + n

yes
n<50

no
Write sum
MARIO@2024
Stop
77

interpretation
▪ The sum and integer n are initially assigned to zero.
The algorithm then increments n by 1 before adding
it to sum.
▪ Using a Boolean condition, the algorithm test if n is
less than or equal to 50, if not, the number is
incremented and looping continues until the
condition is false.
▪ If n < 50, the condition returns false hence the
algorithm exist the loop and prints the cumulative
sum.
MARIO@2024
78

EXERCISE
1. Study the algorithm below and use it to answer the question
that follow.
start
Sum = 0
Get a value
Sum = Sum + value
Output the sum
Go step 3 to get next value
Stop
Draw a flow chart from the statement in the algorithm
MARIO@2024
79

FOR LOOP
▪ The For loop is a pre-test loop that sets the lower and
upper limit that delimits the number of times the
statements within the body are to be executed as
follows:
For loop variable = lowerlimit TO upperlimit DO
Statement
ENDFOR

MARIO@2024
EXAMPLE 80

▪ Write a pseudocode for a program that would be used to display the 50 positive
numbers and their sum.
Interpretation: requires the use of the For…..loop that sets the lower limit to 1. in every
loop, the number is added to sum and incremented by 1 until it is equal to or greater
than the upper limit as shown below:
BEGIN
SET Sum = 0
SET upperlimit t0 50
For number = 1 TO Upperlimit
Sum = Sum + number
END FOR
WRITE Sum
END

MARIO@2024
81

LAB

for (int i = 1; i <= 50; i++) {


cout << i << endl;
}

MARIO@2024
82

EXERCISE

Algorithms and programs use loops to control


the number of times a particular procedure is
used.
(a) Write a procedure using For loop method to
input 20 numbers into variable called x.
(b) Apart from for loop, mention any other two
loop
MARIO@2024
83

MARIO@2024

You might also like