Programming chapter 1
Programming chapter 1
Computer Programming involves writing instructions and giving them to the computer to
complete a task. A computer program or software is a set of instructions written in a computer
language in order to be executed by a computer to perform a useful task.
Ex: Application software packages, such as word processors, spreadsheets and databases are
all computer programs
Computer Programmer is a person who translates the task you want a computer to do into a
form that a computer can understand.
Programming language refers to a series of specifically defined commands designed by human
programmers to give directions to digital computers. It is a means of communication between a
human being (programmer) and a computer. A programmer uses this means of communication in
order to give the computer instructions. A programming language is an artificial language that
can be used to control the behavior of a machine, particularly a computer. Programming
languages, like human languages, are defined through the use of syntactic and semantic rules, to
determine structure and meaning respectively. Programming languages differ from natural
languages in that natural languages are only used for interaction between people, while
programming languages also allow humans to communicate instructions to machines.
All programming language instructions must be expressed in binary code before the computer
can perform them.
Page 1 of 9
1. Machine language: - is the lowest level programming language in which all instructions
and data are written in a binary machine code, i.e. 0s and 1s.
Characteristics:
It is fast
It is not easy to learn
It is machine dependent
2. Assembly language: - is a low-level programming language that uses abbreviations
rather than binary code. It is English-like abbreviations representing elementary computer
operations (ADD, MUL…).
Characteristics:
It is faster to write instruction than machine language
It is slower than machine language, because it needs language translator program
(assembler).
It is not easy to remember (learn).
3. High level programming language: - allow programmer to write a program in a familiar
notation (English like statement). It is also called procedural language, i.e. programmer set
the precise procedures or sets of instruction. Example: FORTRAN, Basic, C++, Pascal, etc.
Characteristics:
It is not machine dependent
It is easy to understand
It is slow
1.3. Program translation
All programs must be translated before their instructions can be executed. Computer translation
languages can be grouped according to which translation process is used to convert the
instructions into binary code:
Assemblers
Interpreters
Compilers
Assembler: a program used to translate Assembly language programs into machine language. It
produces one line of binary code per original program statement. The entire program is
assembled before the program is sent to the computer for execution.
Program in
Program in
Assembly Assembler Machine
language language
Page 2 of 9
Interpreter: A program used to translate high-level programs into machine understandable
form. It translates one line of the program into binary code at a time.
Compiler: a program used to translate high-level programs. It translates the entire program into
binary code before anything is sent to the CPU for execution.
Program in
High level Program in
language Compiler Machine
language
The table below (table 1.1) shows the detail of activities performed in each of the above
mentioned steps.
Page 3 of 9
Step Jobs to be done Product
Page 4 of 9
It is a list of instructions specifying a precise description of a step by step process
that terminates after a finite number of steps for solving a problem, producing the
correct answer in the end.
It is a recipe for solving problems.
A finite set of an instruction that specifies a sequence of operation to be carried out
in order to solve a specific problem.
An unambiguous procedure specifying a finite number of steps to be taken.
It is precise and independent of specific programming languages.
Is a procedure or formula for solving a problem.
Methods of Specifying Algorithm
Finiteness: - there is an exact number of steps to be taken and has an end.
Correctness: - it must be correct and must solve the problem for which it is
designed.
Absence of Ambiguity: - means that every instruction is precisely described and
clearly specified. Every step in an algorithm must be clear as to what it is supposed
to do and how many times it is expected to be executed.
Generality: - i.e. it should have to be general.
Sequence of Execution - instructions are performed from top to bottom.
Input and Output - defined the unknowns of the problem is specified and with the
expected outcome.
Effectiveness - the solution prescribed is guaranteed to give a correct answer and
that the specified process is faithfully carried out.
Scope Definition - applies to a specific problem or class of problem.
Algorithms are usually represented in pseudo code or by a flow chart.
Pseudo code: is English like language for representing the solution to a problem. Pseudo code
is independent of any programming language. Pseudo code (or a flow chart) is the first step in
the process of planning the solution to a problem (also called developing an algorithm).
Flow chart: is a graphical way of representing the solution to a problem. The symbols used in
flow charts are shown in table 1.2 below.
Page 5 of 9
translate an algorithm to more than one programming language. Flowchart uses different symbols
(geometrical shapes) to represent different processes. The following table shows some of the common
symbols.
The purpose of using pseudocode is that it may be easier for humans to read than conventional
programming languages, and that it may be a compact and environment-independent generic
description of the key principles of an algorithm. Writing pseudocode will save you time later
during the construction & testing phase of a program's development.
When you are asked to add numbers, what you know for sure is that addition operation involves
two operands at a given time. The first step is to have the two-operand values. Then we add these
Page 6 of 9
operands and store the result to another operand. This is just a description. To check if this works
lets assume the following.
Input; - x, y
Process: Compute the arithmetic sum of the two numbers, put result on variable
Output: display the sum of two numbers
Start 1. Begin
2. Read X, Y
Read X,Y
3. Calculate Sum
3.1. Formula:
Sum= X+Y
Sum=X+Y
5. End
Stop
Page 7 of 9
Flowchart Pseudo code
Start
1. Begin
Input a,b, c
2. Input a, b, c
3. Calculate Sum
Sum= a+b+c 3.1. Formula:
Sum=a+b+c
Average= Sum/3
4. Calculate Average
Average=Sum/3
Stop 5. Display Average
6. End
Start 1. Begin
2. Read a,b
Read a,b
3. If a>b
No Print a
Is
a>b 4. Else
Yes
Print b
Print a Print b
5. End
Stop
Page 8 of 9
Example 4: sum of the first 10 natural numbers
Yes 7. End
Is
N<10 Exercise
Page 9 of 9