0% found this document useful (0 votes)
204 views

Ict Algorithm Development and Programming2

The document discusses algorithms, their development, and programming. It defines algorithms and describes their key characteristics and categories. The concepts of stepwise refinement, top-down design, flowcharts, and pseudocode are also explained as tools used in developing algorithms.

Uploaded by

henri foumane
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
204 views

Ict Algorithm Development and Programming2

The document discusses algorithms, their development, and programming. It defines algorithms and describes their key characteristics and categories. The concepts of stepwise refinement, top-down design, flowcharts, and pseudocode are also explained as tools used in developing algorithms.

Uploaded by

henri foumane
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

ALGORITHM DEVELOPMENT AND PROGRAMMING

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.

Characteristics of a good Algorithm


A good algorithm has five characteristics.
1. Input
A good algorithm must have an input. Without an input, an algorithm will be solving a
problem with nothing which is not possible.
2. Output
A good algorithm should produce some results based on its input. The output should be the
solution of the problem it was designed to solve.
3. Definiteness
The instructions should be precise and void of any ambiguities.
4. Effectiveness
The algorithm should be able to solve the problem for which it was designed without wasting
system resources such as processor time and memory. In addition, an algorithm should be
executable an efficient. It should handle all possible circumstances in solving the problem
5. Termination
The algorithm should eventually terminate so that output can collected and effectiveness
measured. If the algorithm is intentionally designed to run continually then it must have a
way to permit output to be collected
Stepwise Refinement and Top-down design

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.

Example: Boiling yams.

Below is an algorithm for a household robot to boil yams.

1. Put yam in pot


2. Add water
3. Boil yams
4. Wait for 55minutes
5. Serve yam

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.

Original Algorithm First refinement Second refinement

1 Put yam in pot 1.1 Get yams from bag

1.2 peel yams 1.2.1 Get knife from cupboard


1.2.2 Peel yams
1.2.3 Slice yams
1.3 Wash yams
1.4 Drain water
2.1 Fill cup with water 2.1.1 Put cup under tap
2 Add water in pot 2.1.2 Turn on tap
2.1.3 Wait until cup is full
2.1.4 Turn off tap
3.1 Cover lit of pot
3 Boil yams
3.2 Place pot on gas

3.3 Turn gas on

4.1 Turn off gas


4 Wait for 55 minutes
5.1 Drain water in pot
5 Serve yams
5.2 Put slices in dish

After stepwise refinement we can write our final algorithm as follows


Original algorithm After second refinement
1. Put yam in pot 1. Get yams from bag
2. Add water 2. Get knife from cupboard
3. Boil yams 3. Peel yams
4. Wait for 55 minutes 4. Slice yams
5. Serve yam 5. Wash yams
6. Drain water
7. Put cup under tap
8. Turn on tap
9. Wait until cup is full
10.Turn off tap
11.Cover lit of pot
12.Place pot on gas
13.Turn on gas
14.Wait for 55 minutes
15.Turn off gas
16.Drain water from pot
17. Place slices on a plate

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

Process A rectangle represents a single step or action in an algorithm

Input/Output A parallelogram represents the information entering or leaving the system.


system. Ex Example, student marks (input) and class average(output) of a test

Decision The diamond represents a decision or branching point. Example (if-else)

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

Example: Flow chart for dialling a telephone number.

Start

Dial number

Is line
engaged?

Telephone is answered

Stop

Flow chart fig1

Start

Sum =0
Get N

Sum = Sum + N
Count = Count +
1

Are they more N? Yes

No
Average = End
Sum/Count

Flow chart fig2: To find the average of a set of numbers

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

Pseudo code for dialling a telephone number (fig1 above)

1. Dial number
2. Determine if line is engaged or not
3. Answer call
4. Stop the call

Basic Algorithmic Constructs


Three types of constructs can be used to design a good algorithm. These are Sequence, Selection (or Choice),
iteration recursion.
a) Sequence Construct or Sequential Construct
This is a group of instructions that can only be executed in the order in which they are specified without
skipping any.
ACTIVITY 1 ACTIVITY 2 ACTIVITY 3

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) Iteration and Recursion


Iteration: These constructs are to perform repetition of some instructions. Examples include: Repeat until, Do
until, Do While, For, and While statements. This is a program structures that allow the block of
statements to be performed again and again during program execution. This repeated execution of
the same instructions is often called looping.

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.

Bounded loops refer to those whose number of iterations is known before-hand.

A trip around the loop is known as iteration.

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)

We express the above examples iteratively

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.

When writing recursive function we make clear the following criteria

 Establish a base case which can be solved. (recursion ends here)


 Write down the general case which should make the problem smaller and approach the base
case by looping.

Examples of recursive functions

1. The factorial function:

Function definition:

Function factorial(n)

Get the positive integer n

If n = 0 then the answer is 1

else evaluate n * factorial(n-1);

WHAT IS DATA STRUCTURE?


