Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
50% found this document useful (2 votes)
280 views

Algorithms and Flowcharts

The document discusses algorithms and flowcharts. It defines an algorithm as a set of step-by-step instructions to solve a problem and notes they can be represented through pseudocode or flowcharts. The document then explains the components of algorithms, pseudocode, and flowcharts including inputs, outputs, conditions, and loops. It provides examples of maze algorithms and emphasizes the importance of designing algorithms carefully.

Uploaded by

Asfia Asif
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
50% found this document useful (2 votes)
280 views

Algorithms and Flowcharts

The document discusses algorithms and flowcharts. It defines an algorithm as a set of step-by-step instructions to solve a problem and notes they can be represented through pseudocode or flowcharts. The document then explains the components of algorithms, pseudocode, and flowcharts including inputs, outputs, conditions, and loops. It provides examples of maze algorithms and emphasizes the importance of designing algorithms carefully.

Uploaded by

Asfia Asif
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 78

Algorithms and

Flowcharts
Chapter 4
Information Technology 9626 – Chapter 4 30/03/2022 2

Objectives
• write a basic algorithm that demonstrates a decision-making process.
• use conditional branching within an algorithm.
• use loops within an algorithm.
• use nested loops within an algorithm.
• include procedures/subroutines within an algorithm.
• edit a given algorithm
• write an algorithm to solve a given problem.
• edit a given flowchart.
• draw a basic program flowchart that demonstrates a decision-making process.
• draw a program flowchart to solve a given problem.
• identify errors in an algorithm or program flowchart for a given scenario.
Information Technology 9626 – Chapter 4 30/03/2022 3

Program

• It is a set of instruction that tells the computer what to do, computer works
according to the given instructions in the program
• Written in a programming language (C++, Python, Java)
• Written by programmer
Information Technology 9626 – Chapter 4 30/03/2022 4

pseudocode

Algorithm Final
Program

Flowchart
Information Technology 9626 – Chapter 4 30/03/2022 5

Algorithm
• set of instructions sequenced to solve a problem or to represent a calculation
• a data or program flowchart without the boxes.
• a list of precise steps.
• The order in which these steps are carried out is always crucial to the way an
algorithm works.
• We start at the first line of the algorithm and work downwards to the final instruction.
• Input -> Process -> Output
• Data -> Process -> Information.
Information Technology 9626 – Chapter 4 30/03/2022 6

Algorithm

step-by-step instructions for solving a problem

used to decide what action should be taken in a particular


circumstance.
Idea developed by Countess Ada Augusta Lovelace, the first
programmer in history
Information Technology 9626 – Chapter 4 30/03/2022 7
Information Technology 9626 – Chapter 4 30/03/2022 8

Algorithm
Everyday Life example:
Recipe for baking a cake
Information Technology 9626 – Chapter 4 30/03/2022 9

Algorithm
Computer based example

A set of instructions given to a computer to put together


different sources of INFORMATION and generate a
Defined as a
RESULT. COMPUTER

INPUT -> PROCESS -> OUTPUT


Information Technology 9626 – Chapter 4 30/03/2022 10

Advantages of Algorithms

• Reduced complexity: While writing the algorithm, we can focus on


solving the problem instead of the concentrating on a particular language.
• Increased flexibility: Algorithm is written so that the code may be
written in any language like, Visual Basic, Java, C++ etc.
• Ease if understanding: You don’t have to understand a particular
programming language to understand an algorithm, as it is written in
English like manner
Information Technology 9626 – Chapter 4 30/03/2022 11

Understanding Algorithms using LOGO

• A simple form of an algorithm is a series of steps to move someone (or


something) through a maze. The instructions you can use are:
• FDx : Move forward x spaces
• BKx : Move backward x spaces
• Rty : Turn right y degrees
• LTx : Turn left y degrees
Information Technology 9626 – Chapter 4 30/03/2022 12

Maze 1

FD7,RT90,FD6,LT90,FD2,RT90,FD1
Information Technology 9626 – Chapter 4 30/03/2022 13

Maze 2

