0% found this document useful (0 votes)
12 views8 pages

Chap - 1

Uploaded by

matias bahiru
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views8 pages

Chap - 1

Uploaded by

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

Arba Minch University, Engineering Faculty


Computer Science & IT Department
Comp1032 – Introduction to Computers and Programming
CHAPTER- 4: Problem solving using computers

Computer problem solving is a complicated process requiring careful planning and


attention to detail. The vehicle for the computer solution to a problem is a set of explicit
and unambiguous instructions called a program, expressed in a programming language. A
program is a sequence of instructions that determine the operations to be carried out by
the machine in order to solve a particular problem. There are five stages of
software/program development. These stages are:
There are five stages of program development: namely, Analysis, Algorithm design &
flow chart, Coding, Implementation, and Maintenance.

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.

 What input data are needed to the problem?


 What procedures needed to achieve the result?
 What outputs data are expected?

II. Algorithm design and flowchart


Once the requirements of the program are defined, the next stage is to design an
algorithm to solve the problem. An algorithm is a finite set of steps which, if
followed accomplishes a particular task.

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)

DESIGN AND IMPLEMENTATION OF ALGORITHMS:


An algorithm is a finite set of instruction that specify a sequence of operations to
be carried out in order to solve a specific problem or class of problems. It is just a tool
for solving a problem. All the tasks that can be carried out by a computer can be stated as
algorithms. Once an algorithm is designed, it is coded in a programming language and
computer executes the program. Algorithms may be presented on several levels of detail.
It is difficult to use terms of machine code instructions or high level language because the

Chapter 4 – problem solving using computers


Page 1 of 8

details would obscure the essence of the procedure and due to difficulty to present long
programs in simple form. Therefore algorithm designers must start with a much more
concise representation

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.

An algorithm has five important features.

i) Finiteness: An algorithm terminates after a fixed number of steps.


ii) Definiteness: Each step of the algorithm is precisely defined, i.e., the actions to
be carried out should be specified unambiguously.
iii) Effectiveness: All the operations used in the algorithm are basic (division,
multiplication, comparison, etc.) and can be performed exactly in a
fixed duration of time.
iv) Input: An algorithm has certain precise inputs, i.e. quantities, which are
specified to it initially, before the execution of the algorithm
begins.
v) Output: An algorithm has one or more outputs, that is, the results of
operations which have a specified relation to the inputs.

Example: 1) Algorithm to add two numbers.


Step 1: start
Step 2: Read two numbers n1 and n2.
Step 3: sum  n1 + n2
Step 4: Print sum
Step 5: Stop

Example: 2) Algorithm to find largest number from two numbers.


Step 1: start
Step 2: Read two numbers n1 and n2.
Step 3: If n1 > n2 then
Big  n1
else
Big  n2
Step 4: Print Big
Step 5: Stop

Chapter 4 – problem solving using computers


Page 2 of 8


Example: 3) Algorithm to find largest number from three numbers.

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: SumSum + 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.

Terminal-used to represent the Start and End of


Program

Input/ output –used to represent data input or


data output from a computer

Chapter 4 – problem solving using computers


Page 3 of 8


Processing-usually encloses operations or


(command black) a group of operations( a process)

Decision block – it usually contains a question within it :-


there are typically two output paths: one if the answer
to the question is yes ( true) , and the other if the answer
is no ( false)

Flow line:- is used to indicate the direction of logical flow


( a path from one operation to another)

The advantage of flowchart is it doesn’t depend on any particular programming


language, so that it can used, to translate an algorithm to more than one programming
language.
1. Draw a flowchart to add two numbers. 2. A Flowchart to find largest of two
numbers.

START Start

Read A,B Read A,


B.

C ← A+B T F
Is
A>B ?

Display C

Display A Display B
is Largest is Largest

Stop

Stop

Chapter 4 – problem solving using computers


Page 4 of 8

Assignments:
Algorithms:
1. Write an algorithm to find the smallest number from three numbers.
2. Write an algorithm to find the sum of first N even numbers.
3. Write an algorithm to generate Fibonacci series. The first two digits in the series
are 1 and the rest is computed by adding the preceding two digits. (i.e. 1 1 2 3 5 8
13 . . .)
4. Write an algorithm to find the sum of digits of given number. If for example the
given number is 536, the sum of digits in the number = 5 + 3 + 6 =14
Flowcharts:
1. Draw a flowchart to swap two numbers without using third variable.
2. Draw a flowchart to find sum of N positive numbers.
3. Draw a flowchart to find the biggest among N numbers.
4. Draw a flowchart to find the Factorial of a given number.

Start

Read n

big 0
Count0

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


Flowchart to find largest of n


numbers

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.

Types of programming languages

We have seen that a computer system consists of different layers. It is possible to


communicate with the computer by writing a program at the different layers but basically
there are three types of programming languages: - Machine, Assembly and high- level.

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.

Chapter 4 – problem solving using computers


Page 6 of 8

High-level language: -We have seen that writing a program in low-level languages is
easier and simple compare to machine languages. But still Assembly language has its
own drawback, which is machine dependent. So we need another type of languages
which are not machine-dependent and more flexible. These languages are called high-
level languages.

Advantages of high-level languages:-


 Easier to learn and understand ( Look like English)
 Require less time to write and easier to debug errors.
 Can be used on different machines with little modifications.
 C++ :- Originated from C
 Fortran:- Formula in Business data processing application
 COBOL-:-Common business Oriented Language
 ALGOL 80:- ( Algorithmic Oriented Language)
 BASIC:- ( Beginners All-purpose symbolic Instruction code) simplest language
developed for solving numerical problem
 Pascal, L, Ada, Modula-2:- Used in teaching programming language.

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.

 Source code: high-level language instructions


 Compiler: system software which translates high-level language instructions into
machine language or object code. The compiler translates source code once and
produces a complete machine language program.
 Object code: translated instructions ready for computer
 For every modern language, there is a program that takes the source code and
produces object code in machine language
 An assembler is a software tool for translating low-level language (assembly
language) into machine language.

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.

Chapter 4 – problem solving using computers


Page 7 of 8


Source program Assembler Object


Or compiler Program

Chapter 4 – problem solving using computers


Page 8 of 8

You might also like