Algorithms & Flow Charts
Algorithms & Flow Charts
OBJECTIVES
After studying this unit, students would be able
to appreciate the following:
1. Algorithms
2. Pseudocode
3. Flowcharts
4. Types of Errors
1
INTRODUCTION
2
INTRODUCTION
Eg. The program:
• Declare variable a, b;
• Sum = a + b;
• Print Sum; OR Cout << sum;
Pseudocode:
1. The program:
2. Declare variable a and b
3. Input: a,b. Output-Sum
4. Enter a and b
5. a and b must be added
6. Output must be stored in Sum
7. Output must be displayed
3
INTRODUCTION
4
CONCLUSION OF INTRODUCTION
6
QUALITIES OF A GOOD ALGORITHM
7
QUALITIES OF A GOOD ALGORITHM
10
Examples Of Algorithms In Programming
11
Pseudocode
12
Pseudocode
13
Pseudocode
14
Class Exercise:
Pseudocode for Students’ Grading System
• Using KNUST grading system, write down the
pseudocode for determining student’s grades
based on the following criteria and
interpretations
• 70 – 100 – A – Excellent
• 60 – 69 – B – Very Good
• 50 – 59 – C – Good
• 40 – 49 – D – Pass
• 0 – 39 – F – Fail
15
Class Exercise 2:
• A school conducts a 100 mark exam for its
student and grades them as follows:
Grade:
Grade A: Marks>75
Grade B: 75>Marks>60
Grade C: 60>Marks>50
Grade D: 50>Marks>40
• Write Pseudocode of the grades for a student,
using if. Else statement
16
Class Exercise 2:
Proposed Solution
. 1.input student's grade
2.if grade < 100 AND grade > 75 { student got an A }
3.else if grade < 75 AND grade > 60 { student got a B }
4.else if grade < 60 AND grade > 50 { student got a C }
5.else if grade < 50 AND grade > 40 { student got a D }
6.else { student got an F } // didn't fit into any of the
above scenerios, so must be an F
7.print to screen what the student got
17
Pseudocode
19
FLOWCHART IN PROGRAMMING
• Flowchart is a diagrammatic
representation of an algorithm.
• Flowcharts are graphical methods of
outlining program structure and
generally utilise a set of standard
symbols known as the ANSI (American
National Standards Institute) symbols.
20
FLOWCHART IN PROGRAMMING
21
SYMBOLS USED IN FLOWCHARTS
25
Types of Programming Errors
■ Errors are common when writing programs.
■ If a test run turns up problems with the program, we must
debug it, which means we must locate and eliminate the
errors.
■ The two fundamental types of errors that can arise in
coding a program are syntax errors and logic errors.
Syntax Errors
■ A syntax error is the violation of the programming language’s
rules for creating valid statements.
■ It can be caused, for example, by misspelling a keyword or by
omitting a required punctuation mark.
■ Syntax errors are detected by the language software, either
when the invalid statement is typed or when the program is
translated by the computer into machine language.
■ When the software detects a syntax error, normally it issues a
message and highlights the offending statement. Therefore,
syntax errors are often easy to find and correct.
Logic Errors
■ A logic error results from failing to use the proper
combination of statements to accomplish a certain task.
■ It may occur due to faulty analysis, faulty design, or
failure to code the program properly.
■ Logic errors often cause the program to fail to proceed
beyond a certain point (i.e., to crash or hang or freeze) or
to give incorrect results.
– Unlike syntax errors, logic errors are not detected by
the programming language software.
The following are a few kinds of logic errors:
– Use of an incorrect formula to produce a desired
result
– Use of an incorrect sequence of statements to carry
out an algorithm.
– Failure to predict some input data that may lead to an
illegal operation (such as division by zero) when the
program is run (this kind of bug is sometimes called a
runtime error).
The following are summary of errors:
In any programming language errors is common. If
we miss any syntax like parenthesis or semicolon
then we get syntax errors.
31
NEXT TOPIC
32
Reading Assignment
• Write an algorithm to find the factorial of a
number entered by user.
33
References:
1. https://www.programiz.com/article/algorithm-programming
2. http://www.unf.edu/~broggio/cop2221/2221pseu.htm
3. (PDF) Introduction to Algorithms and Pseudocode. Available from:
https://www.researchgate.net/publication/309410533_Introduction_to_A
lgorithms_and_Pseudocode [accessed Aug 30 2018].
4. https://www.proprofs.com/discuss/q/1079057/what-is-the-difference-
between-flowchart-and-pseudocode
34