FD4, LT90, FD2, RT90, FD3, LT90, FD5, LT90, FD7


Information Technology 9626 – Chapter 4 30/03/2022 14

Maze 3
FD3
RT90
FD4
LT90
FD
RT90
FD4
RT90
FD4
RT90
FD4
LT90
FD3
LT90
FD5
LT90
FD2
Information Technology 9626 – Chapter 4 30/03/2022 15

Questions

• What is meant by an algorithm?


• How can an algorithm be represented?
Information Technology 9626 – Chapter 4 30/03/2022 16

Design of Algorithm
• An algorithm must be carefully designed.
• Problem should be broken down into simple and meaningful steps
• Steps should be descriptive and written in simple english
• Should caters for all possible scenarios.
• Any steps that rely on decisions being made must be dealt with in sequence and
the conditions must be clear and unambiguous.
• not all instructions may be carried out for a particular scenario, but they still have
to be written in a way that allows for all possible scenarios.
Information Technology 9626 – Chapter 4 30/03/2022 17

Ways to write an algorithm

• everyday natural language, pseudocode, flowcharts or programming


languages.
• Natural language can sometimes lead to instructions which are open to
interpretation and so tends not to be used in very complex algorithms. (think
of the recipe of baking a cake)
• Pseudocode and flowcharts are structured ways of writing algorithms.
• Programming languages are primarily intended for converting algorithms
to a form that can be understood and executed by a computer.
Information Technology 9626 – Chapter 4 30/03/2022 18

Pseudocode

• Pseudocode is independent of any programming language.


• Once the pseudocode is written and checked to make sure that it solves
the problem, it would be fairly straightforward to translate it into any
programming language
• It is intended for human reading rather than machine reading.
Information Technology 9626 – Chapter 4 30/03/2022 19

Pseudocode
• Pseudocode consists of short, readable and formally styled English languages used for
explain an algorithm.
• It does NOT include details like variable declaration, subroutines.
• It is easier to understand for the programmer or non programmer to understand the
general working of the program, because it is not based on any programming language.
• It gives us the sketch of the program before actual coding.
• It is not machine readable. (Computer cannot understand it)
• Pseudo code can’t be compiled and executed.
• There is no standard syntax for pseudo code.
Information Technology 9626 – Chapter 4 30/03/2022 20

Advantages of Pseudocode

• It can be easily modified as compared to flowchart.


• Converting a pseudo code to programming language is very easy as
compared with converting a flowchart to programming language.
Information Technology 9626 – Chapter 4 30/03/2022 21

Disadvantages of Pseudocode

• It does not provide visual representation of the program’s logic.


• There are no specific standards for writing pseudo codes.
• It cannot be run as a program.
• For a beginner, It is more difficult to follow the logic or write pseudo
code as compared to flowchart.
Information Technology 9626 – Chapter 4 30/03/2022 22

Flowcharts

• A flowchart is a diagram that uses specific shapes and arrows to


produce an algorithm that someone can then follow.
Information Technology 9626 – Chapter 4 30/03/2022 23

Structure of Flowcharts

• There are set symbols throughout a flowchart. You will be introduced to


these in each section. Every flowchart begins and ends with a terminator
box (a Start box at the beginning and a Stop box at the end).

Start

Stop
Information Technology 9626 – Chapter 4 30/03/2022 24

I/O

• Input
INPUT INPUT x

• Output

OUTPUT OUTPUT “Hello”


Information Technology 9626 – Chapter 4 30/03/2022 25

Algorithm Pseudocode Flowchart

A sequence of instructions used Language representation of Graphical representation of


to solve a problem algorithm algorithm

No knowledge of programming
User needs knowledge to write No knowledge of program to
language needed to understand
algorithm draw or understand flowchart
or write a pseudocode
Information Technology 9626 – Chapter 4 30/03/2022 26

Example – find the average of 3 numbers

Algorithm using Algorithm using


Algorithm In a programming language
Pseudocode Flowchart

INPUT X,Y,Z

1. Get the 3 numbers


