Introduction To Programming and Algorithms
Introduction To Programming and Algorithms
1. INTRODUCTION TO PROGRAMMING
A language used by human beings to communicate is called a natural
language. English is an example of a natural language. A programming
language is used to communicate a set of instructions to a computer.
Every natural language has a set of symbols, which are used to form
sentences. Similarly, every programming language has a set of
characters, which are used to construct programs. This set of symbols
is known as character set of that language. There are two aspects to
any language: syntax and semantics. The syntax of a language is the
set of rules for constructing correct sentences in the language.
Semantics, on the other hand, associates a meaning according to
certain rules, like those used in a natural language. The different is
that in programming languages, the rules are strict and precise. The
following sentences in English are perfectly clear to the reader.
The word lead is used in both sentences and has different meanings but
there is no confusion because the meaning can be interpreted from the
context. A programming language however cannot allow such usage. There
should be no scope for ambiguity because programs have to be
interpreted correctly by the computer.
Programming concepts
A computer works by executing a set of instructions known as programs.
1
syntax of a language is the set of rules for constructing correct
sentences in the language.
2. PROGRAM DEVELOPMENT
1. problem recognition
2. problem definition
3. problem design
4. program coding
5. problem testing and debugging
6. documentation
7. program implementation and maintenance
Problem
Recognition Documentation
Problem
definition
Program
design
Program
coding
The solid arrows shows the next step in
the order of program development. Program
testing
1. Problem recognition
Program recognition refers to the understanding and interpretation of
a particular problem. In order to understand a problem you need to
look for particular key words such as compute, evaluate, compare etc.
You can rewrite the problem in a more simplified way using the
keywords.
A programmer identifies problems in the environment and seeks to solve
them by writing a computer that would provide the solution. Many of
the privileges of automation that we enjoy today are as a result of
people who wrote computer programs to do the tasks. For example, the
intelligent control of traffic lights, the autopilot in aircraft and
the use of robots in industry are good examples where problems were
recognized and the computer was used as a tool to solve them. Think of
the person who after seeing the tedious nature of typing using a
manual typewriter decided to develop a word processor program!
3
At the end of the stage, the boundaries of the expected program will
have been established. I.e. clear view of what the program needs to
comply must be in place. In case, several methods are identified hat
can be used to solve the same problem, them the best alternative
should be chosen.
3. Program design
Program design is the actual development of the program’s processing
or problem solving logic called the algorithm. An algorithm refers to
a limited number of logical steps that a program follows in order to
solve a problem. It is the programmer who will usually come up with
the algorithm after carefully analyzing the requirements
specification. Many programs are not made up of one large block of
code i.e. they are mot monolithic. Instead, several units called
modules work together to form the whole as shown below
Modules
Program
Program boundary
Program
boundary
4. Program coding
Program coding is the actual process of converting a design model into
its equivalent program. This is done by recreating the program using a
particular programming language. The end result of this stage is a
source program that can be translated into a machine readable form for
the computer to excite and solve the target problem. Programs can be
written in many different programming languages e.g. C++, Pascal, and
Visual Basic etc.
4
5. Program testing and debugging
Testing is part of the procedures that ensures the program corresponds
with the original specifications, and that it works in the intended
environment.
It is the process of running software to find errors (or bugs), though
it is possible that that some errors may get through the testing
process without being found. The process of finding errors is called
debugging.
a. Syntax errors
These errors emanate from improper use of language rules. E.g. grammar
mistake, punctuation, improper naming of variables and misspelling of
the defined and reserved words. An error in language usage is called
syntax error.
Reserved words are those have a special meaning to the programming
language and should not be used by the programmer for anything else.
These errors are detectable by the translator and must be corrected
before the program runs.
Program testing
There are several stages in testing a program
- desk checking
- Translator system checking.
- Program run with test data
- Diagnostic procedures.
- Full –scale (system) test with actual data
5
1) Desk checking (or dry run)
After writing the program, the programmer goes through the program on
paper to pick up any logic errors that would otherwise cause extra
work at a later stage.
This process is sometimes called a dry run
4) Diagnostic procedures
For complex programs, diagnostic procedures, such as trace routines,
may be used to find logical errors. A trace print out the results at
each processing step to enable errors to be corrected quickly. If a
trace routine is not available, the programmer can insert instructions
in a program to print out intermediate result at key points.
TEST DATA
Is data read in a program to verify accuracy of program processing
basing on original design specifications.
Normal data
This includes the most general data for which the program was
designed.
Extreme data
These test the behaviour of the program when valid data at the upper
or lower limits of acceptability are used. For example for numerical
data, very large or small values could be used.
Exceptional data
Programs are usually designed to accept a certain range or class of
inputs. If ‘illegal’ data is used, the program should be capable of
6
rejecting it rather than attempting to process it to avoid getting
wrong results.
NB
Real data- data intended to be used by the program i.e. data for which
the program is designed for.
Dummy data- data which is not real i.e. data not intended to be used
by the designed program.
7. Program documentation
The documentation is the writing of support materials explaining how
the program can be used by the users, installed by the operators or
modified by other programmers. All stages of program development
should be documented in order to help during future modification of
the program.
Documentation can be for user of the program (user manuals ) or for
the other programmer who might modify the program in
future(technical / program manuals).
NB: Both the machine and assembly languages are referred to as low-
level languages
While other are referred to as High Level languages.
7
1. Machine languages (First generation languages)
These were the 1st primary languages to be used. The program
instructions were written using binary digits (0’s and 1’s). The
computer, being driven by electricity, is a bi-state device i.e. the
internal circuitry is either in an “ON” or “OFF” state. When carrying
out a given task, the computer’s internal circuits will keep on
changing state from OFF to ON and vice versa.
When a program was written in machine language, it directly addresses
the internal circuit of the machine. It is directly addresses the
internal of the machine. It is so called because it is the language
that the “understand”.
The machine language had the advantage of fast program execution since
translation was not required, and also the ability of the programmer
to directly address the control internal circuits of the computer. On
the other hand, tracing of errors in a program written in machine
language was extremely difficult. Writing the program itself was time
consuming and cumbersome and the programmer had to be an expert on the
internal workings of the computer. Also program written in machine
code are not portable i.e. machine.
Also program written in machine code are not portable i.e. are machine
dependent because CPU’s have different machine codes e.g. those for
Intel Pentium processors may differ from Motorola or Cyrix processors.
Therefore before decoding the meaning, a programmer has to find out
which CPU the program was written.
Also given that data and instructions are in binary form, many lines
of code are needed to accomplish even a simple task like adding two
numbers. A program written in machine might look like this;
Key:
MOV-move;
SUB-subtract;
AX- data register
The first line in the code means over the value 15 into the processor
register with the name AX. The second line tells the computer to
subtract 10 from the value in register AX.
9
High-level languages
High –level languages are very close to human language (English -like)
and they can be read ad understood even by people who are not experts
in programming. There are many types of high-level languages and each
of them was developed to address a particular problem-solving domain
while others came due to advancement in technology. These languages
are machine independent. This means that a programmer concentrates on
problem solving during a programming session rather than on problem
solving during a programming session rather than how a machine
operates.
Pascal
Pascal was initially developed as an academic language, to help in the
teaching and learning of structured programming.
FORTRAN (FORmula TRANslator);
This language was developed for mathematicians scientists and
engineers. It enables writing of programs with mathematical
expressions.
COBOL( Common Business Oriented Language);
This language is designed for developing programs that solve business
problems e.g. developing data processing applications such as
computer-based inventory control systems.
10
simplicity, it is a powerful tool for students who wish to learn
programming. It was first high-level language that was available that
was available for microcomputer users.
C
This is a programming language mainly used for developing system
software such as the operating system. It is one of the most popular
and powerful high-level languages in the business world because of its
ability to provide the programmer features of low-level languages and
at the same time easily understandable as a high level language.
Ada
This language was named after the first lay programmer, Ada Lovelace.
Ada is suitable for developing military, industrial and real-time
systems.
11
Disadvantages
2. Low level languages are difficult and cumbersome to sue and
learn.
3. They require highly trained experts both to develop and maintain
programs.
4. Removing errors (debugging) in low level language programs is
difficult.
5. Low level programs are machine dependent i.e. they are not
transferable from one hardware or software platform to another.
a. Unstructured Languages
Are languages that have one entry and one exit point.
The program flow is sequential during execution of instructions
They are also known as imperative languages. They evolved from early
machine code and they have benefits of enhancing programmer
productivity and portability by defining the machine independent
programming environment.
12
c. Object oriented programming languages
In object oriented programming, data is declared as part of the code
with object oriented programs, data and code, that is methods that
operate on your data, suddenly become one. Object is a combination of
code and data together that can be treated as a unit, i.e.
object = code + data.
Languages used in OOP, include C++, Visual Basic and Turbo Pascal for
windows.
3. Trained manpower
Available, trained manpower on the market that can use the available
language.
13
4. Portability of program
Programming for programs enables a system program developed on one
computer to run on another computer.
Visual Basic can only run on windows environment but not in Unix (it
is not portable)
2. maintainability
The extent in which a programming language features support source
code that can be modified to satisfy new requirements or to correct
errors.
4. expressivity
It is the capability of a programming language to express solution of
a problem in
(a)closest way to the original problem formulation.
(b) in a clear natural and concise way.
(c) in terms of other sub programs.
e.g. programs using routines / sub programs are more expressive than
languages without them.
5. Generality
14
A good programming language should support construction of instruction
codes which to a great extent solve general problems in addition to
task they were designed to solve
6. Code reuse
It is mostly supported by generality. e.g. in the use of sub routines.
Source program
The term source refers to the program code that the programmer entered
in the program editor window that is not translated into machine
readable form. The source program is usually created using a
particular programming language discussed later.
Object Code
The term object code refers to the program code that is in machine
readable. A source code that is not in machine readable form must be
translated into object code.
Translators
The term translator is used to refer to language processors such as
assemblers, interpreters and compilers that converts the source
program into object code.
Assembler
An assembler translates assembly language into machine language that
the computer can understand and execute.
Interpreter
An interpreter translates the source program line- by –line, allowing
the CPU to execute one line before translating the next. The
translated line is not stored in the computer memory. It means that
every time the program is needed for execution, it has to be
translated. This method of translating programs was very common in
early computers that did not have enough memory to store the object
code as a file that can be executed later.
Compiler
A compiler translates the entire source program into object code. The
object code file can be made into a fully executable program by
carrying out process known as linking which joins the object code to
all the other files that are needed for the execution of the program.
After the linking process, an executable file (application file) is
generated. This file is stored on a storage media such as disk with a
name that has a unique extension(.EXE) Examples of executable files
are Microsoft Word etc.
15
The difference between the interpreters and compilers are as below.
Interpreters Compilers
1. Translates the source 1. Translates the entire source
program one statement at code at once before
a time execution.
Characteristics of an algorithm
1. Algorithms are well ordered.
2. Algorithms are unambiguous operations.
3. Algorithm have effectively computable results
4. algorithms produce a result
5. Algorithms halts in a finite amount of time
16
Each operation of an algorithm must be something that computer can
able to do (a number should not be divided by zero since the answer
is infinite)
2. Introductory comments
Task performed by algorithm is described briefly. Any assumptions
made by the algorithm are mentioned here along with variables used and
their data types.
3. Steps
It is made up of a sequence of numbered steps each step by step s
preceeded by a brief comment describing its function. If a step is
composed of several statements they are executed in a left to right
manner.
4. Comments
Can be included within the body of step by step enclosing them in
parenthesis.
Example
17
Write a pseudocode that can be used to prompt the user to enter two
numbers, calculate the sum and average of the two numbers and then
display the output on the screen.
START
Print “Enter two numbers”
INPUT x,y
SUM = x+y
AVERAGE = sum/ 2
PRINT SUM
PRINT AVERAGE
STOP
Example
STEP 3: [initialize]
Sum = 0
Count = 0
STEP 6: [finished]
18
Exit
FLOW CHARTS
A flow chart is a pictorial representation of an algorithm. It shows
the logic of the algorithm and flow of control. The flow chart uses to
represent specific actions and arrows to indicate the flow of control.
The flowcharts are language independent and constitute a very general
way of representing an algorithm.
Processing symbol
Direction symbol
Connector
19
Decision symbol
20
Start
Read n
n > 0
Sum = 0
Count = 0
Count<0
Read
number
Average = sum +1
Write average
stop
21
2. Pseudocode
A pseudocode is a structured expression of an algorithm. It consists
of English like statement describing an algorithm. Indentation is used
to increase clarity.
Example
A pseudocode to computer average of n numbers
Begin
Read n
If (n>0)
Sum =0
Count =0
If (count < n) then
Read number
Sum = sum + number
Count = count + 1
End if
End if
Average = sum /n
Write average
End.
Subprogram
Subprogram
Subprogram
Subprogram
main program
The bottom –up tends to run into interface problems especially when
you have to put together subprograms or systems modules written by
different teams of programmers.
Example
23
Computer pay
Program
24
In this method, programs are designed around objects. Data is declared
as part of the code. The code then performs operations on data. In
this method data and code suddenly become one. It is data driven and
it is based on data types and their interactions.
- readability
- reliability
- portability
- maintainability
- performance efficiency
- ease of use
*******
PROGRAMMING STYLES
Programming style refers to a set of rules or guidelines used when
writing the source code for a computer program.
Following a particular programming style will help programmers to
quickly read and understand the source code as well as helping to
avoid making errors during program development or modification.
2. choice of identifiers
The identifiers chosen should be easy to read , understand and relate
to the of the program. For example using identifier Length to
represent longer side of a rectangle, UnitPrice to represent price per
unit of a commodity.
3. Indenting
This involves arranging program segments in a manner they can be
easily identified and their function interpreted. For example
subprogram with control structures should be written in a manner that
each loop is clearly identified.
e.g. if (x>0) then
if (x<100)
write(x) ;
4. use of subprogram
25
Subprograms are independent program segment within a program which
perform a specific task. Their use makes the program clear i.e. easy
to read and understand, easy to modify and debug etc.
5. Comments
They help to understand the program by the user, in various stages of
program flow by explaining /highlighting what a program does.
C programming
26