Algorithm Introduction
An algorithm is an effective step-by-step procedure for solving a
problem in a finite number of steps. In other words, it is a finite set of
well-defined instructions or step-by-step description of the procedure
written in human readable language for solving a given problem. An
algorithm itself is division of a problem into small steps which are
ordered in sequence and easily understandable. Algorithms are very
important to the way computers process information, because a
computer program is basically an algorithm that tells computer what
specific tasks to perform in what specific order to accomplish a specific
task. The same problem can be solved with different methods. So, for
solving the same problem, different algorithms can be designed. In
these algorithms, number of steps, time and efforts may vary more or
less.
Characteristics of an Algorithm
An algorithm must possess following characteristics :
1. Finiteness: An algorithm should have finite number of steps and
it should end after a finite time.
2. Input: An algorithm may have many inputs or no inputs at all.
3. Output: It should result at least one output.
4. Definiteness: Each step must be clear, well-defined and precise.
There should be no any ambiguity.
5. Effectiveness: Each step must be simple and should take a finite
amount of time.
Guidelines for Developing an Algorithm
Following guidelines must be followed while developing an
algorithm :
1. An algorithm will be enclosed by START (or BEGIN) and STOP (or
END).
2. To accept data from user, generally used statements are INPUT,
READ, GET or OBTAIN.
3. To display result or any message, generally used statements are
PRINT, DISPLAY, or WRITE.
4. Generally, COMPUTE or CALCULATE is used while describing
mathematical expressions and based on situation relevant
operators can be used.
Example of an Algorithm
Algorithm : Calculation of Simple Interest
Step 1: Start
Step 2: Read principle (P), time (T) and rate (R)
Step 3: Calculate I = P*T*R/100
Step 4: Print I as Interest
Step 5: Stop
LIMITATIONS of Algorithms:
Alogorithms is Time consuming.
Difficult to show Branching and Looping in Algorithms.
Big tasks are difficult to put in Algorithms.
What is Pseudocode?
Pseudocode is an informal high-level description of a computer
program or algorithm. It is written in symbolic code which must
be translated into a programming language before it can be
executed.
Mathematical operations
Assignment: ← or :=
Example: c ← 2πr, c := 2πr
Comparison: =, ≠, <, >, ≤, ≥
Arithmetic: +, −, ×, /,
modFloor/ceiling: ⌊, ⌋, ⌈, ⌉
a ← ⌊b⌋ + ⌈c⌉
Logical: and, or
Sums, products: Σ Π
Example: h ← Σa∈A 1/a
Keywords
TART: This is the start of your pseudocode.
INPUT: This is data retrieved from the user through typing or
through an input device.
READ / GET:
This is input used when reading data from a data file.
PRINT, DISPLAY, SHOW: This will show your output to a screen or the
relevant output device.
COMPUTE, CALCULATE, DETERMINE: This is used to calculate the result
of an expression.
SET, INIT: To initialize values
INCREMENT, BUMP: To increase the value of a variable
DECREMENT: To reduce the value of a variable
CONDITIONALS
F — ELSE IF — ELSE
This is a conditional that is used to provide statements to be
executed if a certain condition is met. This also applies to multiple
conditions and different variables.
Here is an if statement with one condition
you are happy
THEN smile
ENDIF
We can add additional conditions to execute different statements
if met.
IF you are happy THEN
smile
ELSE IF you are sad
frown
ELSE
keep face plain
ENDIF
ITERATION
To iterate is to repeat a set of instructions in order to generate a
sequence of outcomes. We iterate so that we can achieve a certain
goal.
FOR structure
The FOR loop takes a group of elements and runs the code within
the loop for each element.
FOR every month in a year
Compute number of days
ENDFOR
WHILE structure
PRECONDITION: variable X is equal to 1
WHILE Population < Limit
Compute Population as Population + Births — Deaths
ENDWHILE