2. Algorithm
A typical programming task can be divided into
two phases:
Problem solving phase
Implementation phase
Algorithm is a written series of logical instructions
which accomplish solution to a problem.
Pseudo code is an artificial and informal language
that helps to develop algorithms which is similar to
everyday English
3. Flowchart
Flowchart shows graphical representation of a
sequence of steps involved in a program
(algorithm)
The two major types of flowchart:
System flowchart – depicts the entire data flow from
one program to another throughout a system. It
provides a logical diagram of how the system
operates.
Program flowchart – depicts the series of operations
that the computer follows to generate the desired
information. It represents in detail, the various steps to
be performed within the system for transforming the
4. Flowchart Symbols
Name Symbol Use in Flowchart
Terminal Defines the starting and ending point of flowchart
Initialization
The preparation/initialization of memory space for data
processing
Input/Output
The inputting of data for processing, and the printing out
of processed data
Process
Manipulation of data (assignments and mathematical
computations)
Predefined
Process
Manipulation of data (assignments and mathematical
computations) in function/subroutine form
Decision
Process conditions using relational operators. Used for
trapping and filtering data
Flow Lines
Defines the logical sequence of the program. It points to
the next symbol to be performed
On-page
Connector
Connects the flowchart to avoid spaghetti connection on
the same page
Off-page
Connector
Connects the flowchart to avoid spaghetti connection on
different page
5. Basic Control Structure
Used to organize the flow of control in an
algorithm
Also called single entry/single exit structure,
there are three basic structure:
Sequence – the process is execute from one to
another in a straight forward manner
Entry Exit
6. Basic Control Structure
Selection – a choice is provided between two
alternatives
The condition to be tested
Process/statement to be performed if the condition is
satisfied
Process/statement to be performed if the condition is
satisfied
True
False
Exit
Entry
7. Basic Control Structure
Iteration/Repetitive or loop control – provides a
means of repeating part of an instruction without
rewriting that part of an instruction
Body of the loop
Loop-exit condition
Entry
Exit
8. Example No. 1
Pseudo code:
1. Print “Enter your name”
2. Input a name
3. Print “Hello” and the name inputted
Algorithm:
1. Print “Enter your name”
2. Input Name
3. Print “Hello”, Name
10. Example No. 2
Pseudo code:
1. Print “The program will add 2 numbers:”
2. Print “Input the first number:”
3. Input the first number
4. Print “Input the second number:”
5. Input the second number
6. Calculate the sum of the two numbers
7. Print “The sum is”, and the sum of the two
numbers
11. Example No. 2
Algorithm:
1. Print “The program will add 2 numbers:”
2. Print “Input the first number:”
3. Input fst
4. Print “Input the second number:”
5. Input sec
6. Sum = fst + sec
7. Print “The sum is”, Sum
12. Example No. 2
Flowchart:
Start
fst, sec, Sum
Print (“The program will
add 2 numbers:”)
Print “Input the first
number:”
Input fst
1
1
Print (“Input the second
number:”)
Input sec
Print (“The sum
is”, Sum)
Sum = fst + sec
Stop
13. How many
hours did
you work?
START
Print(“How
many hours did
you work?”)
Input Hours
Print(“How
much do you
get paid per
hour?”)
Input PayRate
Print (“Gross
Pay”,
GrossPay)
END
Variable Contents:
Hours: ?
Pay Rate: ?
Gross Pay: ?
Output
Operation
Stepping through the
Flowchart
GrossPay=Hours
* PayRate
14. How many
hours did
you work?
40
START
Print(“How
many hours did
you work?”)
Input Hours
Print(“How
much do you
get paid per
hour?”)
Input PayRate
Print (“Gross
Pay”,
GrossPay)
END
Variable Contents:
Hours: 40
Pay Rate: ?
Gross Pay: ?
Input
Operation
(User types
40)
GrossPay=Hours
* PayRate
Stepping through the
Flowchart
15. How much
do you get
paid per
hour?
START
Print(“How
many hours did
you work?”)
Input Hours
Print(“How
much do you
get paid per
hour?”)
Input PayRate
Print (“Gross
Pay”,
GrossPay)
END
Variable Contents:
Hours: 40
Pay Rate: ?
Gross Pay: ?
Output
Operation
GrossPay=Hours
* PayRate
Stepping through the
Flowchart
16. How much
do you get
paid per
hour? 20
START
Print(“How
many hours did
you work?”)
Input Hours
Print(“How
much do you
get paid per
hour?”)
Input PayRate
Print (“Gross
Pay”,
GrossPay)
END
Variable Contents:
Hours: 40
Pay Rate: 20
Gross Pay: ?
Input
Operation
(User types
20)
GrossPay=Hours
* PayRate
Stepping through the
Flowchart
17. How much
do you get
paid per
hour? 20
START
Print(“How
many hours did
you work?”)
Input Hours
Print(“How
much do you
get paid per
hour?”)
Input PayRate
GrossPay=Hours
* PayRate
Print (“Gross
Pay”,
GrossPay)
END
Variable Contents:
Hours: 40
PayRate: 20
GrossPay: 800
Process: The
product of 40
times 20 is
stored in
Gross Pay
Stepping through the
Flowchart
18. START
Print(“How
many hours did
you work?”)
Input Hours
Print(“How
much do you
get paid per
hour?”)
Input PayRate
Print (“Gross
Pay”,
GrossPay)
END
Variable Contents:
Hours: 40
Pay Rate: 20
Gross Pay: 800
Output
Operation
Gross pay is
800
GrossPay=Hours
* PayRate
Stepping through the
Flowchart
19. Example No.3
1. Create a flowchart that
computes the product and
the quotient of two numbers.
20. Start
n1,n2,
prod,quo
Print (“ Enter
two numbers”)
prod=n1* n2
quo=n1/ n2
1
1
Print (“ The Product of
two numbers :”, prod)
Print (“ The Quotient of two
numbers :”, quo)
Stop
Example No.3
Input n1, n2
21. Seatwork:
1. Create a flowchart that will
convert inches to centimeter.
Wherein, 1 inch = 2.54cm.
22. Start
inch, cen
Print (“ Enter
Inch(es)”)
cen=inch* 2.54
Print (“ Converted Inche(s)”, cen)
Stop
Input inch
23. Additional exercises:
Add 10 and 20
Algorithm (in simple English)
Initialize sum = 0 (PROCESS)
Enter the numbers (I/O)
Add them and store the result in sum
(PROCESS)
Print sum (I/O)
Flowchart
24.
25. Find the sum of 5 numbers
Algorithm (in simple English)
1. Initialize sum = 0 and count =
0 (PROCESS)
2. Enter n (I/O)
3. Find sum + n and assign it to sum and then
increment count by 1 (PROCESS)
4. Is count < 5 (DECISION)
5. if YES go to step 2
else
Print sum (I/O)
26.
27. Print Hello World 10 times
Algorithm (in simple English
1. Initialize count = 0 (PROCESS)
2. Print Hello World (I/O)
3. Increment count by 1 (PROCESS)
4. Is count < 10 (DECISION)
5. if YES go to step 2
else Stop
28.
29. Draw a flowchart to log in to facebook
account
Algorithm (in simple English)
1. Enter www.facebook.com in your
browser. (I/O)
2. facebook Home page loads (PROCESS)
3. Enter your Email ID and Password (I/O)
4. Is Email ID and Password Valid (DECISION)
if NO then
Log in error (PROCESS)
go to step 3
else
30.
31. Find the sum of the numbers in each set.
You are given hundred numbers divided in ten
sets in the following order.
Set 1: 1-10
Set 2: 11-20
Set 3: 21-30
…
Set 10: 91-100
32. Algorithm in simple English
Initialize count = 1 and i = 1 (PROCESS)
Check if i is less than or equal to 10 (DECISION)
if YES then perform step 3
else STOP
Set sum = 0 and j = 1 (PROCESS)
Check if j is less than or equal to 10 (DECISION)
if YES then perform step 5
else perform step 9
Add count to sum
sum = sum + count (PROCESS)
Increment count by 1
count = count + 1 (PROCESS)
Increment j by 1
j = j + 1 (PROCESS)
Go to Step 4
Print sum (I/O)
Increment i by 1
i = i + 1 (PROCESS)
Go to Step 2
Editor's Notes
Problem Solving:
Analysis and Specification
Understand (define) the problem and what the solution must do
Algorithm Development
Develop a comprehensive unambiguous logical sequence of steps to solve the problem
Verification of Algorithm
Follow steps closely (manually) to see if solution works
Implementation Phase:
Program Development
Translate algorithm into a program written in a programming language
Program Testing
Test program for syntactical and logical errors. Fix the errors.
Algorithm vs. Pseudocodes
Algorithm is basically a step by step procedure to solve a particular problem .
Pseudocode is the informal representation of the code, doesn't follow rules of the language