from user 1. INPUT X,Y,Z Sum  X+Y+Z
2. Add all 3 numbers 2. Sum  X+Y+Z
3. Divide the sum by 3. 3. Average  Sum/3
4. Result will be average 4. OUTPUT Average Average  Sum/3
of 3 numbers
OUTPUT
Average
Information Technology 9626 – Chapter 4 30/03/2022 27

Writing the Pseudocode


• Statements lines are indented (usually by three spaces) to indicate that they are
contained within a statement in a previous line.
• Keywords are in upper-case, e.g. IF, REPEAT, PROCEDURE.
• Identifiers (variable names) are in mixed case with upper-case letters indicating
the beginning of new words, for example NumberOfPlayers.
• Meta-variables – symbols in the pseudocode that should be substituted by other
symbols are enclosed in angled brackets < >
• Comments are preceded by two forward slashes //. The comment continues until
the end of the line. For multi-line comments, each line is preceded by //.
Information Technology 9626 – Chapter 4 30/03/2022 28

Line numbers

• Where it is necessary to number the lines of pseudocode so that they


can be referred to, line numbers are presented to the left of the
pseudocode with sufficient space to indicate clearly that they are not
part of the pseudocode statements.
• Line numbers are consecutive, unless numbers are skipped to indicate
that part of the code is missing. This will also be clearly stated.
Information Technology 9626 – Chapter 4 30/03/2022 29

Writing the algorithm using pseudocode

• It has statements which have linear progression, that is the result of


one statement is used in the next.
• conditional branching, such as IF...THEN...ELSE or
CASE...ENDCASE, which results in a decision being made between
two or more courses of actions.
• use of loops such as WHILE... ENDWHILE or REPEAT.. .UNTIL.
Information Technology 9626 – Chapter 4 30/03/2022 30

Key Actions

key actions that are performed and common terms used in pseudocode:

inputting data: INPUT or READ


outputting data: WRITE or PRINT
calculation: +, - , * , /
comparison: >, < , =, <>
setting values: , =
Information Technology 9626 – Chapter 4 30/03/2022 31

Variable

One of the most basic units of an algorithm is a variable.


an area of the computer's memory that stores one item of data, such as a number.
The algorithm designer is able to choose the names of the variables,
making it easier, and also it is possible to write an algorithm where we can
change the values (hence the name VARIABLE) each time we work the
algorithm.
Storing a value in a variable is called assignment

Arrow symbol =
Information Technology 9626 – Chapter 4 30/03/2022 32

Assigning values to variables

Most commonly used names are x,y,z although its always better to name
it appropriately, according to the value it will used for (marks, number,
average etc)

X = 109
Addition = X + Y + Z
Information Technology 9626 – Chapter 4 30/03/2022 33

Naming variables

INPUT X,Y // input 2 numbers, X and Y


Z = X*Y // Multiply the two numbers and store in
variable Z (also called assignment)
PRINT Z // print or output the value in Z
Information Technology 9626 – Chapter 4 30/03/2022 34

Example
Problem: An algorithm needs to ask a user to enter their name. It should take their name as an input and then welcome
them by name for example “Welcome Sasha”
Draw a flow chart for the algorithm

Algorithm steps:
• Ask them to enter their name
• Take their name as input.
• Welcome them by their name
• Identify the types of the symbols
• Ask them to enter their name (output)
• Take their name as input (input and store in variable )
• Welcome them by their name (output including variable)
• Draw the flow chart following the steps identified.
• Write the pseudocode
Information Technology 9626 – Chapter 4 30/03/2022 35

Pseudocode Flowchart

Start
OUTPUT “Enter your name”

OUTPUT “Enter your name”


INPUT name

INPUT name
OUTPUT “Welcome” & name

OUTPUT “Welcome” & name

Stop
Information Technology 9626 – Chapter 4 30/03/2022 36

Processes

• A process is an action that is performed.


• In a program it usually means that a change is being made to something.
If you have a number stored in a variable, you could change this; you
might add something to it, or subtract from it.
• Flowchart representation :

Process
Information Technology 9626 – Chapter 4 30/03/2022 37

