You are on page 1of 5

A.

FLOW CHART AND ALGORITHM

The main purpose of using an algorithm is to illustrate the flow of data and problem solution logic.

A.1 What is Algorithm?

An algorithm is a finite sequence of well-defined steps or operations for solving a problem in a


systematic manner. In another words we can say that an algorithm is a step-by-step problem-solving
procedure.

An algorithm generally takes some input, carries out a number of effective steps in a finite amount of
time, and produces some output.

A.1.1 Rules for writing Algorithm

1. Each instruction should be precise and unambiguous.


2. Each instruction should be executed in a finite time.
3. One or more instructions should not be repeated infinitely.
4. After executing the instructions, the desired results are obtained.

Algorithm Example 1: Write an algorithm to add two nos.


Step1: start
Step2: Read a, b
Step3: sum = a + b
Step4: write sum
Step5: stop

Algorithm Example 2 Simple Calculator:


1. Start
2. Read a, b
3. Compute sum = a + b
4. Compute sub = a – b
5. Compute mul = a * b
6. Compute div = a / b
7. Compute mod = a % b
8. Write sum, sub, mul, div, mod

Algorithm Example 3 Find volume of a circle


1. Start
2. Read r
3. v = 4/3*(3.14)*(r*r*r)
4. Write v
5. Stop

B. PSEUDOCODE
Kind of structured English for describing algorithms. It allows the designer to focus on the logic of the
algorithm without being distracted by details of language syntax. At the same time, the pseudocode
needs to be complete. It describes the entire logic of the algorithm so that implementation becomes a
rote mechanical task of translating line by line into source code

Pseudocode is a compact and informal high-level description of a program using the conventions of a
programming language, but intended more for humans.

B.1 Why Pseudocode?


Pseudocode omits programming level details (like declaration of variables, looping syntax ...) and so it
makes things very easy to understand for human being and implement it in any programming language
easily.

B.2 How to Write Pseudocode?

There is no pseudocode standard syntax and so at times it becomes slightly confusing when writing
Pseudocode and so let us understand pseudo code with an example.

Example #1
Write a program to find the Perimeter and Area of a rectangle.
• -To find the area of a rectangle, you need to know the rectangle's length and width.
• The perimeter and area of the rectangle are then given by the following formulas:
– perimeter = 2 * (length + width)
– area = length * width
Pseudocode
1. Get the length
2. Get the Width
3. perimeter = 2 * (length + width)
4. area = length * width
5. Display the perimeter
6. Display the area

Example -#3:
Pseudo-code to read a number (assumed to be non-zero), check whether it is positive or negative and
output the result.

output(“Input positive or negative numbers”)


Begin:
if number > 0
then
output(“Number is positive”)
endif
if number < 0
then
output(“Number is negative”)
endif
end
C. What is Flow chart?
It is a graphical representation of a program flow or an algorithm of a problem to be solved by a
computer.

It is an aid to solve a complex problem easily and efficiently. It shows the detailed view of a program
flow.

Rules for drawing flowchart:

1. Use common statements that are easy to understand for words within flowchart symbols.
2. Be consistent in using names and variables in the flowchart.
3. Go from left to right and top to bottom in constructing flowcharts.
4. Keep the flowchart as simple as possible.
5. If a new flow charting page is needed, break the flowchart at an input or output point. Use properly
labeled connectors to link the flowchart on different pages.

Advantages of drawing a flowchart:


1. It helps a programmer to get a good visual reference of the structure of a program.
2. It not only serves as program documentation but also helps as a means to communicate with
several programmers, as it is language independent.
3. It allows a programmer to test alternative solutions to a problem without even coding the program.
It is much easier to find and correct logic errors prior to coding.
4. It serves as a useful reference or a programming aid due to its simple and efficient method of
representing the program logic of a problem.
Difference between Flowchart and Algorithm

Algorithm Flowchart
An algorithm is a finite sequence of well-defined
steps or operations for solving a problem in a It is graphical representation of algorithm
systematic manner.
It is easy to write and difficult to understand than It is difficult to draw and easy to understand than
flowchart algorithm
It does not follow strict rules It use standard symbols for drawing.

Example #1: Determine and Output Whether Number N is Even or Odd

Algorithm:

Step 1: Read number N,


Step 2: Set remainder as N modulo 2,
Step 3: If remainder is equal to 0 then number N is even, else number N is odd,
Step 4: Print output.

Flowchart
Example #2: Determine Whether A Student Passed the Exam or Not:

Algorithm:

Step 1: Input grades of 4 courses M1, M2, M3 and M4,


Step 2: Calculate the average grade with formula "Grade=(M1+M2+M3+M4)/4"
Step 3: If the average grade is less than 60, print "FAIL", else print "PASS".

Flowchart

You might also like