Sie sind auf Seite 1von 26

Algorithms

Algorithm And Flowcharts


and flowcharts 12ththAPRIL,
12 APRIL,2018
2018

ZAMBIA ASSOCIATION FOR MATHEMATICS ASSOCIATION

EASTERN PROVINCE

CAPACITY BUILDING WORKSHOP PRESENTATION

ON

PSEUDO CODES AND FLOWCHARTS

BY

MR PHIRI G.W

HEAD OF DEPARTMENT – MATHEMATICS

CHIZONGWE TECHNICAL SECONADRY SCHOOL

B.A.Ed (UNZA), DIP.Ed – (Nkrumah),

Mr. Phiri G .W- HOD Mathematics, Chizongwe Technical Secondary School Page 1
Algorithms And Flowcharts 12thAPRIL, 2018

A. COMPUTER
WHAT IS A COMPUTER

A computer is a machine that can perform calculation and logical decision.


SPECIFIC OUT COMES
Identify Basic elements of a computer.
Name Input, output and storage devices.
Draw flow charts.
Write pseudo codes.

BASIC ELEMENTS OF COMPUTER

Computer
Monitor Keyboard
case

Used to display text and images. Used to input data


Used to protect and house the
mother body

CD ROM
MOUSE
HARD DISK DRIVE

Used to locate text used to store data or information. Used to output data to disc

Mr. Phiri G .W- HOD Mathematics, Chizongwe Technical Secondary School Page 2
Algorithms And Flowcharts 12thAPRIL, 2018

. THE INPUT DEVICES OF A COMPUTER


These are devices which are used to input or enter data or information required to be processed
in computer after an instruction. Examples of input devices are; keyboard, mouse, microphone,
scanner, digital camera, sensors, Optical Character Recognition (OCR)
THE CENTRAL PROCESSING UNIT (CPU)).
Has three main parts; Arithmetic and logic unit, control unit and memory.
a) Arithmetic and logic unit;
This part is responsible for performing calculations such addition, subtraction,
multiplication and division and logical operations such as comparison of information.
b) Control unit
This part of the CPU acts as a supervisor which directs operations of the processor. Its
job is to provide timing and control signals through which it controls communication
between input and output devices.
c) Memory
These are temporal storage found inside the computer which losses data when the
computer is powered off. The following are types of memory;
i) RAM – Random Access Memory
ii) Registers-prepares data for the processor to process into information.

OUT PUT DEVICES


These are devices which are used by computers to output text, images, videos, sound and
graphics, for example, printer’s monitors and speakers.

STORAGE DEVICES
These are devices that are used to store information that has been produced by the computer, for
example Hard Disc Drive, Magnetic tape ( cassette tape) flash or memory stick, compact Disc
(CD) or Digital Vase tail Disc(DVD).

B. ALGORITHMS AND FLOW CHARTS

Mr. Phiri G .W- HOD Mathematics, Chizongwe Technical Secondary School Page 3
Algorithms And Flowcharts 12thAPRIL, 2018

An algorithm is a series of steps used to perform operations which lead to the


solution (a step by step procedure of carrying out an instruction).
What is a flowchart?
A flow chart is a graphical representation of the problem solving process. It gives a step by step
procedure for solution of a problem.

ELEMENTS OF A FLOWCHART
 Various geometrical shaped boxes represent the steps of a solution.
 The boxes are connected by directional arrows to show the flow of a solution.
FLOW CHART SYMBOLS
NAME SHAPE PURPOSE

OVAL Terminator

Used to start/stop
Or Begin /end a program
Elongated rectangle
Terminator

Parallelogram Used to input/ output, print,


Input/Output
display or publish data.

Diamond Decision Used to make a decision of


either Yes or No/ True or
False

Processing Box Used to carry out an action


Rectangle
Used to show the direction of
Arrow flow problem solving
process.

Mr. Phiri G .W- HOD Mathematics, Chizongwe Technical Secondary School Page 4
Algorithms And Flowcharts 12thAPRIL, 2018