A data structure is about storing and manipulating data. The method we choose to store our data depends on:

 the size of the data and


 the ease with which the data can be manipulated.
Data exist in different format. For this reason they are stored and manipulated differently. Data has to be store in a
form that is easily accessible and manipulated. This different method of storing data for easy manipulation is what
is known as data structure.

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.

TYPES OF DATA STRUCTURES


1.1.Primitive Types
This is the simplest data structure. They hold single entries such as one number, one date or a single character
or string. The table below shows the basic primitive types.

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.

Integer Integer values in the range 2,147,483,648 to 2,147,483,647.

Numeric Long Very large integer values.


These are decimal point numbers. Used where precision is not
Float
Floating necessary.
point These are decimal numbers. Used where precision if highly
Double
needed.
Integer and floating-point numbers scaled by a factor in the range
Decimal Decimal
from 0 to 28.
Boolean Boolean Boolean store True or False values
Character Char Stores a single character
String
String String Stores text
Date Date Date Stores date

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

Array 0 1 2 3 4 5 Array index


14 12 15 16 3 8

Element in array position 2

Figure 2.1: Array structure (an array of 6 elements)

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

Figure 2.2: Linked-List structure

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.

BASIC OF PROGRAMMING LANGUAGES

A programming language is an artificial language designed to communicate instructions to a


machine, particularly a computer. Programming languages can be used to create programs that
control the behavior of a machine and/or to express algorithms precisely.

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.

Different types of languages


Programmers write instructions in various programming languages, some directly understandable
by computers and others requiring intermediate translation steps. Hundreds of computer
languages are in use today. These may be divided into three general types:

Classification of programming Languages


Low Level Languages (LLL)
a) Machine Language
This is a programming language in which the instructions are in a form that allows the computer to
perform them immediately, without any further translation being required.

 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

 These are English-like abbreviations (symbols) to represent elementary operations. These


abbreviations form the basis of assembly languages. Translator programs called
assemblers were developed to convert early assembly-language programs to machine
language at computer speeds.

Machine Language Assembly Language


100101 ADD
011001 SUB
001101 MPY
100111 CMP

c) High level languages


These are programming languages that enable programmers to write instructions in a language
closer to human language without requiring detailed knowledge of the hardware or type of
computer used. They are called high level because they are closer to human languages and
further from machine languages. The compiler provides this interface transparently for the
programmer

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

Programming Paradigm or LANGUAGE TOOLS


A programming paradigm is a fundamental style of computer programming. (Compare with a
methodology, which is a style of solving specific software engineering problems.) Paradigms differ
in the concepts and abstractions used to represent the elements of a program (such as objects,
functions, variables, constraints, etc.) and the steps that compose a computation (assignment,
evaluation, continuations, data flows, etc.).

Source Program or Source code - A program written in a human readable version.


Object Program or Object code - The machine language version of a source program as
compiled by a compiler.

 Different language tools

Translators: A translator is a program that converts statements written in one language to


statements in another language e.g converting assembly language to machine code. the
assembly-language program would be called the source program and the machine-code would be
called the object program.

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

Program paradigms and their differences

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.

Procedural (imperative) programming


The imperative paradigm, also known as the procedural paradigm, represents the traditional approach to the
programming process. As the name suggests, the imperative paradigm defines the programming process to
be the development of a sequence of commands that, when followed, manipulate data to produce the desired
result. Thus the imperative paradigm tells us to approach the programming process by finding an algorithm
to solve the problem at hand and then expressing that algorithm as a sequence of commands.

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.

Logic programming is sometimes also called declarative programming in contrast to imperative


programming.
In an imperative programming language, the programmer states exactly the method to be applied to
achieve the desired result; in a logic language the programmer states what is the result that he or she wants
to achieve, and it is up to the language as to how it achieves it.
Declarative programming is a way of specifying what a program should do, rather than specifying
how to do it. Most computer languages are based on the steps needed to solve a problem, but some
languages only indicate the essential characteristics of the problem and leave it to the computer to determine
the best way to solve the problem.
The former languages are said to support imperative programming whereas the latter support
declarative programming. As the name implies, logic programming depends on a form of logic – in this case
that which is known as predicate calculus. In this calculus the programmer can formulate propositions,
where a proposition is a logical statement which may or may not be true. An important application area is in
expert systems. For example, rules might capture the knowledge ‘If the patient has a temperature and a
runny nose, then they have a cold.’ The most widely used logic language is Prolog.
Declarative programming often considers programs as theories of a formal logic, and computations
as deductions in that logic space. Declarative programming has become of particular interest recently, as it
may greatly simplify writing parallel programs.

Object-oriented design

This is a problem-solving methodology that produces a solution to a problem in terms of self-contained


entities called objects, which are composed of both data and operations that manipulate the data. Object-
oriented design focuses on the objects and their interactions within a problem. Underlying object-oriented
design (OOD) are the concepts of classes and objects.

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.

You might also like