Ict Algorithm Development and Programming2
Ict Algorithm Development and Programming2
Introduction
In general, an 'algorithm' is the name given to a defined set of steps used to complete a task.
It is a set of instructions that clearly defines the steps and actions to be performed in order to
accomplish a specific task.
For instance you could define an algorithm to make a cup of tea. You start by filling the kettle, then
place a teabag in the cup and so on.
In computer terms, an algorithm describes the set of steps needed to carry out a software task.
Algorithms are used to create programs which are a sequence of instructions written to instruct a
computer to carry out a specific task or to solve a specific problem.
Categories of Algorithm
Algorithmic operations are in an order, that is, first instruction, second instruction etc. However, an
algorithm must have the ability to alter the order of its instructions. An instruction that alters the
order of an algorithm is called a control structure. There are three categories of algorithmic
operations
i) Sequential operations: These instructions are executed in order. That is, they have a
particular order in which they are executed.
ii) Conditional or choice operations: It is a control structure that asks a true/false, yes/no
question and then selects the next instruction based on the answer.
iii) Iterative operations(loops): A control structure that repeats the execution of a block of
instructions.
However not every problem or task has a good algorithmic solution. There are:
i) Unsolvable problems: Those that no algorithm can exist solve them
ii) “Hard”(intractable) problems: Algorithms take too long to solve these types of problems
iii) Problems with no known algorithmic solution.
Top-down design and stepwise refinement are two structured programming concepts commonly used when
developing algorithms.
Stepwise refinement means replacing existing steps/instructions with a new version that fills in more details.
When carrying out stepwise refinement, you break a complex problem down into a number of simpler steps,
each of which can be solved by an algorithm which is smaller and simpler than the one required to solve the
overall problem.
These steps are probably not detailed enough for the robot. We therefore refine each step into a sequence of
smaller steps. Other steps may also require further refinement. After a number of refinements, the robot is
able to execute every step.
Top-down design
The aim of top-down design is to divide a given problem into sub-problems. A sub-problem in turn, can be
further simplified and divided into smaller sub-problems and so on. The idea is that a sub-problem would be
easier to solve than the original problem. The division of the problem starts at the top level going down(Top-
down design)
B C
D E F G
Flow-chart
A flowchart is a diagram which shows the breakdown of an algorithm into all of the necessary steps
(subtasks or execution steps). Each symbol contains information about what must be done and the arrows
show the order in which the instruction must be executed.
START
An Oval shape which indicate the start and end of a task or procedure.
Terminator
END
Connector A circle is used to connect two flowcharts or algorithms, where they have a
Common label. They denote a process performed on data such as calculation,
Sorting.
Line arrow An arrow is used to indicate the order in which subtask are carried out
Start
Dial number
Is line
engaged?
Telephone is answered
Stop
Start
Sum =0
Get N
Sum = Sum + N
Count = Count +
1
No
Average = End
Sum/Count
Pseudo code
Pseudo code uses English like words to represent algorithms. It is actually a mixture of English and a programming-
like language. Pseudo codes are not executed in a computer, they help the programmer to write his programme in a
simple clear language before attempting to write it using a programming language such as C, C++, PASCAL etc
1. Dial number
2. Determine if line is engaged or not
3. Answer call
4. Stop the call
Examples
Begin
Writeln(“ This is Cameroon”)
Writeln(“ I love very much”)
Writeln(“ What about you?”)
End.
b) Selection Construct or Conditional construct or Choice construct
This is a group of instructions designed in such a way as to permit the computer to make choice. The
choice is based on a condition. The computer is generally given two choices to make. One for a true, and
the other for a false condition. Examples include If-Then-Else, Case and GoTo statements.
If-Then-Else Statement
Activity 1
True False
Activity 2 Condition Activity 3
Begin
If x < 10 Then
Writeln(“ Passed”)
Else
Writeln(“Failed”)
End
A loop: is a sequence of statements which is specified once but which may be carried out several
times in succession. The code "inside" the loop is obeyed a specified number of times, or once for
each of a collection of items, or until some condition is met. In a flowchart a back arrow hints the
presence of a loop.
A loop is represented by the while, for, repeat constructs in most programming languages. A loop
can be bounded or unbounded.
Unbounded loops refer to those whose number of iterations depends on the eventuality that the
termination condition is satisfied.
You must ensure that the condition for the termination of the looping must be satisfied after some
finite number of iterations, otherwise it ends up as an infinite loop. They are used in the following:
ways.
While (Condition) do Repeat Repeat For (condition) do
Statement1 Statements Statements Statements
Statement 2 Until (Condition) While (Condition)
Recursion: In simple terms recursion is when a function calls itself. That is, in the course of the
function definition there is a call to that very same function.
Function definition:
Function factorial(n)
A data structure is a way to store and organize data in order to facilitate access and modifications. Note that when
talking of data structures, databases are excluded even though they form one way of storing data.
Data Data
Type Stores/Description
Format Types
The Byte type holds an integer in the range 0 to 255. Bytes are
Byte frequently used to access binary files, image and sound files, and
so on.
Integers Short Integer values in the range 32,768 to 32,767.
1.2.Linear Data Structure: This is data structure (container) that holds a sequence of elements arranged
linearly. Examples of linear data structures are:
1.2.1.Array: an array is a data structure consisting of a collection of data of the same type. E.g. an array of n
integers is a container holding n integers only. Each element of an array is identified by an index (a
positive integer). The position of an element in an array is determined by the index which is also used to
access the array element. An array is structured as shown in Figure 2.1 below
1.2.2 List: A type of a data structure where one item points to it successor. A good way to think of a linked
list is to imagine a chain, where one element is written on each link. Once we get hold of one link of the chain,
we can retrieve all elements. The size of a list can be fixed (static list) or of variable size (dynamic list). A
static list can be implemented using an array while a dynamic list can be implemented using a linked-list
structure.
Linked-List
It is similar to an array except that it allows efficient insertion and removal of elements in any position in the list. This
aspect makes it advantageous over arrays. A linked list is also dynamic, that is, resizable. Each item has two fields: a
value and a pointer (link) to the next item. A single linked list is structured as shown in figure 2.2
2 4 7 5 null
1.1.Abstract Data Type: This is a data type in which the data and the functions that operate on the data
are defined but their implementation is not defined. It can have several different implementations thus can
have different efficiency. Examples of abstract data type:
1.2. Stack: A stack is a data structure with two basic methods- push and pop. It is has the Last In First Out
(LIFO) structure. A common picture is that of a pile of plates. The first plate begins the pile. The next is
placed on top of the first and the next on top of that, and so on. A plate may be removed from the pile at any
time, but only from the top. The order of pushing plates onto the pile or popping them from the top is
arbitrary. There will always be a certain number of plates on the pile. Pushing a plate onto the pile increases
the number by one; popping a plate decreases it by one. But naturally you cannot pop a plate off an empty
pile doing so will lead to an error called the under flow error. Nor can you push a plate onto a full one--at
least if there is a maximum number of plates the pile can hold doing so will lead to an over flow error. So
these two conditions need to be monitored. A stack can be implemented using array or linked-list.
1.3. Queue: A queue is similar to a stack, except that you join the queue at one end and leave it at the other. In it
uses the First In First Out (FIFO) structure. It could be any data items awaiting processing, print requests,
instructions awaiting execution, etc. It can be implemented using array. A queue is like the line of customers
waiting to be served by a bank teller. As customers arrive, the joint the end of the queue while the teller
serves the customer at the head of the queue.
1.4. Deque: This is a special type of a queue where data can be added and removed from both ends.
Programming
A computer program is a sequence of instructions that is used to operate a computer to
produce a specific result.
Programming is the process of writing these instructions in a language that the computer
can respond to and that other programmers can understand.
The set of instructions that can be used to construct a program is called a programming
language.
On a fundamental level, all computer programs do the same thing (Figure1-1).
They direct a computer to accept data (input), to manipulate the data (process), and to
produce reports (output).
This implies that all computer programming languages must provide essentially the same
capabilities for performing these operations.
Machine language is the "natural language" of a computer and as such is defined by its
hardware design.
Machine languages generally consist of strings of numbers (ultimately reduced to 1s and
0s) that instruct computers to perform their most elementary operations one at a time.
Machine languages are machine dependent (i.e., a particular machine language can be
used on only one type of computer).
b) Assembly Languages
A low-level language is one in which instructions are written using mnemonic to be translated for
the mac
hine to be execute. A low-level language does not need a compiler or interpreter to run. They are
call low level because they are closer to machine language.
Such languages are cumbersome for humans and simply too slow and tedious for most
programmers.
Assembly languages
Examples: Pascal, BASIC, COBOL, C, C++, C#, Java, FORTRAN, LISP, Prolog, Ada etc
High-Level languages versus Low-Level languages
Low level High level
Does not need a Need a compiler
compiler
Difficult to understand Easier to read, write, maintain Or user friendly
Machine oriented Problem oriented
Machine dependent Portable across platform
Require less memory Require more memory because of the compiler
Interpreters convert each high level instruction into a series of machine instructions and then
immediately run (or execute) those instructions. In some cases, the interpreter has a library of
routines and looks up the correct routine from the library to handle each high level instruction.
Compilers convert a finished program (or section of a program) into object code. This is often
done in steps. Some compilers convert high level language instructions into assembly language
instructions and then an assembler is used to create the finished object code. Some compilers
convert high level language instructions into an intermediate language. This intermediate
language is platform-independent (it doesn’t matter which actual computer hardware is eventually
used). The intermediate language is then converted into object code for a specific kind of
computer. This approach makes it easier to move (or port) a compiler from one kind of computer
to another. Only the last step (or steps) need to be rewritten, while the main complier is reused.
Compiled code almost always runs faster than interpreted code. An optimizing compiler
examines a high level program and figures out ways to optimize the program so that it runs even
faster.
A compiler is a computer program that translates code written in a high level language to a
lower level language, e.g. assembly language or machine language. The most common reason for
translating source code is to create an executable program (converting from a high level language
into machine language).
Advantages of using a compiler
• Source code cannot be stolen/copied
• Tends to be faster than interpreting source code
• Produces an executable file, and therefore the program can be run without need of the source
code
Disadvantages of using a compiler
• Object code needs to be produced before a final executable file
• The source code must be 100% correct for the executable file to be produced
An assembler translates assembly language into machine language. Assembly language consists
of mnemonics for machine opcodes so assemblers perform a 1:1 translation from mnemonic to a
direct instruction. Conversely, one instruction in a high level language will translate to one or more
instructions at machine level.
Advantages of using an assembler
• Assembly code is often very efficient (and therefore fast) because it is a low level language
• It is fairly easy to understand due to the use of English-like mnemonics
Disadvantages of using an assembler
• Lots of assembly code is needed to do relatively simple tasks, and complex programs require
lots of programming time
• Assembly tends to be optimised for the hardware it's designed for, meaning it is often
incompatible with different hardware
Linkers
As programs grow in size, requiring teams of programmers, there is a need to break them up into
separate files so that different team members can work on their individual assignments without
interfering with the work of others. Each file is compiled separately and then combined later.
Linkers are programs that combine the various parts of a large program into a single object
program.
A loader is a program that loads programs into main memory so that they can be run. In the past,
a loader would have to be explicitly run as part of a job. In modern times the loader is hidden away
in the operating system and called automatically when needed.
Editors An editor (text editor) is a program that is used to edit (or create) the source files for
programming. Editors rarely have the advanced formatting and other features of a regular word
processor, but sometimes include special tools and features that are useful for programming.
Debugger: A software bug is an error, mistake or fault in a computer program. It is caused
mostly by errors made by people in the program code or its design. Sometimes a bad compiler will
produce errors in codes etc. Debugging is the process of removing bugs or faults or mistakes
from a program. A debugger is that program which performs the debugging process
Programming paradigms represent fundamentally different approaches to the programming process and
therefore affect the entire software development process. . The four paths are: the functional, object-
oriented, imperative, and declarative paradigms, with various languages associated with each paradigm.
A program can often be conceived simply as a list of instructions to be executed in order; that is, a procedure
to be followed by the computer. Procedural programming captures standard solutions to computational
problems in blocks of codes that can be accessed by name.
In procedural programming, the code for a specific job is contained in a named procedure. Another name for
a procedure is often subroutine. For instance, one might create a procedure to find the standard deviation of
an array of numbers.
Once you have a routine that calculates the standard deviation of an array of numbers, that routine can be
used again and again. Such reuse can be accomplished by including the routine in whatever new program
one writes or by adding the routine to a library where other programs can access the procedure by name.
Logic languages
Imperative programming language
It is a programming paradigm that describes computation in terms of statements. Imperative programs define
sequences of commands for the computer to perform i.e. define how things are done in terms of sequences of
actions to be taken. Examples of imperative programming languages are FORTRAN, BASIC and C. An imperative
programming language can be procedural or declarative.
a) Procedural programming
In this approach, emphasis is on procedures. A problem is divided into procedures and each procedure has a clearly
defined task. Examples of procedural is programming languages are Pascal, C, BASIC, and FORTRAN are procedural
programming languages
b) Declarative programming
It is a programming approach in which programs describe the desired results of the program, without explicitly
listing the steps that need to be carried out to achieve the results. In other words it describes a problem rather than
defining the solution. The focus to the programmer is what the program is doing and not how it is done. As such a
programmer can often tell, simply by looking at the names, arguments and return types of procedures including
comments, what a particular procedure is supposed to do without necessarily looking at the details.
Object-oriented design
Object Orientation
An object is an independent entity which can be treated in isolation of all other objects. Each object has an
identity which is distinct from all others. Given any pair of objects, it is always possible to determine
whether they are the same or different. An object class, or class for short is a description of a group of
objects with similar properties and behaviours.