USES OF A FLOW CHART


 To specify the method of solving a problem
 To plan the sequence of a computer program
 Communicate the ideas, and solutions.
GENERAL RULES OF A FLOW CHART
 All boxes of a flow chart are connected with arrows.
 Flow charts start with a terminator and end with a terminator.
 Flow chart symbols have an entry point on top with no other point. The exit point of all
flow chart symbols is at the bottom except for decision box.
 The decision box has two exit points, these can be on the sides or bottom and one side.
 Generally flow charts will flow from top to bottom.
 Sub route have their own independent flow charts.

ARITHMETIC EXPRESSIONS
+ Addition
- Subtraction
∗ Multiplication (× is not used as it can be confused with X)
/ Division (÷ is not used in computer programming)

Mr. Phiri G .W- HOD Mathematics, Chizongwe Technical Secondary School Page 5
Algorithms And Flowcharts 12thAPRIL, 2018

Example 1.
Draw a flow chart to compute the volume of cuboid given length ‘𝑙’ breadth ‘b’ and height
‘h’.
Solution

EXAMPLE 2
Draw a flow chart to calculate the total surface area of a cylinder given radius ‘r’ and height ‘h’.
Solution

Mr. Phiri G .W- HOD Mathematics, Chizongwe Technical Secondary School Page 6
Algorithms And Flowcharts 12thAPRIL, 2018

4. The diagram below, is an incomplete program


flowchart to calculate the curved surface area, S of a
cone with base radius, ‘r’ and slant height‘𝑙’. Complete the flow chart below by writing
appropriate statements in the blank symbols

Mr. Phiri G .W- HOD Mathematics, Chizongwe Technical Secondary School Page 7
Algorithms And Flowcharts 12thAPRIL, 2018

3. The flow chart below shows how a company calculate agent’s commission based on monthly
sales. Mrs Nkhata an agent of this company, made sales of K 4, 780.00 in one month.

i) Calculate Mrs Nkhata’s commission in that month.


ii) If she made sale of K 8800 in the next month how did she receive as commission?
SOLUTION
𝟏
i) 𝑪𝒐𝒎𝒎𝒊𝒔𝒔𝒊𝒐𝒏 = 𝑲𝟒𝟕𝟖𝟎 × 𝟏𝟎

= K478
𝟏
ii) 𝑪𝒐𝒎𝒎𝒊𝒔𝒔𝒊𝒐𝒏 = 𝑲 𝟖𝟖𝟎𝟎 × 𝟓

= K 1,760
NOTE
 The key part of flow chart construction is to identify the formula of the concept being
examined in the question that has to be put in the processing box.
 Identify the domain (variables) of the formula to be entered in the input box.
 Then identify all stages taken in the identified problem solving.
 Display or print the result in the output box after the processing has been done.

Mr. Phiri G .W- HOD Mathematics, Chizongwe Technical Secondary School Page 8
Algorithms And Flowcharts 12thAPRIL, 2018

EXERCISE
1. Given that the base of a triangle is ‘b’ and its perpendicular height is ‘h’. Complete the
flow chart below, which is for calculating and displaying its area.

2. Given three numbers a , b and c , complete the flow chart below to calculate the mean (𝑋̅)
of the numbers.

3. With the use of flow charts find the inverse of the function 𝑓(𝑥) = 3𝑥 − 4.
4. Draw a flow chart on how to calculate the length of the hypotenuse side in a right angled
triangle, and then write the program in BASIC.

Mr. Phiri G .W- HOD Mathematics, Chizongwe Technical Secondary School Page 9
Algorithms And Flowcharts 12thAPRIL, 2018

5. The diagram below shows a flow chart for a program to calculate tax on an income.

Complete the table below

INCOME TAX
K 3, 700
K 2, 400
K 8,000

3. Write an algorithm in pseudo code to calculate the volume of a given cylinder with radius
“r” and height “h”.

