Professional Documents
Culture Documents
Book on arithmetic:
Hindu numeration, decimal numbers, use of zero,
method for finding square root
Latin translation (c.1120 CE): “Algoritmi de
numero Indorum”
Book on algebra
Hisab al-jabr w’al-muqabala
The Problem-solving Process
Analysis
Problem
specification
Design
Algorithm
Implementation
Program
Compilation
Executable
(solution)
Components of an Algorithm
Properties of an algorithm
It should be definite- no ambiguity
It should be finite- definite number of steps
It should be complete- must work
successfully for which it has been
designed for.
It should be efficient
Need for writing algorithm
Effective communication-understandability
Effective analysis-easy to write programs
Proper documentation- to identify logical
errors
Easy and efficient coding
Program debugging-detect & fix errors
Program maintenance- maintenance of
the software becomes easier
Algorithm -- Examples
Assembly instructions for a model
The rules of how to play a game
VCR instructions
Directions for driving from A to B
Algorithmic Notations
Provide the
necessary
Algorithms are identified by a algorithm information as what
name followed by the type of computation the algorithm
performs, along with
it performs the inputs and
Ex: Algorithm Area_of_Circle outputs
( This algorithm computes the area of a circle given radius)
Each step is identified by a number followed by a
description of what it does
Ex: Step 3:[write the area of the circle]
print area
Finally the algorithm is terminated by using a stop/end.
Step 4: [Stop]
finished
Algorithm Example #1:
Algorithm Area_of_a Circle
This algorithm computes the area of a circle given radius
Step 1: [start]
Step 2: [ input the value of radius]
read radius
Step 3: [ calculate the area of the circle]
area= 22/7*r*r
Step 4: [ output the calculated area]
write area
Step 5: [finished]
stop
Algorithm Example #2:
Algorithm Area_of_a_Rectangle
This algorithm computes the area of a rectangle given length and
breadth
step 1: [start]
Step 2: [ input the value of length and breadth]
read length, breadth
1 temp 3
a b
2
Concept of Swapping
“SWAP”- refers to exchange the values between 2
variables, if the 2 variables are taken as a & b with initial
values a=2, b=3 then before swapping a=2,b=3, but after
swapping a=3,b=2, indicating that the values
are interchanged & this is achieved by using a temporary
variable called “temp” to hold the value during the
exchange process.
Steps involved in the exchange process are
temp=a, Copy the value of a to temp
a=b, Copy the value of b to a
b=temp. Copy the value of temp to b
Algorithm Example # 3:
Algorithm Swap
This algorithm exchanges the values of a with b and vice versa
step 1: [start]
Step 2: [ input the value of a and b]
read a, b
Step 3: [ perform the exchange operation]
temp=a, the value of a is put to temp, hence a is empty
a=b, the value of b is put to a, thus b is empty
b=temp, temp contains the value of a and is put to b
Step 6: [finished]
stop
Algorithm Example # 9:
Algorithm biggest _of_ 3 numbers Using if..else statement
This algorithm determines the largest or biggest among 3
numbers step 1: [start]
step 1: [start] Step 2: [ input the value of a, b, c]
Step 2: [ input the value of a,b,c] read a, b, c
read a,b,c
Step 3: [ compare and b]
Step 3: [ assume that a is largest] if (a>b) and (a>c)
largest=a write a
Step 4: [ obtain the largest among a and b] else if (b>a) and (b>c)
if (b> largest) write b
largest=b else
End if write c
Step 5:[ obtain the largest among a ,b,c] End if
if (c> largest) Step 4: [finished]
largest=c stop
End if
Step 7: [finished]
stop
Algorithm Example # 9:
Algorithm roots_of_ a Quadratic equation
This algorithm determines the roots of a quadratic equation
step 1: [start]
Step 2: [ input the coefficients of the quadratic equation]
read a,b,c
Step 3: [ check whether the roots can be formed]
if(a=b=c=0)
write roots cannot be formed
exit @ a=1,b=-4,c=4,
Step 3: [ find the 2 equal roots ] solve the Q.E.
if (b2-4ac=0)then
root 1=root 2= -b/(2xa)
write (“equal roots”, root 1, root2)
goto step 6
end if
C Program
Flow Chart
Flowcharts use standard symbols to represent a type of operation or process to
be performed. The use of standardized symbols provides a common language for people
to visualize problems and also makes flowcharts easier to read and understand.
Types of Flowcharts
There are four basic types of flowcharts: Basic, Process, Deployment, and Opportunity.
Process flowcharts examine the process in great detail. They provide a comprehensive listing of
all the major and sub-steps in a process.
Deployment flowcharts are similar to Process flowcharts in that they are very detailed but also
indicate the people who are involved in the process. This could be very useful when the process
involves cooperation between functional areas.
Opportunity flowcharts highlight decision step and check point. They are used for very
complicated processes because they highlight specific opportunities for improvement.
Flow Chart - Symbols
Ovals are used to represent starting and ending points to the flowchart
process
Flow lines
Terminators
Start Oval Shape
One line in or out
Used to indicate the beginning
and end of a process
Stop
Data Boxes
Parallelogram shaped
Input One line in, One line out
Used to indicate the input or
output of data from the system.
Output
Process Boxes
Rectangle shaped
One line in, One line out
Used to indicate a process step
Process
Decision Boxes
Diamond shaped
One line in, two lines out
Must contain a binary
Yes/No question (Yes/No,
Question True/False, 0/1, etc)
Used to branch a program
dependent upon a condition
being met
Subprocess Box
Rectangle shaped, with
bars on the sides
One line in, One line out
Subprocess
Used to include a
predefined process
Simple Flowchart
This chart indicates a
typical example of Input,
Process, Output
Three basic structures
All flowcharts are composed of three basic
control structures…
Sequence
Decision
Repetition
Sequence
Events happen one after the
other, with no branching
Flow is directly from top to
bottom
Decision
Uses a decision box to
branch to one of two
options
Flow is downwards
Lines rejoin the flow at
lines, NOT boxes.
Repetition
Used to repeat a process, or
wait until a condition is met
before proceeding.
Flow is upwards.
Lines join at lines, NOT
boxes.
Bad design…
What is wrong with this chart?
The box has two lines in,
one line out…
Lines must join other lines,
never boxes!
Bad design…
What is wrong with this
flowchart?
Never use curved lines.
Always straight
Always vertical or
horizontal.
Bad design…
What is wrong with this
flowchart?
Never use curved lines.
Always straight
Always vertical or horizontal.