Unit 2-Algorithms PDF
Unit 2-Algorithms PDF
Unit 2-Algorithms PDF
Logic
Logic is a tool to develop reasonable conclusions
based on a given set of data. Logic is free of
emotion and deals very specifically with
information in its purest form.
1. Informal logic is the mode used in everyday
reasoning and argument analysis.
2. Formal logic deal with deductive reasoning
and the validity of the inferences produced.
Steps involved solving a particular problem:
Analyze the problem.
Identify the Inputs
Identify the Outputs
Identify the Process
Write the steps in english to solve the problem.
Draw a control flow diagram to link these steps.
Write the program for the solution.
Execute the program with different inputs and
verify for output.
1.1. PROGRAMMING
A typical programming task can be divided into two phases:
Problem solving phase
Produce an ordered sequence of steps that describe solution of problem
This sequence of steps is called an algorithm
Implementation phase
Implement the program in some programming language
• Algorithm:
• An ordered set of unambiguous steps that produces a
result and terminates in a finite time.
ALGORITHM
“Algorithm" is a formally defined procedure for performing some calculation. If a procedure is
formally defined, then it must be implemented using a programming language. The algorithm
gives logic of the program, that is, a step-by-step description of how to arrive at a solution.
Algorithm provides a blueprint to write a program to solve a particular problem in finite
number of steps. Algorithms are mainly used to achieve software re-use
In order to qualify as an algorithm, a sequence of instructions must process the following
characteristics:
Instructions must be precise
Instructions must be unambiguous
Not even a single instruction must be repeated infinitely
After the algorithm gets terminated, the desired result must be obtained
PROPERTIES OF ALGORITHM
1. Input:- an algorithm accepts zero or more inputs
2. Output:- it produces at least one output.
3. Finiteness:- an algorithm terminates after a finite numbers of steps.
4. Definiteness:- each step in algorithm is unambiguous. This means that the
action specified by the step cannot be interpreted (explain the meaning of) in
multiple ways & can be performed without any confusion.
5. Effectiveness:- it consists of basic instructions that are realizable. This means
that the instructions can be performed by using the given inputs in a finite
amount of time.
DEVELOPING AN ALGORITHM
1.Identify the Inputs
What data do I need?
How will I get the data?
In what format will the data be?
Any algorithm has a finite number of steps and some steps may involve decision making,
repetition. Broadly speaking, an algorithm exhibits three key features that can be given as:
Sequence: Sequence means that each step of the algorithm is executed in the specified order.
Decision: Decision statements are used when the outcome of the process depends on some
condition.
Repetition: Repetition which involves executing one or more steps for a number of times can be
implemented using constructs like the while, do-while and for loops. These loops executed one
or more steps until some condition is true.
Three constructs
Algorithm
• Find area of a rectangle
• Step 1: Input L,B
Step 2: area=L*B
Step 3: Print area
Step 4: Exit
• Find the area of a circle
• Step 1: Input R
Step 2: area=3.14*R*R
Step 3: Print area
Step 4: Exit
• Step 1: Input SP,CP
Step 2: if (SP>CP) then
p=SP-CP
Print p
else
l=CP-SP
Print l
endif
Step 3:Exit
• Detailed Algorithm
• Step 1: Input M1,M2,M3,M4
Step 2: GRADE (M1+M2+M3+M4)/4
Step 3: if (GRADE < 50) then
Print “FAIL”
else
Print “PASS”
endif
Step 4:Exit
• Detailed Algorithm
• Step 1: Input M1,M2,M3,M4
Step 2: GRADE (M1+M2+M3+M4)/4
Step 3: if (GRADE > 60) then
Print “Ist”
elseif (GRADE > 50)and (GRADE <60) then
Print “IInd”
else
Print “Fail”
endif
Step 4:Exit
• Step 1: Input the value of N
Step 2: Set i=1
Step 3: Repeat until i<=N
Step 5: Print i
Step 4: Set i=i+1
end loop
Step 5: Exit
Concept of a subalgorithm
Translators
• Translators are just computer programs which
accept a program written in high level or low
level language and produce an equivalent
machine level program as output. Translators
are of three types :
• Assembler
• Compiler
• Interpreter
• Assembler is used for converting the code of low level language
(assembly language) into machine level language.
• A compiler searches all the errors of a program and lists them. If the
program is error free then it converts the code of program into
machine code and then the program can be executed by separate
commands.
The System
The System