Chap - 1
Chap - 1
Computer Science & IT Department
Comp1032 – Introduction to Computers and Programming
CHAPTER- 4: Problem solving using computers
I. Analysis
Analysis stage requires a thorough understanding of the problem at hand and
analysis of the data and procedures needed to achieve the desired result. In
analysis stage, therefore, what we must do is workout what must be done rather
than how to do it.
An algorithm can be expressed in many ways. Here, we only consider two such
methods: Narrative (pseudocode) and Flowchart. English is often used to
describe or narrate the algorithm. A flowchart is a diagram consisting of labeled
symbols, together with arrows connecting one symbol to another. It is a means
of showing the sequence of steps of an algorithm. A program flowchart shows
the operations and logical decisions of a computer program. For one problem
there may be a lot of algorithms that help to solve the problem, but the
algorithm that we select must be powerful, easy to maintain, and efficient (it
doesn’t take too much space and time)
An algorithm consists of a set of explicit and unambiguous finite steps which, when
carried out for a given set of initial conditions, produce the corresponding output and
terminate in a fixed amount of time. By unamabiguity it is meant that each step should be
defined precisely i.e., it should have only one meaning. This definition is further
classified with some more features.
Step 1: start
Step 2: Read three numbers n1, n2 and n3.
Step 3: If n1 >n2 and n1 > n3 then
Big n1
Else
If n2 > n1 and n2 > n3 then
Big n2
else
Big n3
Step 4: Print Big
Step 5: Stop.
Example 4) Algorithm to find sum of N positive integer numbers.
Step 1: start
Step 2: Read N
Step 3: Sum 0,
Step 4: Count 0
Step 5: Read Num
Step 6: SumSum + Num
Step 7: count count +1
Step 8: If Count < N then goto step 5
Step 9: Print Sum
Step 10: Stop
Example 5) Algorithm to find factorial of a given Number
(N! = 1*2*3*…*N)
Step 1 : Read N
Step 2 : Fact=1
Step 3 : Count = 1
Step 4 : Fact = Fact * Count
Step 5 : Count = Count +1
Step 6 : If Count < = N then Goto step 4
Step 7 : Print Fact
Step 8 : Stop
Flowchart
A flowchart consists of an ordered set of standard symbols (mostly, geometrical shapes)
which represent operations, data flow or equipment. A program flowchart shows the
operations and logical decisions of a computer program.
The standard flowchart symbols and their meaning are given below.
START Start
C ← A+B T F
Is
A>B ?
Display C
Display A Display B
is Largest is Largest
Stop
Stop
Start
Read n
big 0
Count0
Read num
Is
num > big T
?
big num
F
Count count+ 1
Is
T Count <n
?
Display big
Chapter 4 – problem solving using computers
Page 5 of 8
Stop
III. Coding
The flowchart is independent of programming language. Now at this stage we
translate each steps described in the flowchart (algorithm description) to an
equivalent instruction of target programming language, that means, for example
if we want to write in C++ program language, each step will be described by an
equivalent C++ instruction (Statement).
IV. Implementation
Once the program is written, the next step is to implement it. Program
implementation involves three steps, namely, debugging (the process of
removing errors), testing (a check of correctness), and documenting the program
(to aid the maintenance of a program during its life time).
V. Maintenance
There are many reasons why programs must be continually modified and
maintained, like changing conditions, new user needs, previously undiscovered
bugs (errors). Maintenance may involve all steps from requirements analysis to
testing.
Machine language: - There is the only language that the computer understands directly..
A machine language is a set of machine instructions which consists of zero’s and one’s.
A machine instruction contains two parts an operation code ( op code) and an address.
The OP code tells the microprocessor system what operation it should perform, add,
transfer, compare, or move data to output device, etc. The address identifies the location
(memory, register) holding the required operands that is, the data to be operated upon.
The address part may contain one, two or more addresses that is , there may be one ( or
single address, two( double) address, and three ( or triple) address instructions.
Assembly Language: - In machine language we have seen that the OP code and the
address are represented as a binary sequence but it is difficult for the programmer to write
a big program using binary sequence and it is difficult to debug an error from such
program so instead of representing the OP code and the adders as a binary sequence we
can represent them in mnemonics (symbols). An Assembly language is a programming
language which uses mnemonics to write a program. It is machine dependent.
There are also other languages which are still simplest and easier than high-level
languages which we call them fourth generation languages. These languages are
application oriented languages.
Ex: - Visual basic,
Translation and Execution
The only language that the computer understands is the machine language. Therefore any
program that is written in either low-level or high level language must be translated to
machine code so that the computer could process it.
A program written in high-level or Assembly is called source code or source program
and, the translated machine code is called the object code or object program. Programs
that translate a program written in high level language and Assembly to machine code
program are called translators. There are three types of translators; assembler, interpreter,
and compiler.
The translator not only translates the instructions into machine code but also it detects
whether the program fulfills the syntax of the programming language. A program passes
through different stages before it carries out its function. First the program will be
translate to object code (compilation time), then it will be loaded to the memory and
finally it will be executed (run time) or carries out its function.