0% found this document useful (0 votes)
4 views

Ch2_Algorithm

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

Ch2_Algorithm

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

‫جامعة محمد بوضياف – المسيلة‬

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.

3. Pseudo-code the solution with basic steps that would


help us get a clear intuition of what we are going to do
with the problem statement.

4. Run through test cases to verify the solution.

5. Code the solution.


Algorithm Example :

1. Beat the eggs with a fork


2. Pour the eggs into the pan and put the fire down
3. serve the omelette

Variables Values Algorithm

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

 A flowchart is a step-by-step approach to solve a problem.

 flowcharts is used in order to structure our solution,.


Algorithm representation :

2. Basic flowchart components

2.1. Terminators
Algorithm representation :

2. Basic flowchart components

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. Basic flowchart components

2.3. Process

Used to perform the operation in the program


C A+B
Example: calculate the SUM = A+B
Algorithm representation :

2. Basic flowchart components

2.4. Test or Comparison

Used to test condition ; the result will be true (yes) or false (No)

Yes Example: if A> B


No
A>B
Algorithm representation :

2. Basic Flowchart components

2.5. Direction or arrow

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

If (condition either true or false)


{
//Statement block
}
If Statement Example :
Example :
Find the Greatest of Two Numbers.

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 :

Example : Suppose we have to


check if a number is even or odd.

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 :

Example : To print the numbers less


than or equal to n where n is given
by the user.
Turbo pascal :

Program ... ; { Program heading }


Program head Uses ... ; { Uses clause }
Label ... ; { Labels }
. Const ... ; { Constants }
Déclaration Part Type ... ; { Types }
Var ... ; { Variables}
Procedure ... ; { Procedures }
Function ... ; { Functions }
Begin
statement1; { Statements }
body part statement2; { Statements }
……
statement; { Statements }
End.

Figure 1. General Structure of turbo pascal program

You might also like