2. INTRODUCTION TO ALGORITHMS
• At the end of this Chapter, the student will student will be able to:
• Define an algorithm;
• Give examples of algorithms from daily life processes;
• Demonstrate an understanding of how algorithms are constructed;
• Define a program; and
• Identify Relationship between Algorithms and Programs.
• Demonstrate an understanding of Pseudo-code
3. DEFINITION OF AN ALGORITHM
• What is an algorithm?
• An algorithm can be defined as a sequence of instructions or a set of rules that are
followed to complete a task.
NB: This task can be anything, so long as you can give clear instructions for it.
In other words, an algorithm can also be defined as a step by step solution to a problem.
4. CHARACTERISTICS:
1. An algorithm should be finite
2. An algorithm should be unambiguous
3. An algorithm must always include all the finer details about a problem that should be solved. (Do
not omit any despite the fact that it may sound trivial)
4. Every algorithm can be transformed into an executable program
5. An algorithm may be written by one person but may in future be used by any other person whom
might not necessarily know how it was derived
6. When an algorithm is executed, it should always give a correct solution to a problem irrespective of
the changing data set as long as the data set belong to a specified data type that from which the
input data should be taken
7. An algorithm is often presented using either pseudo-code; flow charts or Nassi Schneiderman
Diagrams
5. WHY LEARN ALGORITHMS?
• Data Structures and Algorithms course is designed to prepare you for the
programming course. It is designed to give an understanding of how to make
a computer do anything.To make a computer do anything, you have to write a
computer program. To write a computer program, you have to tell the
computer, step by step, exactly what you want it to do. The computer then
"executes" the program, following each step mechanically, to accomplish the
end goal.
6. Figure .3 c) Executable file, which results
from the process of compiling
Algorithm to Source Code
7. DEFINE
• Source code –a text listing of commands to be compiled or assembled into an
executable computer program.
• A compiler is a computer program (or a set of programs) that transforms source
code written in a programming language (the source language) into another
computer language (the target language), with the latter often having a binary
form known as object code.
• A program is a set of instructions derived from a specific algorithm that is written in
a specific programming language.
8. RELATIONSHIP BETWEEN ALGORITHMS
AND PROGRAMS
• A program is an algorithm that is translated into a set of instructions that the CPU
can understand. A program is executed by a computer in order to give a solution to
a problem for which it was designed to solve. Algorithms are only turned into
programs once they are validated.
9. PSEUDO-CODE
• Pseudo-code (or Pseudo code) is an informal high-level description of the operating
principle of a computer program or other algorithm.
• The word Pseudo means false.
• Pseudo code is a standard way of representing algorithms.
11. . DATA TYPES AND DATA STRUCTURES
• A data type is a set of values that have common characteristics (they belong to the
same domain).
• Data structure is a particular way of organizing data in a computer so that it can
be used efficiently.
12. . DATA TYPES AND DATA STRUCTURES
Examples of data types:
1. Integers: Set of negative and positive whole numbers
2. Real: A set of all possible numbers including decimal/fractional numbers, also
referred to as floating point numbers.
3. Characters: A set of all possible symbols, including letters of the alphabet. All
symbols found on a computer keyboard including numbers and special signs are
characters.
4. String: Is a group of characters (2 or more) that are used to represent one value
5. Boolean: A set of two values that represent either a true or false situation.
13. .USER DEFINED DATA STRUCTURES:
can represent more than one value at a time.
Examples of user defined data types:
1. Array: is a data type that is derived from a specific simple data type (same
domain) but can store more than one value at time.
2. File/Record/Structure: A user defined composite data type that groups together
different data values from one or more domains into a single data type.
3. Union: A data type that can only keep one value from a set of specified possible
data types.
14. . VARIABLES, CONSTANTS AND LITERALS :
Variables and Constants
1. Variable Is a name given to a specific memory location that is reserved to
store/keep a value from a specific data type.
NB: The values kept in a variable may change during program execution.
1. Constant Is a name given to a specific memory location that is reserved to
store/keep a value that cannot change during program execution.
15. . DECLARING A VARIABLE
Is the process of reserving and naming the memory space that will be used to keep
values from a specific data type during program execution.
How are variables declared
Data_Type Variable_Name
For example, a variable X that keeps an integer value will be declared as
Integer X
16. . DECLARING A CONSTANTS
How to Declare Constants
1. Constant_name Value
E.g. Rate 2.5 1.
Typed constants are declared as follows:
Data_type Constant_name value
E.g.String institutionName “NSEA”
17. LITERALS
Literals : is A value written exactly as it's meant to be interpreted. In contrast, a
variable is a name that can represent different values during the execution of the
program. And a constant is a name that represents the same value throughout a
program.
But a literal is not a name -- it is the value itself.
For example, in an expression:
Integer x = 20
The value 20 is a literal, while x is a variable
18. NAMING CONVENTION FOR VARIABLES AND CONSTANTS
1. Variable or constant name should only be one word.
2. If two or more words are to be used as a variable or constant name, they should be
joined with an underscore e.g. Integer student_age
3. No variable or constant name should start with a number but with a letter from the
alphabet.
4. No two different variables have the same name (same spelling)
5. Some special symbols, e.g. +, -, ‘,/, *, should not be used in a variable name
6. Variable and constant names may consist of one or more letters of the alphabet
7. A number may only be used as part of a variable name only if it written after a letter(s)
of the alphabet.
19. PROBLEM SOLVING AND BASIC COMPUTER
OPERATIONS
• Seven Basic Steps that should be followed when solving a problem
namely:
Understand/Identify the problem
Outline aSolution
Develop the ouline into an algorithm
Test/Validate your algorithm
Transform your algorithm into a program using a specific programming language
Run/Execute your program
Install, document and maintain the program
20. PROBLEM SOLVING AND BASIC COMPUTER
OPERATIONS
• Seven Basic Steps that should be followed when solving a problem
At this stage the problem can be divided into three separate components
namely: Three basic stages that are followed by a computer when solving a
problem: 1. Input 2. Processes 3. Output
the inputs
the outputs, and the
processingsteps to produce the required outputs
1. UNDERSTAND/IDENTIFY THE
PROBLEM
21. STEPS IN ALGORITHM DEVELOPMENT CONTINUE...
• The solution outline developed in step 2 is then expanded into an
algorithm: a set of precise steps that describes exactly the tasks to be
performed and the order in which they are to be carried out. We will be
using pseudo code (a form of structured English) to represent the solution
algorithm. Flowcharts and Nassi-Schneidermann diagrams will also be
introduced as graphical methods of representation
3. DEVELOP THE OULINE INTO AN
ALGORITHM
22. STEPS IN ALGORITHM DEVELOPMENT
CONTINUE….
• One of the most important steps in the development of a program, often
referred to as desk checking. The main purpose of this step is to identify
major errors early, so that they may easily be corrected. It involves
walking with test data through the logic of the algorithm exactly as a
computer would, keeping track of major variables on a sheet of paper.
4. TEST/VALIDATE YOUR ALGORITHM
23. STEPS IN ALGORITHM DEVELOPMENT
CONTINUE….
• Only after all design considerations have been met in the previous four
steps, and after you tested whether your algorithm is really working you
should actually start coding the program in your chosen programming
language.
5. TRANSFORM YOUR ALGORITHM INTO A PROGRAM
USING A SPECIFIC PROGRAMMING LANGUAGE
24. STEPS IN ALGORITHM DEVELOPMENT
CONTINUE….
• This step uses a program compiler / interpreter and programmer-
designed test data to machine test the code for syntax and
syntax/compilation errors (those detected at compile time) and run-
time/logical errors (those detected at run time). At this stage there are
hopefully no logical errors anymore, because they ideally should have
been eradicated in step 4.
6. RUN/EXECUTE YOUR PROGRAM
25. STEPS IN ALGORITHM DEVELOPMENT
CONTINUE….
• Documentation involves both external documentation (such as hierarchy charts,
the solution algorithm, and the test data results), and internal documentation that
may have been coded in the program. Program maintenance refers to changes
which may need to be made to the program throughout its life cycle.
7. INSTALL, DOCUMENT AND MAINTAIN THE
PROGRAM