Mr. Phiri G .W- HOD Mathematics, Chizongwe Technical Secondary School Page 10
Algorithms And Flowcharts 12thAPRIL, 2018

PSEUDO CODES
A pseudo code is an artificial and informal language that helps programmers develops
algorithms. A pseudo code may be English or any other spoken language or a combination of
the two.

GUIDE LINES WHEN DESIGNING PSEUDO CODES.


a) The statements must be short, clear and readable.
b) The statement must not have more than one meaning i.e. should not be ambiguous.
c) The pseudo code lines should be clearly outlined and indented.
d) A pseudo code should show clearly the start and stop of executable statements and the
control structures.
NOTE
 Pseudo codes simply show the steps that are taken in a computation process.

EXAMPLE 1

Write down an algorithm to calculate the average of five numbers; 13,88,37,19 and 18

SOLUTION;

PSEUDO CODE
Begin
Input; 13,88,37,19,18.
𝐴𝑣𝑒𝑟𝑎𝑔𝑒 = ( 13 + 88 + 37 + 19 + 18)/5
Print; average,
End program
EXAMPLE 2
Write an algorithm to calculate the volume of a given cuboid with length ‘L’ breadth ‘b’ and
height ‘h’.
SOLUTION
Start
Enter, 𝑙
Enter, 𝑏
Enter, ℎ
𝑉𝑜𝑙𝑢𝑚𝑒 = 𝑙 ∗ 𝑏 ∗ ℎ
Print; volume
Stop

Mr. Phiri G .W- HOD Mathematics, Chizongwe Technical Secondary School Page 11
Algorithms And Flowcharts 12thAPRIL, 2018

EXAMPLE 3.
Write an algorithm to calculate the volume of a given cylinder with radius ‘r’ and ‘height ‘h’.
Pseudo code.
Begin program
Input; 𝑟,
Input; ℎ
const; 𝜋.
𝑉𝑜𝑙𝑢𝑚𝑒 = 𝜋 ∗ 𝑟 ∗ 𝑟 ∗ ℎ
Print; volume
End program.

ARITHMETIC OPERATORS
Following table shows all the arithmetic operators supported by Pascal. Assume variable A
holds 10 and variable B holds 20, then:
Table 1.

OPERATOR DESCRIPTION EXAMPLE


+ Adds two operands 𝐴 + 𝐵 will give 30
− Subtracts second operand from the first 𝐴 − 𝐵 will give −10
∗ Multiplies both operands 𝐴 ∗ 𝐵 will give 200
/ Divides numerator by denominator B div A will give 2

DECISION MAKING IN THE DECISION BOX


This section shows the decision making structure found in Pascal:
Decision making structures require that the programmer specify one or more conditions to be
evaluated or tested by the program, along with a statement or statements to be executed if the
condition is determined to be true, and optionally, other statements to be executed if the
condition is determined to be false.
Following is the general form of a typical decision making structure found in most of the
programming languages:
Pascal programming language provides the following types of decision making statement.

Mr. Phiri G .W- HOD Mathematics, Chizongwe Technical Secondary School Page 12
Algorithms And Flowcharts 12thAPRIL, 2018

Table 2.

Statement Description
if - then statement An If - then statement consists of a Boolean expression
followed by one or more statements.
If-then-else statement An if - then statement can be followed by an optional else
statement, which executes when the Boolean expression is
false.
nested if statements You can use one if or else if statement inside another if or
else if statement(s).
case statement A case statement allows a variable to be tested for equality
against a list of values.
case - else statement It is similar to the if-then-else statement. Here, an else term
follows the case statement.
nested case statements You can use one case statement inside another case
statement(s).

The IF statement

There are three forms of the IF forms.

IF-THEN statement

The IF…THEN statement is used when only one option is available, the simplest form of
control statement, frequently used in decision making and changing the control flow of the
program execution. Its structure is as shown below.

IF < condition> THEN

Statement(s)