Processes
Operator Description Example
+ Addition myNumber = 10 + 3

– Subtraction myNumber = 10 – 3

* Multiplication myNumber = 10 * 3

/ Division myNumber = 10 / 3

^ Power of myNumber = 10 ^ 3

MOD Modulus division (gives the remainder) 10 MOD 3 = 1


20 MOD 8 = 4
DIV Integer division (gives the whole number) 10 DIV 3 = 3
20 DIV 6 = 2
Information Technology 9626 – Chapter 4 30/03/2022 38

Example 2

• Write an algorithm in pseudocode and make its flowchart.


• It would take two number, multiply them and output the answer.
Information Technology 9626 – Chapter 4 30/03/2022 39

Example 2 Pseudocode

PRINT “Enter first number: ”


INPUT first
PRINT “Enter second number: ”
INPUT second
ans  first*second
PRINT "The answer is", ans
Information Technology 9626 – Chapter 4 30/03/2022 40
A
Flowchart B

Start OUTPUT “Enter


second number” ans = first*second

OUTPUT “Enter first number”


INPUT second
OUTPUT “The answer is “ & ans
INPUT first
B
Stop
A
Information Technology 9626 – Chapter 4 30/03/2022 41

BASIC program

If we use 42 and 36 as our numbers, answer would be 42*


36, which is 1512.
Information Technology 9626 – Chapter 4 30/03/2022 42

Theoretical Questions

5) What is a process?
6) What action does the ^ operator process?
7) Write a statement to multiply 3 by 7
8) Describe the stages in this flowchart.
Information Technology 9626 – Chapter 4 30/03/2022 43

Practical Activity

1 Convert the algorithm in Question 8 into pseudocode.


2 An algorithm needs to take 3 numbers as input. Add them together and
output the result. Then divide the total by the quantity of numbers and output the
average.
Draw a flowchart for this algorithm.
3 Convert your algorithm from part 2 into pseudocode.
Information Technology 9626 – Chapter 4 30/03/2022 44

Conditional branching (IF-Else)

• referred to as selection or Decision.


• certain statements are carried out depending on the value stored within a
particular variable name.
• There are generally considered to be two types,
• IF.. .THEN.. .ELSE
• CASE.. .ENDCASE
Information Technology 9626 – Chapter 4 30/03/2022 45

It is a statement that involves a comparison and is


either true or false. If the result is true, then some
code is run, if it is false then either different code is
run or some code is skipped.
A comparison needs two sides of the argument,
and a comparison symbol.
Either side of the operator can be a number or a
variable.

Table 4.2 shows common comparison operators.


Information Technology 9626 – Chapter 4 30/03/2022 46
Operator Description Example
>  Greater than 10 > 5 is true
5 > 10 is false

<  Less than 10 < 5 is false


5 < 10 is true

>= Greater than or equal to 10 >= 5 is true


10 >= 10 is true
5 >= 10 is false

<= Less than or equal to 10 <= 5 is false


10 <= 10 is true
5 <= 10 is true

= Equals to 10 = 5 is false
10 = 10 is true

!= Not equal to 10 != 10 is false


or 5 <> 10 is true
<> 
Information Technology 9626 – Chapter 4 30/03/2022 47

Selection in Flowchart

• Single selection
• The selection must be in the form of a question, for example ‘is 10 >
5?’ This is because it has two results: true and false.
• The selection flowchart symbol is a diamond.

No YE
DECISION S
Information Technology 9626 – Chapter 4 30/03/2022 48
Information Technology 9626 – Chapter 4 30/03/2022 49

IF age > 18 THEN


OUTPUT("You are old enough")
ENDIF
-----------------------------------------------------------------
IF age > 18 THEN
OUTPUT("You are old enough")
ELSE
OUTPUT("You are not old enough")
ENDIF
Information Technology 9626 – Chapter 4 30/03/2022 50

IF – THEN - ELSE

IF <condition>
THEN
<statement or list of statements to be carried out >
ELSE
<alternative statement or list of alternative
statements to be carried out>
ENDIF
Information Technology 9626 – Chapter 4 30/03/2022 51

