100% found this document useful (4 votes)
283 views

Algorithms

Algorithms are step-by-step procedures for solving problems or accomplishing tasks using computers. They involve sequences of instructions, decisions, and repetitions. An algorithm contains both the steps to be followed and the data needed to execute those steps. Pseudocode and flowcharts are common ways to represent algorithms without using a specific programming language. Pseudocode uses a restricted vocabulary to describe the steps in plain language while flowcharts use standard symbols and diagrams to visually depict the program logic and flow of data.

Uploaded by

Siddharth Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
100% found this document useful (4 votes)
283 views

Algorithms

Algorithms are step-by-step procedures for solving problems or accomplishing tasks using computers. They involve sequences of instructions, decisions, and repetitions. An algorithm contains both the steps to be followed and the data needed to execute those steps. Pseudocode and flowcharts are common ways to represent algorithms without using a specific programming language. Pseudocode uses a restricted vocabulary to describe the steps in plain language while flowcharts use standard symbols and diagrams to visually depict the program logic and flow of data.

Uploaded by

Siddharth Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 27

Algorithms

Introduction
Algorithms are the building blocks of any
computer program
They are a set of rules to be followed in order to
solve problems using computers
What is an Algorithm?
Programs = Algorithm + Data
An algorithm is a part of the plan for the
computer program; an algorithm is an effective
procedure for solving a problem in a finite
number of steps.
It is effective, which means that an answer is
found and it has a finite number of steps and
also guaranteed to terminate

The Key Features of an


Algorithm
Algorithm Example: Making a pot of tea
1. If the kettle does not contain water, then fill the
kettle
2. Plug the kettle into the power point and switch it on
3. If the teapot is not empty, then empty the teapot
4. Place tea leaves in the teapot
5. If the water in the kettle is not boiling, then go to
step 5
6. Switch off the kettle
7. Pour water from the kettle into the teapot

The algorithms show three features:

Sequence (known as process)


Decision (known as selection)
Repetition (known as iteration or looping)

Features
Sequence
Sequence means that each step or process in the algorithm
is executed in the specific order
Each process must be in the proper place otherwise the
algorithm will fail

The decision constructs-if then, if then else


In algorithms the outcome of a decision is either true or
false; there is no state in between
The outcome of the decision is based on some condition that
can only result in a true or false value
Ex: if today is Friday then collect pay
General form of decision constructs:

(i) if proposition then process


(ii) if proposition
then process1
else process2

Features
The repetition constructs-repeat and while
Repetition can be implemented using constructs like the repeat loop,
while loop. And if then goto loop

Repeat Loop
The repeat loop is used to iterate or repeat a process or sequence of
processes until some condition becomes true.
General Form:

Repeat
Process1
Process2
.
.
Process N
Until proposition
Example:

Repeat
Fill eater in Kettle
Until Kettle is full
The repeat loop does some processing before testing the state of the proposition

Features
While loop
The while loop finds out whether some condition is true
before repeating a process or a sequence of processes
General Form:

while proposition
begin
Process1
Process2
.
.
Process N
End
Example:

while kettle is not full


Fill water in kettle

Features
if then goto
The if then goto is also used to repeat a
process or a sequence of processes until the given
proposition is false
General Form:

Process1
Process2
.
.
Process N
If proposition then goto Process1
Example:

1. Fill some water in kettle


2. if kettle not full then goto 1

Variables

Programs = Algorithms + Data


Data is a symbolic representation of value
Every algorithm contains data and usually the
data is contained in what is called a variable
The variable is container for a value that may
vary during the execution of the program
Example: In tea making algorithm
Water_Level (level of water in the kettle)
Water_Temperature (the temperature of the water)
Tea_Leaves_Quantity (he quantity of tea leaves)

Variables
Variables and data types
The data used in algorithm can be different
types
Simple data types used in algorithm are:
Numeric data, e.g., 12, 11.5, etc.
Alphabetic or character data, e.g., A, z.
Logical data, i.e., propositions with true/false values

Naming of variables:
Choose meaningful names for variables in
algorithms to improve the readability of the
algorithm or program

Pseudo-code and Flowcharts


There are other ways of stating algorithms
Pseudo-code
Flowchart

Step-form
It is a written form that uses normal language

Pseudo-code
pseudo-code is a written forms
In pseudo-code, is in human language but tends towards more
precision by using a limited vocabulary

Flowcharts
Flowchart is graphical representation, they use symbols
and language

Developing Algorithms using Stepform


1.
2.
3.
4.

5.

6.

Each algorithm will be logically enclosed by two statements START


and STOP
To accept data from user, the INPUT or READ statements are to
be used
To display any user message or the content in a variable,
PRINT statement will be used and the message will be enclosed
within quotes
The arithmetic operators that will be used in the expressions are
= Assignment
- subtraction
+ Addition
/ Division
* Multiplication
In propositions, the commonly used relational operators are
> Greater then
<=Less then or equal to
< Less than
= Equality
>= Greater than or equal to
!= Non-equality
The most commonly used logical operators are
AND Conjunction
OR Disjunction
NOT Negation

