Fundamentals Of AlgorithmsFundamentals Of Algorithms
Algorithms RepresentationAlgorithms Representation•Algorithm transforms input to output after doing
some processing
•Algorithms can be written byPseudo codeFlow charts
•Algorithms implemented in some programming language
Writing ProgramsWriting Programs
Understanding the problem
Designing theSolution
Implementation(Coding)
Testing
FlowchartsFlowcharts•A Flowchart is a diagram representing the logical
sequence in which a combination of steps or operations is to be performed.
•A graphic representation, using symbols interconnected with lines, of the successive steps in a procedure or system.
•A visual representation of an algorithm.
•Also known as Flow Sheet or Flow Diagram
FlowchartsFlowcharts•Flowcharts aid in breaking down a problem into
simple steps
•Most widely used graphic method for describing computer operations.
•Helps in communicating the nature of the operation, regardless of the programming language or the computer used.
Program FlowchartProgram Flowchart•Represents the sequence of operations/ statements
that computer performs to solve a specific problem.
•It graphically describes what is to take place in the program.
•It displays specific operations and decisions, and their sequence within the program.
Flowcharting SymbolsFlowcharting Symbols•Symbols are used to represent functions.
•These fundamental functions are processing, decision, input/output, terminal, flow lines and connector symbol.
•The contents of these symbols are called statements.
Flowcharting SymbolsFlowcharting Symbols
1. Process Symbol ( Rectangle)
Used whenever data is being manipulated, most often with arithmetic operations.
A single flow line enters and a single flow line exits
ComputeMonthly Interest
R = I/12
Divide I by 12Assign Value to R
Flowcharting SymbolsFlowcharting Symbols
2. Input/Output Symbol (Parallelogram)
Used whenever information is being entered into the flowchart or displayed from the flowchart.
One flow line enters and one flow line exits.
Enter 3 values to be stored in location A, B, C
InputA, B, C
Flowcharting SymbolsFlowcharting Symbols
3. Decision Symbol (Diamond)
Used to represent operations in which there are two possible alternatives (sometimes 3).
One flow line enters and two (or three) flow lines exit. If A is NOT equal to B then
take the NO Branch
If A is equal to B then take the YES Branch
Is A=B
?
Yes
No
Flowcharting SymbolsFlowcharting Symbols
4. Preparation Symbol (Hexagon)
Used to initialize objects or variables to be used in the flowchart.
One flow line enters and one flow line exits.
Set the value of C to zeroInitializeC = 0
Flowcharting SymbolsFlowcharting Symbols
5. Terminal Symbol (Oval)
Used to designate the beginning and end of a program, or a point of interruption. For example , Start, Stop, Halt, Delay, or Interrupt.
Single flow line. Flow begins or terminates here.
Start/Stop flowchart at this point
Start Stop
Flowcharting SymbolsFlowcharting Symbols
6. On-page Connector (Small Circle)
Used to connect remote flowchart portions on the same page.
One flow line enters or exits.A
A
Represents the entry and exit points in a flowchart
Entry Exit
A
A
Flowcharting SymbolsFlowcharting Symbols
7. Off-page Connector (Small Pentagon)
Used to connect remote flowchart portions on different pages.
One flow line enters or exits.
Represents the entry and exit points in a flowchart in different pages
Entry Exit
11
Flowcharting SymbolsFlowcharting Symbols
8. Comment Symbol
Used to add comments or clarifications.
Left Comment
Right Comment
This is a top secret data
Flowcharting SymbolsFlowcharting Symbols
9. Flow Lines (Horizontal/Vertical Lines)
Used to show reading order or sequence Sometimes drawn with arrowheads.
Arrowheads are not required when the logic flow is from top to bottom or from right to left.
Flowcharting SymbolsFlowcharting Symbols
10. Flow Direction Indicators or Flow Arrowheads
Used to show the direction of processing or data flow.
These are added to the flow lines if a flowchart’s layout appears confusing.
Flowcharting SymbolsFlowcharting Symbols
Graphic SymbolsGraphic Symbols
NOTATION MEANING NOTATION MEANING+ Add Less than or equal to- Subtract Not equal to* Multiply ! Not/ Divide !> Not greater than Plus or minus !< Not less than= equal to YES or Y Yes> greater than NO or N No< less than TRUE or T True greater than or equal to FALSE or F False
• Within a flowchart, graphic symbols are used to specify arithmetic operations and relational conditions.
Start
sum=0
Input price
sum=sum+price
Moreitems?
tax=sum x 0.0725total=sum+tax
Output sum, tax, and total
Stop
No
Yes
Flowcharting ExamplesFlowcharting Examples• Flowchart for a cash register program
Flow Charts Symbols (Summary)Flow Charts Symbols (Summary)
Terminator.
Data Input or Output.
Processing.
Decision.
Predefined Process.
Connector.
Off-page connector..
Flowline.
Flow Chart StructureFlow Chart Structure
Start
Action
Action
Action
Finish
Decision
Finish
ActionAction
Start
Finish
Test
Start
Sequence Selection Loop
Action
A Pre-Test Repetition StructureA Pre-Test Repetition Structure•This type of structure is known as a pre-test repetition structure.
The condition is tested BEFORE any actions are performed.
x < y? Display x
Add 1 to xYES
A Pre-Test Repetition StructureA Pre-Test Repetition Structure•In a pre-test repetition structure, if the condition does not exist,
the loop will never begin.
x < y? Display x
Add 1 to xYES
A Post-Test Repetition StructureA Post-Test Repetition Structure•This flowchart segment shows a post-test
repetition structure.
•The condition is tested AFTER the actionsare performed.
•A post-test repetition structure alwaysperforms its actions at least once.
Display x
Add 1 to x
YESx < y?
Using a Repetition Structure Using a Repetition Structure •Example:
Draw a flowchart to compute the area of the circles starting with R=1.0 up to R=5.0, then print out each radius and the corresponding area of the circle.
•Solution:Given: R=1, Pi =3.1416
Output: R, Area (where radius (R) is from 1.0 to 5.0)
Start
Stop
Area = PI * R * R
Print R, Area
R= 1PI = 3.1415
R = R + 1.0
ISR <= 5.0
?A
A
Y
N
1. Initialize the value of R to 1 and the value of Pi to 3.1416
2. Area = Pi * R *R
3. Print R, A
4. R = R+1 (Increment value of R by 1)
5. Is R <= 5.0? (Test if R is less than or equal to 5)
6. If R is less than or equal to 5, loop back and repeat steps 2 through 5. However, if R is greater than 5, stop processing.
Using a Repetition Structure Using a Repetition Structure
ExercisesExercises•Draw a flow chart to find
the larger of two numbers Start
InputX , Y
Is X
OutputX is greater
OutputY is greater
OutputNumbers are Equal
Stop
> y
= y
< y
Finding Largest NumberFinding Largest Number•What about finding largest
of three non equal numbers? Start
InputX , Y, Z
Is X < Y
F T
Is X < Z
Is Y < Z
T
F T F
Stop
OutputX is greater
OutputZ is greater
OutputY is greater
1 1
•Finding Factorial of a number N (Pre Test Condition)
Outputfactorial
Stop
FStart
InputN
counter = N
factorial = 1
is counter > 0
T
factorial = factorial x counter
counter = counter - 1
HomeworkHomework•Draw flow charts for following problems:-
Find largest of list of 10 numbersFind GCD of two numbersSearch a number from list of n numbersInput n numbers and calculate their average