Multiple Decisions

• The first decision is ‘If age is


less than 10?’ If it is not, then
it goes straight to another
decision, and asks whether
age is less than 20.
Information Technology 9626 – Chapter 4

Multiple Decisions Is cost


>= 100 ?
YES
OUTPUT “That is far too expensive”

IF cost >= 100 THEN No

OUTPUT "That is far too expensive" YES


Is cost OUTPUT “That is bit too expensive”
>= 80 ?
ELSEIF cost >= 80 THEN
OUTPUT "That’s a bit too expensive" No

ELSEIF cost >= 60 THEN Is cost


YES
OUTPUT “That is reasonable”
>= 60 ?
OUTPUT "That is reasonable"
ELSEIF cost >= 40 THEN No

OUTPUT "That looks like a good deal" YES


Is cost
OUTPUT “That looks like a good deal”
>= 40 ?
ELSE
OUTPUT "I think that is too cheap" No

ENDIF OUTPUT “I think that is too cheap”


Information Technology 9626 – Chapter 4 30/03/2022 53

Decision Making
Information Technology 9626 – Chapter 4 30/03/2022 54

IF
Information Technology 9626 – Chapter 4 30/03/2022 55

IF - Else
Information Technology 9626 – Chapter 4 30/03/2022 56

IF – Else IF
Information Technology 9626 – Chapter 4 30/03/2022 57

Nested IF
Information Technology 9626 – Chapter 4 30/03/2022 58

Nested IF
Information Technology 9626 – Chapter 4 30/03/2022 59

NESTED Ifs Example


1 INPUT mark
2 IF mark < 75
3 THEN
4 IF mark < 60
5 THEN
6 IF mark < 40
7 THEN
8 PRINT "Sorry you have failed"
9 ELSE
10 PRINT "You have passed"
11 ENDIF
12 ELSE
13 PRINT "Well done. You have been awarded a merit"
14 ENDIF
15 ELSE
16 PRINT "Congratulations you have been awarded a distinction"
17 ENDIF
Information Technology 9626 – Chapter 4 30/03/2022 60

Flowchart Is Mark<
No
OUTPUT “Congratulations you have
75 ? been awarded a distinction”

YES

Is Mark< No
OUTPUT “Well done. You have been
60 ? awarded a merit”

YES

Is Mark<
No
OUTPUT “You have passed”
40 ?

YES

OUTPUT “You have failed”


Information Technology 9626 – Chapter 4 30/03/2022 61
Information Technology 9626 – Chapter 4 30/03/2022 62

SELECT CASE
SELECT <identifier>
CASE <value 1>
<statement or list of statements>
CASE <value 2>
<statement or list of statements>
DEFAULT
<statement or list of statements>
Information Technology 9626 – Chapter 4 30/03/2022 63

CASE Flowchart
Information Technology 9626 – Chapter 4 30/03/2022 64

Constant values

Depending on the value entered, either one statement or several statements


can be carried out.
SELECT menuChoice
CASE 1:
OUTPUT "You chose 1"
CASE 2:
OUTPUT "You chose 2"
CASE 3:
OUTPUT "You chose 3"
Information Technology 9626 – Chapter 4 30/03/2022 65

Default Case
There is also a default keyword for CASE, the code in this will run if none of the conditions have been met.

OUTPUT “Enter value 1”


INPUT value1 (10)
OUTPUT “Enter value 2”
INPUT value2 (11)
OUTPUT “Enter the calculation symbol” (/)
INPUT symbol
SELECT symbol
CASE "+":
result = value1 + value2
CASE "-":
result = value1 – value2
CASE "*":
result = value1 * value2
CASE "/":
result = value1 / value2
CASE DEFAULT:
OUTPUT "Invalid symbol“
result = “Not valid”
OUTPUT result
Information Technology 9626 – Chapter 4 30/03/2022 66