ENDIF

Mr. Phiri G .W- HOD Mathematics, Chizongwe Technical Secondary School Page 13
Algorithms And Flowcharts 12thAPRIL, 2018

EXAMPLE.

Write a pseudo code and draw a flow chart for a program that determines if a person is allowed
to vote or not, given that a person can only vote if he or she is over 18 years of age.
Pseudo code.

Start
Enter; age
If age >18 then,
Print; vote
Else
Stop
Draw a flow chart diagram for the information above.

SOLUTION

Figure 2

Mr. Phiri G .W- HOD Mathematics, Chizongwe Technical Secondary School Page 14
Algorithms And Flowcharts 12thAPRIL, 2018

1. IF-THEN-ELSE STATEMENT
An IF…THEN statement can be followed by an optional ELSE statement, is used if only two
options are available. It’s a structure is as follows;

IF < condition> THEN

Statement(s)

ELSE

Statement(s)

ENDIF

EXAMPLE 2

The program below is given in form of a pseudo code.

Start

Enter radius

If radius < 0, THEN

Display “error message” and reenter positive radius.

Else

Enter height

If height < 0 THEN

Display “error message” and reenter positive height

Else
1
Volume = 3 ∗π∗square radius ∗height.

End if

Display Volume

Stop.

Draw a corresponding flowchart for the information given above

Mr. Phiri G .W- HOD Mathematics, Chizongwe Technical Secondary School Page 15
Algorithms And Flowcharts 12thAPRIL, 2018

SOLUTION

EXAMPLE 3.

The program below is given in form of a pseudocode.

Start

Enter radius

If radius is less than 0 , THEN

Display “error message” radius must be positive.

Else

Enter height,

If height is less than 0, THEN

Display, “ error message” height must be positive.

Else

Surface Area = 2 ∗ 𝜋 ∗ 𝑟(𝑟 + ℎ)

Endif

Display surface Area.

Stop

Mr. Phiri G .W- HOD Mathematics, Chizongwe Technical Secondary School Page 16
Algorithms And Flowcharts 12thAPRIL, 2018

SOLUTION

The NESTED IF statement is used if more than two options are available. It is always legal in
Pascal programming to nest if-else statements, which means you can use one if or else if
statement inside another if or else if statement(s). Pascal allows nesting to any level, however, if
depends on Pascal implementation on a particular system. The structure of the NESTED IF
statement is shown below. The format of the flow chart for a NESTED IF statement is as shown

below; n

Mr. Phiri G .W- HOD Mathematics, Chizongwe Technical Secondary School Page 17
Algorithms And Flowcharts 12thAPRIL, 2018

PSEUDOCODE FOR NESTED IF STATEMENT

IF< condition> THEN,

Statements

ELSE

IF< condition> THEN

Statements

ELSE

IF <condition> THEN

Statements

ENDIF

ENDIF

ENDIF

EXAMPLE 3

The information below would be used to categorize student’s performance in mathematics.

80 and above:” remark distinction”

Between 60 and 70; remark “meritorious”

Between 40and 59; remark,” credit”

Between 0 and 39; remark,” poor”.

a) Write a pseudo code for information above


b) Draw a flow chart for the pseudo code program in part a above.

Mr. Phiri G .W- HOD Mathematics, Chizongwe Technical Secondary School Page 18
Algorithms And Flowcharts 12thAPRIL, 2018

SOLUTION

a) PSEUDO CODE
Start
Enter; mark x
IF x≥ 80, then
Remark= distinction
ELSE
IF 60≤ 𝑥 ≤ 79, then
Remark=meritorious
ELSE
IF 40≤ 𝑥 ≤ 59, then
Remark= credit
ELSE
Remark =poor
ENDIF
ENDIF
ENDIF
Stop.
b) Flow chart

Mr. Phiri G .W- HOD Mathematics, Chizongwe Technical Secondary School Page 19
Algorithms And Flowcharts 12thAPRIL, 2018