Algorithm Examples

To find sum of two


numbers

1. STAR
2. PRINT ENTER TWO
NUMBERS
3. INPUT A, B
4. C = A + B
5. PRINT C
6. STOP

To Interchange the
numerical values of two
variables
1. START
2. PRINT ENTER THE
VALUE OF A & B
3. INPUT A, B
4. C = A
5. A = B
6. B = C
7. PRINT A, B
8. END

Algorithm Examples
To compare two numbers
1. START
2. PRINT ENTER TWO NUMBERS
3. INPUT A, B
4. IF A > B THEN
PRINT A IS GREATER THEN B
5. IF B > A THEN
PRINT B IS GREATER THEN A
6. IF B = A THEN
PRINT BOTH ARE EQUAL
7. STOP

Pseudo-code
Pseudo-code is an informal way to express the
design of a computer program or an algorithm
Pseudo-code, is a written statement of an
algorithm using a restricted and well-defined
vocabulary
There is no standard for pseudo-code
It is easy to read and use
It is like a young child putting sentences
together without any grammar
Example
dowhile kettle_empty
Add_Water_To_Kettle
End dowhile

Example
Computing SalesTax
1.getprice of item
2.getsales tax rate
3.sales tax=price of item times sales tax
rate
4final prince=price of item plus sales tax
5.displayfinal price
6.halt

Variables: price of item, sales tax rate, sales


tax, final price

Flowcharts
A flowchart provides appropriate steps to be
followed in order to arrive at the solution to a
problem
It is a program design tool which is used before
writing the actual program
A flowchart comprises a set of various standard
shaped boxes that are interconnected by flow
lines
Flow lines have arrows to indicate the direction of
flow of control between the boxes
The activity to be performed is written within the
boxes in English
There are connector symbols that are used to
indicate that the flow of control continues elsewhere,
for example, the next page

Guidelines for drawing a


flowchart
Flowcharts are drawn using standard symbols
The Most Commonly used Flowchart Symbols are
Terminator,Process,Decision,Document,
andConnector.

Other Flowchart Symbols


Symbol

Name

Description

Branching and Control of Flow Symbols


Connector (Inspection)

It is used as a Connector to show a jump from


one point in the process flow to another.
Connectors are usually labeled with capital
letters (A, B, AA) to show matching jump
points.

Input and Output Symbols


Display

Indicates a process step where information is


displayed to a person (e.g., PC user, machine
operator).

Process / Operation Symbols


Predefined Process

This shape commonly depicts sub-processes

(Subroutine)

(or subroutines in programming flowcharts).

File and Information Storage Symbols


Magnetic Disk (Database)

The most universally recognizable symbol for


a data storage location, this flowchart shape
depicts a database.

Sequential Access Storage

Although it looks like a 'Q', the symbol is

(Magnetic Tape)

supposed to look like a reel of tape.

Guidelines for drawing a


flowchart
All necessary requirements should be listed
out in a logical order
There should be a logical start and stop to
the flowchart
Direction of the flow of a procedure or
system is from left to right or top to bottom
Only one flow line should emerge from a
process symbol

Guidelines for drawing a


flowchart
The writing within standard symbols should be brief
English should be used in the flowcharts, not programming
languages
Only one flow line should enter a decision symbol, but
two or three flow lines, one for each possible answer,
can leave the decision symbol

Only one line is used with conjunction with a terminal


symbol

Guidelines for drawing a


flowchart
The flowchart for each subroutine, if any, must
appear on a separate page. Each subroutine begins
with a terminal symbol with the subroutine name
and a terminal symbol labeled return at the end
Draw arrows between symbols with a straight edge
and use arrowheads to indicate the direction of the
logic flow
If flowchart becomes complex, connector symbols
should be used to reduce the number of flow lines.
The intersection of flow lines should be avoided to
make the flow chart a more effective and better way
of communication
The validity of the flowchart should be tested by
passing simple test data through it

Advantages of using
flowcharts
Communication: Flowcharts are a better way of
communicating the logic of a system to all concerned
Effective analysis: With the help of flowcharts, problems can
be analyzed more effectively
Proper documentation: Program flowcharts serve as a good
program documentation needed for various purposes
Efficient coding: Flowcharts act as a guide or blueprint during
the system analysis and program development phase
Proper debugging: Flowcharts helps in the debugging
process
Efficient program maintenance: The maintenance of an
operating program becomes easy with the help of a flowchart

Limitations of using
flowcharts
Complex logic: Sometimes, the
program logic is quite complicated. In
such a case, a flowchart becomes
complex and clumsy
Alterations and modification: If
alterations are required, the flowchart
may need to be redrawn completely

FLOWCHART EXAMPLES
To print the climatic condition

FLOWCHART EXAMPLES
To find the sum of first
50 natural numbers

FLOWCHART EXAMPLES

THE END

You might also like