CASE Example
INPUT grade
SELECT grade
CASE ‘A’ :
X = 75
Y = 100
CASE ‘B’ :
X = 60
Y = 74
CASE ‘C’ :
X = 59
Y = 40
CASE‘F’ :
X = 39
Y = 0
PRINT "Your mark must have been between " X “and " Y
Information Technology 9626 – Chapter 4 30/03/2022 67
Information Technology 9626 – Chapter 4 30/03/2022 68

Work it out!

Make the flowchart for the previous example


on slide 67
Information Technology 9626 – Chapter 4 30/03/2022 69

Variable values

A CASE statement can perform comparisons other than equals to, but this time you need to
include the variable name to make it clear what the comparison is.

SELECT age
CASE age < 12: OUTPUT "You can watch PG films"
CASE (age >= 12 and age < 15): OUTPUT "You can watch PG and PG12 films"
CASE (age >= 15 and age < 18): OUTPUT "You can watch PG, PG12 and 15 rated films"
CASE age >= 18: OUTPUT "You can watch all films"
Information Technology 9626 – Chapter 4 30/03/2022 70

Practical Activity
1 Describe the stages in the flowchart opposite.
2 Convert the flowchart in part 1 into pseudocode.
3 An algorithm needs to take three numbers as input, and output the smallest.
Draw a flowchart for this algorithm.
4 Convert your flowchart from part 3 into pseudocode.
5 An algorithm needs to take two numbers as input, then subtract the smallest from
the largest.
Create a pseudocode algorithm for the problem using an IF statement.
6 Replace the IF statement in task 5 with a SELECT CASE statement.
Information Technology 9626 – Chapter 4 30/03/2022 71

Loops

• A loop means repetition; doing the same thing several times. Its
technical term is iteration.
• Loops are mainly used in two scenarios:
• You know how many times you want to repeat the code. This might be 10
times, or age times (where age is a variable that stores a value). This is
a count-controlled loop.
• You don’t know the number of times, but want to keep going until
something happens. This is a condition-controlled loop
Information Technology 9626 – Chapter 4 30/03/2022 72

Count controlled

• This type of loop needs a counter to keep track of the number of


repetitions you have done (the number of loops).
• The counter will be a variable.
• Before you start the loop, the counter needs to be set to the starting
value (usually 0 or 1).
• Then inside the code that loops, you need to increment the counter (this
means add 1 to it).
Information Technology 9626 – Chapter 4 30/03/2022 73

Flowchart
Information Technology 9626 – Chapter 4 30/03/2022 74

Pseudocode

A FOR loop is a count-controlled loop. You need to know what number to start


with, and when you will end. The loop increments the number each time through.
In this example, the variable counter starts at 0 and continues until it is 9.
 
FOR counter = 0 to 9
OUTPUT counter
NEXT counter
Information Technology 9626 – Chapter 4 30/03/2022 75

Work it out! Algorithm (FOR)


An algorithm needs to ask the user to input the 10 marks of its students, and then output the average.
Draw a flowchart for the algorithm.
Start by identifying the steps required.
Step 1:Repeat 10 times.
Step 2:Ask the user to input a mark.
Step 3:Input the mark.
Step 4:Add the mark to a total.
Step 5:After all the marks are entered, calculate the average.
Step 6:Output the average.
Information Technology 9626 – Chapter 4 30/03/2022 76

Work it Out! Pseudocode (FOR)

Write the Pseudocode of the Previous Algorithm (FOR


loop)
Information Technology 9626 – Chapter 4 30/03/2022 77

total = 100 Total counter


100 0
Counter = 0 50 1
0 2
WHILE total >= 0 -10 3
OUTPUT It took 3 numbers
INPUT number
total = total – number
counter = counter+1
ENDWHILE
OUTPUT “ It took “ & counter & “numbers”
Information Technology 9626 – Chapter 4 30/03/2022 78

• 1) counter is set to 10
count = 10
• LOOP through count
from 10 to 0
WHILE count >=0
• Output the counter
OUTPUT count
value. count= count – 1
• Decrement the count ENDWHILE
value by 1
• When the value of count
becomes less than 0,
then end the loop
FOR count = 10 to 0
OUTPUT count
NEXT count

You might also like