Download as pdf or txt
Download as pdf or txt
You are on page 1of 34

Programming Techniques

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

Pseudocode is a textual representation of


an algorithm.
It lists out all the logical functions an
algorithm will do including the input and
the output of the program.

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

• A flowchart is the graphical or pictorial


representation of an algorithm with the
help of different symbols, shapes and
arrows in order to demonstrate a
process or a program.

4
CONCLUSION OF INTRODUCTION

Take note that flowchart is normally used to


represent the algorithm in a diagrammatic
manner. OR A flowchart is a diagrammatic
description of an algorithm.

Pseudocode is a method of describing


computer algorithms using a combination of
natural language and programming
language.
5
Algorithm in programming

In programming, algorithms are the set of


well defined instruction in sequence to
solve a problem.

An algorithm should always have a clear


stopping point.

6
QUALITIES OF A GOOD ALGORITHM

• Inputs and outputs should be defined


precisely.

• Each steps in algorithm should be clear


and unambiguous.

7
QUALITIES OF A GOOD ALGORITHM

• Algorithm should be most effective


among many different ways to solve a
problem.

• An algorithm shouldn't have computer


code. Instead, the algorithm should be
written in such a way that, it can be used
in similar programming languages.
8
EXAMPLES OF ALGORITHM

• Algorithm to add two numbers


• Algorithm to find the largest among
three numbers
• Algorithm to find all the roots of
quadratic equation
• Algorithm to find the factorial
• Algorithm to check prime number
9
Examples Of Algorithms In Programming

• Write an algorithm to add two numbers


entered by user.

10
Examples Of Algorithms In Programming

Write an algorithm to add two numbers


entered by user.

11
Pseudocode

• Pseudocode is an artificial and informal


language that helps programmers
develop algorithms.

• Pseudocode is a "text-based" detail


(algorithmic) design tool.

12
Pseudocode

• The rules of Pseudocode are reasonably


straightforward. All statements showing
"dependency" are to be indented.

• These include while, do, for, if, switch.


Examples below will illustrate this notion.

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

• Pseudocode can be very useful for


specifying the logic of a computer
program (or some critical portion of a
program) prior to that program being
written.

• It's also useful for documenting the logic


of a computer program after the fact.
18
Pseudocode

• It can be used to express the high-level


logic of an entire program, the lower-
level details of a core function in an
operating system or run-time library, the
behaviors and methods in an agent-
based or object- oriented program, and
everything in between.

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

• Flowchart are very helpful in writing


program and explaining program to
others.

21
SYMBOLS USED IN FLOWCHARTS

• Different symbols are used for different


states in flowchart, For example:
Input/Output and decision making has
different symbols.

• The table below describes all the symbols


that are used in making flowchart
SYMBOLS USED IN FLOWCHARTS
SYMBOLS USED IN FLOWCHART
EXAMPLES OF FLOWCHARTS IN
PROGRAMMING

• Draw a flowchart to add two numbers


entered by user.

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.

Apart from this we also get run time errors during


the execution of code. In a similar way the errors
are classified as below:
Syntax Errors
Runtime Errors
Logical Errors
Linked Errors
Semantic Errors
QUESTIONS TIME

31
NEXT TOPIC

• Introduction to C++ programming


• Software Installation and fundamental
concepts

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

You might also like