EXERCISE
1. Study the program below and answer the questions that follow.
Start
Enter value base
If base < 0, THEN
Display “error message “and re-enter positive base.
Else, enter height
If height < 0, THEN
Display “error message” and re-enter positive height
Else,
1
Area= 2 *base*height

End if
Display area
Stop.
2. The program below is given in form of a pseudo code.
Start
Enter, radius
If radius < 0, THEN
Display “error message” and re-enter positive radius
Else,
Enter, height
If height < 0, THEN
Display “error message” and re-enter positive height.
Else,
𝑣 = 𝑏𝑎𝑠𝑒 𝑎𝑟𝑒𝑎 × ℎ𝑒𝑖𝑔ℎ𝑡
End if
Display volume.

Stop.

Draw a corresponding flow chart to calculate volume of a cylinder corresponding


to the information above.

Mr. Phiri G .W- HOD Mathematics, Chizongwe Technical Secondary School Page 20
Algorithms And Flowcharts 12thAPRIL, 2018

3. Draw a flow chart to represent the equation 𝑦 = 4(2𝑥 + 5). Use the flow chart to find the
value of y when 𝑥 = 7. [4]
4. You are given the following criteria to award a grade in an examination. If a mark is;
“Greater than or equal to 80” distinction
“60 to 70”, credit.
“40 to 59” pass
“Less than 40”, failure.

Write an algorithm in pseudo code to test and award a grade. [5]

5. Draw a flow chart and write a program to calculate the average, height of four pupils in
your class with data: 1.5m, 1.69 m, 1.6m, 1.72m. [3]
6. Draw a flow chart and write a program, to calculate volume of a sphere given radius as
data.

Mr. Phiri G .W- HOD Mathematics, Chizongwe Technical Secondary School Page 21
Algorithms And Flowcharts 12thAPRIL, 2018

EXAMPLE 1

The program below is given in the form of a flow chart.

Write a pseudo code corresponding to the flow chart program above.

SOLUTION
Start
Enter 𝑎, 𝑟
If |r |< 0, THEN
𝑎
𝑆∞ = 1−𝑟

End if
Display Sum to infinity

Stop.

Mr. Phiri G .W- HOD Mathematics, Chizongwe Technical Secondary School Page 22
Algorithms And Flowcharts 12thAPRIL, 2018

EXAMPLE 2

2. Study the flow chart below

Write a pseudo code corresponding to the flow chart program above

SOLUTION

Start
Enter r
If r < 0, THEN
display error message r must be positive
Else
1
𝐴𝑟𝑒𝑎 = ∗ 𝑟 ∗ 𝑟 ∗ 𝑠𝑖𝑛 𝜃
2

Endif

Display area

Stop.

Mr. Phiri G .W- HOD Mathematics, Chizongwe Technical Secondary School Page 23
Algorithms And Flowcharts 12thAPRIL, 2018

3. Study the flow chart below.

Write down a pseudo code program corresponding to the flow chart program above.

SOLUTION
Start

Sum =0

Count =0

Repeat

Enter number

Sum = sum + number

Count = count +1

Until count =10

Mean = sum / 10.

Endif

Display mean

Stop

Mr. Phiri G .W- HOD Mathematics, Chizongwe Technical Secondary School Page 24
Algorithms And Flowcharts 12thAPRIL, 2018

EXERCISE

1. Study the flow chart program below

Write down a pseudo code program for the flow chart on how to calculate profit or Loss.

2. Study the flow chart Program below.

Write down the pseudo code program corresponding to the flow chart program above.
Mr. Phiri G .W- HOD Mathematics, Chizongwe Technical Secondary School Page 25
Algorithms And Flowcharts 12thAPRIL, 2018

3. Study the flow chart program below.

Write down a pseudo code progarm corresponding to the flow chart program above.

Mr. Phiri G .W- HOD Mathematics, Chizongwe Technical Secondary School Page 26

Das könnte Ihnen auch gefallen