Algorithm and Flowcharts
A typical programming task can be divided into two phases:
a) Problem solving phase
produce an ordered sequence of steps that describe solution of problem
this sequence of steps is called an algorithm
b) Implementation phase
implement the program in some programming language
Steps in Problem Solving
1. First produce a pseudocode
2. Refine the pseudocode successively to get step by step detailed algorithm that is very close to a computer language
3. Write the algorithm
Pseudocode & Algorithm:
Example 1: Write an algorithm to determine a student’s final grade and indicate whether it is passing or failing. The final
grade is calculated as the average of four marks.
Pseudocode: Detailed Algorithm
• Input a set of 4 marks Step 1: Input M1,M2,M3,M4
• Calculate their average by summing and dividing by 4 Step 2: AVERAGE = (M1+M2+M3+M4)/4
• if average is below 50 Step 3: if (AVERAGE < 50) then
Print “FAIL” Print “FAIL”
else else
Print “PASS” Print “PASS”
endif endif
Flowchart
Flowchart is a schematic representation of a sequence of operations in a manufacturing process or program
Information system flowcharts show how data flows from source documents through the computer to final
distribution to users. Program flowcharts show the sequence of instructions in a single program or subroutine.
Different symbols are used to draw each type of flowchart.
Pseudocode Algorithm Flowchart
Pseudocode is an artificial and An algorithm is writing and presenting Flowchart is a representation of
informal language that helps the solution to a problem in a well sequence of steps in a manufacturing
programmers to develop algorithms. defined set of steps or instructions. process or computer program.
Pseudocode is similar to everyday A recipe in a cookbook is a good Graphical representation of algorithm
English. example of an algorithm of a program
Advantages and disadvantages of Flowcharts
Advantages Disadvantages
Communication: Flowcharts are better way of Complex logic: In case of complicated programming,
communicating the process graphically flowchart becomes complex and clumsy
Effective analysis: With the help of flowchart, problem can Alterations and Modifications: If changes are required the
be analyzed in more effective way flowchart may require re-drawing completely.
Proper documentation: Program flowcharts serve as a Reproduction: As the flowchart symbols cannot be typed,
good program documentation, which is needed for various reproduction of flowchart becomes a problem.
purposes The essentials of what is done can be lost in the technical
Efficient Coding: The flowcharts act as a guide or blueprint details of how it is done
during the systems analysis and program development
phase
Proper Debugging: The flowchart helps in detection of
errors
Efficient Program Maintenance: The maintenance of
operating program becomes easy with the help of
flowchart. It helps the programmer to put efforts more
efficiently on that part
Advantages and disadvantages of Pseudocode
Advantages Disadvantages
It can be done written and modified easily on a word Converting a pseudocode to programming language is
processor easy as compared with converting a flowchart
It can be read and understood easily to programming language
Clarifies algorithms in many cases It's not visual and it doesn’t offer clear picture of
Helps to detect errors before they become code program like algorithm and flowchart
Its implementation is very useful in structured design It creates an additional level of documentation to
elements maintain
triggers possibilities of errors in translating to code
There is no standardized style or format of pseudocode,
so one pseudocode may be different from another
A Flowchart
o shows logic of an algorithm
o emphasizes individual steps and their interconnections
o e.g. control flow from one action to the next
Consider the following flowchart for example
Example 2
Write an algorithm and draw a flowchart to convert
the length in feet to centimeter.
Pseudocode:
Input the length in feet (Lft)
Calculate the length in cm (Lcm) by multiplying
LFT with 30
Print length in cm (LCM)
Example 3
Write an algorithm and draw a flowchart that will read the two sides of a rectangle and calculate its area.
Pseudocode Print A
• Input the width (W) and Length (L) of a
rectangle
• Calculate the area (A) by multiplying L with W
• Print A
DECISION STRUCTURES
The expression A>B is a logical expression
It describes a condition we want to test
If A>B is true (if A is greater than B) we take the action on left
print the value of A
If A>B is false (if A is not greater than B) we take the action on right
print the value of B
IF–THEN–ELSE STRUCTURE
The structure is as follows
If condition then
true alternative
else
false alternative
endif
Relational Operators
Relational Operators
Print B
> Greater than ³ Greater than or equal to
< Less than £ Less than or equal to
= Is equal to ¹ Not equal to
Example 5
Write an algorithm that reads two values, determines the largest value and prints the largest value with an identifying
message.
ALGORITHM
Step 1: Input VALUE1, VALUE2
Step 2: if (VALUE1 > VALUE2) then
MAX = VALUE1
else
MAX = VALUE2
endif
Step 3: Print “The largest value is”, MAX
NESTED IFS
Nested ifs are one of the alternatives within an IF–THEN–ELSE statement. It may involve further IF–THEN–ELSE
statement
Example
Write an algorithm that reads three numbers and prints the value of the largest number.
Step 1: Input N1, N2, N3
Step 2: if (N1>N2) then
if (N1>N3) then
MAX = N1 [N1>N2, N1>N3]
else
MAX = N3 [N3>N1>N2]
endif
else
if (N2>N3) then
MAX = N2 [N2>N1, N2>N3]
else
MAX = N3 [N3>N2>N1]
endif
endif
Step 3: Print “The largest number is”, MAX
Exercise: Draw the flowchart of the above Algorithm
Print Max
Loop
A loop is a series of commands that will continue to repeat over and over again
until a condition is met. For example, you want to print your name for five times.
Instead of keeping five output statements you can have loop statement
and one input statement
Algorithm to print your name for five times
Step 1 : count=1
Step 2: while (count <=5)
2.a print “your name”
2.b count=count+1
[end of while]
Exercises on Loop
a) Write an algorithm and draw a flowchart to print 1 to 100 using loop
b) Write an algorithm and draw a flowchart to print all even number between 50 to 100 using loop
c) Write an algorithm and draw a flowchart to print 40 to 10 in reverse order using loop (40 39 38…………………..10)
Important terms
1. Variable: A variable is a storage location and an associated symbolic name (an identifier) which contains some
known or unknown quantity or information, a value. The variable name is the usual way to reference the stored
value.
2. Subroutine: A subroutine is a sequence of program instructions that perform a specific task, packaged as a unit. This
unit can then be used in programs wherever that particular task should be performed. Subprograms may be defined
within programs, or separately in libraries that can
be used by multiple No programs.
Answer the following questions.
1. Explain pseudocode, algorithm and flowcharts
2. Differentiate among pseudocode, algorithm and flowcharts
3. Explain advantages and disadvantages of pseudocode
4. Explain advantages and disadvantages of flowcharts
5. Explain decision structure and IF-THEN-ELSE structure with the help of example
6. Write various relational operators and state what they represent
7. Explain Nested IFS with the help of example
8. What is iteration or looping?
9. Explain variable and subroutine
10. Name and explain various flowchart symbols and their functions
11. Explain the phases of creating a program
12. Write step by step working of problem solving phase of a process
13. What are the basic elements of flowchart?
14. Discuss uses of flowcharts
15. Name various structures of flowcharts