Ch2_Algorithm
Ch2_Algorithm
Computer Science
Chapter 2
Algorithm
Algorithm
Lesson Outlines:
Students should be understand :
Algorithm and program definitions .
State Algorithm Representation (textual , flow chart)
Algorithm definition :
Word derived from the name of the mathematician Al_Khwarizmi who lived in the 9th century,
was a member of an academy of sciences in Baghdad.
• An algorithm takes data as input, expresses a particular processing and provides data as
output. Outputs
Inputs Algorithm Results
processing
An algorithm does not depend on the language in which it is implemented. An algorithm can be
represented in lexical form or in flowchart form
steps to be followed to solve an
algorithmic problem :
1. Analysing the problem statement and making the objective of the program
clear in our minds like what is the input and what is the required output.
2. Break-down the complex problem into smaller parts to make them easier to understand.
Results
Program definition :
Program: A series of instructions that can be executed in sequence, or in parallel (hardware parallelism)
that performs a task and implements an algorithm.
Instruction 1
Instruction 2
Instruction 3
Instruction 4
Instruction 5
Instruction ..
.
.
Instruction N
Algorithm representation :
1. Textual representation
2. flowchart representation
Algorithm representation :
1. Textual representation
Algorithm Algorithm_Name
{This part is reserved for data declaration}
Constant MAX : integer - 10
Variables val, Number: integer
Name, familly_name : string
Begin
{ this part is reserved for data processing }
val = max + number
Display (name , val)
End
Algorithm representation :
2. flowchart representation
A flowchart is a diagrammatic representation of an algorithm
2.1. Terminators
Algorithm representation :
2.2. Input/Output
Read VAR Used for taking input from the user and store it in variable
‘VAR’
Print VAR
Used to output value stored in variable ‘VAR’
Algorithm representation :
2.3. Process
Used to test condition ; the result will be true (yes) or false (No)
Used to show the flow of the program from one step to another
2.6. Connector
Used to connect different part of the program and are use in case of
break- through.
Algorithm example :
Example 1:
Suppose we have to make a flowchart for
adding 2 numbers A and B.
if statement
Step 1: Start.
Step 2: Take two inputs (a and b) from the user.
Step 3: If a is greater than b then go to step 4 otherwise go to step 5
Step 4: Print a greater than b
Step 5: Print b greater than a
Step 6: Stop.
if-else statement
If (condition)
{Statement block }
Else
{Statement block }
if-else statement Example :
Step 1: Start.
Step 2: Take input from the user.
Step 3: Check condition. If remainder is zero
go to step 4 else go to step 5
Step 4: Print a is even and go to step 6
Step 5: Print a is odd
Step 6: Stop
Algorithm : Case statement
switch(variable)
{
case n1: //Statement block; break;
case n2: //Statement block; break;
...
case n: //Statement block;
break;
}
Algorithm example :