Algo & Flow Chart

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 7

1

Chapter 1: Algorithms and Flowcharts:

Algorithms:

Definitions:

 An algorithm is a precise specification of a sequence of instructions to be carried out in


order to solve a given problem.
 An Algorithm is a technique to solve a given problem systematically.
 An Algorithm is a step-by-step approach to solve a given problem in finite number of steps.
 It takes a set of input values and processes it and gives the output.
 Each instruction tells what task is to be performed.

Properties (characteristics) of an Algorithm:

 It should be simple.
 It may accept zero or more inputs.
 It should be precise with no ambiguity.
 It should have finite number of steps.
 It should produce atleast one output.

Need for an Algorithm:

1. Efficient Communication: Since an algorithm is written in English like language, it seems to


be the better way of communicating the logic. Ie.., a procedure to solve a given problem to
the users.
2. Efficient Analysis: With the help of an algorithm problem can be analysed effectively. There
can be number of algorithms for solving a given problem. Based on the analysis, a suitable
and efficient algorithm can be selected.
3. Proper Documentation: The selected algorithm has to be documented, which will be useful
to identify the logical errors.
4. Easy and efficient coding: the algorithm act as blue print during program development. It will
be easier to write efficient program using high level language.
5. Program Maintenance: Maintaining the software or set of programs becomes easy.

Disadvantages of an Algorithm:

(i) Developing algorithms for large and complex problems is time consuming and difficult to
understand.
(ii) Understanding complex logic is tedious.

Flowchart:
2

Definitions:

 A flowchart depicts pictorially the sequence in which instructions are carried out in
an algorithm.
 A flowchart is a schematic representation of an algorithm or a stepwise process,
showing the steps of boxes of various kinds and their order by connecting these with
arrows.
 Flowcharts are used not only as aids in developing algorithms but also to document
algorithms.
 Flowcharts are used in designing or documenting a process or program.

Flowchart symbols:

1) -Start and Stop: It represents the beginning and ending of flowchart

(or procedure).

2) -Input/Output: This is used to accept the variable for processing and display the

processed results.

3) -Processing: This is used to process the data.

4) -Connector: It is used to connect the flowchart or data flow if it exceeds one

page.
3

5) -Decision Making: Is used for comparison, decision making and branching.

6) -Looping: Is used for repeating steps several numbers of times until the given

condition is satisfied.

7) -Preprocessing: Is used to define a module or sub-program.

8) -Data Flow: It indicates the flow of data from one geometrical signal to another.

Need for the flowchart:

1. Efficient Communication: since its represented in pictorial form a flowchart seems to be the
better way of communicating the logic to the programmers or any common man.
2. Efficient Analysis: With the help of a flowchart problem can be analyzed effectively.
3. Proper Documentation: flowchart plays an important role in understanding the logic of
complicated and lengthy problems. Hence it is very much required to document the
flowcharts along with algorithms.
4. Easy and efficient coding: by looking at the flowchart writing programs is a straight forward
approach.
5. Program Debugging: flowchart helps in debugging process i.e., to identify the logical errors
in the program.
6. Program Maintenance: Maintaining the software or set of programs becomes easy.

Drawbacks of flowchart:

(i) Complex logic: For complicated logic the flowchart becomes complex and clumsy.
4

(ii) Alterations and modifications: if there is a change in the logic, the flowchart has to
be completely rewritten and requires lot of time.
(iii) Reproduction: in the case of any problem, reproduction of flowchart becomes
problem since the flowchart symbols cannot be typed.

Examples for Algorithms:


1. Algorithm to swap the contents of two variables.

Step 1: [Input 2 numbers]


Read a, b;

Step 2: [Exchange a & b]


Temp=a;
a=b;
b=temp;

Step 3: [Output the result]


Display a b.

Step 4: [Terminate]
Stop.

2. Algorithm to check whether a given number is even or odd.

Step 1: [Input the number].


Read n.

Step 2: [check for remainder when divided by 2]


Check condition (n%2==0) then
If true display “The number is even”.
Else
If false display “the number is odd”.
Step 3: [Terminate]
Stop.

3. Algorithm to check whether a given number is positive or negative.

Step 1: [Input the number].


Read n.

Step 2: [Check whether the number is + ve or –ve].


Check condition (n<=0) then
If true Display “The number is negative”
5

Else
If false Display “the number is positive”.
Step 3: [Terminate]
Stop.

4. Algorithm to find the area of the triangle.

Step 1: [Input 3 sides of the triangle].


Read a,b,c.

Step 2: [Compute the value of s].


S = (a+b+c)/2.

Step 3: [Compute the area of the triangle].


Area= (s*(s-a)*(s-b)*(s-c))

Step 4: [Output the result].


Display area.

Step 5: [Terminate].
Stop.

5. Algorithm to find the biggest and smallest of given set of numbers.

Step 1: [ initialization]
small = big = temp
Step 2: [Input the variables].
Read temp.

Step 3: [for the first iteration , Initialize]


Count=0
[Check whether count=0]
Check condition (count==0) then
small = big = temp.
else go to step 4.

Step 4: [Increment the count value].


count = count + 1.

Step 5: [Check whether temp < small]


if (temp < small) then
small = temp.
else if (temp > big) then
big = temp.

Step 6: [Check for the value of count]


if (count==10)
6

display “smallest and biggest number is “ smallbig.


else
go to step 2.
Step 7: [Terminate].
Stop.

6. Algorithm to find the factorial of a given number.

Step 1: [Read the number]


Read n.

Step 2: [Initialization]
Factorial =1.

Step 3: [Find the factorial of n]


For i <- 1 to n in steps of 1 do
factorial = factorial * i.
End for.

Step 4: [Output the result]


Display factorial.

Step 5: [Terminate]
Stop.

7. Algorithm to find the sum of first N natural numbers.

Step 1: [Input the number of terms]


Read n.

Step 2: [Initialization]
Sum=0

Step 3: [Find the sum of all terms]


For i <- 1 to n in steps of 1 do
sum = sum + i
end for.
Step 4: [output the result]
Display sum.

Step 5: [Terminate]
Stop.
7

8. Algorithm to find the biggest of 3 numbers.

Step 1: [Read the numbers]


Read a,b,c.

Step 2: [Comparision]
Check condition (a>b) and (a>c) then
display a is greatest.
else check if (b>c) then
display b is greatest.
else display c is greatest.
Step 3: [Terminate]
Stop.

9. Algorithm to find the roots of the quadratic equation.

Step 1: [Input the co-efficient of Quadratic Equation]


Read a, b, c.

Step 2: [Find whether the roots are equal roots]


Check condition (b2 – 4ac) is equal to 0 then
x1=x2=(-b/2a)
display “The roots are equal” x1 x2.

Step 3: [Find whether the roots are distinct]


Check condition (b2 – 4ac) > 0 then
x1= ((-b+ b2 – 4ac)/2a).
x2= ((-b - b2 - 4ac )/2a).
display “Two distinct roots” x1 x2.
else
display “Complex roots” x1 x2.
Step 4: [Terminate]
Stop.

You might also like