Lect 3 4
Lect 3 4
algorithms(hal-115)
Lecture 3,4
Algorithms
Algorithms (Definition)
An Algorithm (pronounced AL-go-rith-um)
Algorithm -- Examples
A cooking recipe.
The rules of how to play a game.
VCR instructions.
Directions for driving from A to B.
Computer Algorithms
A computer program is another example of an
algorithm.
To make a computer do anything, you have to
write a computer program. To write a computer
program you have to tell the computer step by
step, exactly what you want to do.
The computer then executes the program
following each step to accomplish the end goal.
When you telling the computer what to do, you
also get to choose how its going to do it. Thats
where computer algorithms come in.
Prepared by: Miss Humera Gull, Dept of
Information System,KKU
Algorithm: A sequence
of instructions describing
how to do a task (or
process)
C Program
Examples:
Suppose Add two number a & b and store
in c.
Steps:
Implement of Algorithm in C
Void main()
{
int a,b,c; //declaration of variables
a=1,b=1; //initialize variable a and b
c=a+b; // add a and b and store result in c
printf(%d,&c); // print output
}
Example:- Algorithm:
Drive_To_Uni
Steps
...etc...etc...etc...
{
1. find car keys
52. find parking space
2. disable car alarm
53. pull into parking
3. open car door
space
4. get in car
54. turn off engine
5. shut car door
55. remove keys from
6. put keys in ignition
ignition
7. start car
56. open car door
8. back car out of
driveway
57. get out
9. drive to end of street
58. shut car door
10. turn right
59. lock car door
11. drive to end of street
60. enable alarm
12. turn left
...etc...etc...etc
} Gull, Dept of
Prepared by: Miss Humera
Information System,KKU
Controls Structures
A control structure or logic structure is a structure that
I.
10
2. Selection Logic
Selection logic employs a number of conditions
which leads to a selection of one out of several
alternatives conditions.
The selection control structure also known as IFTHEN-ELSE structure.
Its offers two paths to follow when a decision must
be made by a program.
The selection control structure fall into 3 types:
Prepared by: Miss Humera Gull, Dept of
Information System,KKU
11
12
Double Alterative:
This structure has the form
IF condition, then
[Module A]
Else
[Module B]
End of IF Structure
------------------------------
Algorithm:
Step1: start
Step2: if a=1 then goto step3
Step3: Print a=1
Else
Step4: print a!=1
Prepared by: Miss Humera Gull, Dept of
Information System,KKU
Step5: End/Exit.
13
Multiple Alterative:
This structure has
Algorithm:
the form.
IF condition1, then
[Module A] Else
IF condition2, then
[Module B] Else
IF condition3, then
[Module C]
Else
[Module D]
Step1: start
Step2: if a=1 then goto step3
Step3: print a=1.
else
Step4: if a=2 then goto step5
Step5: print a=2.
Else
Step6: if a=3 then goto step7
Step7: print a=3.
Else
Step8: print a!=1,2,3
Step9: End/Exit.
Prepared by: Miss Humera Gull, Dept of
Information System,KKU
14
For Loop
While Loop
For loop is used to repeat a process until a
fix number of times
While loop is used to repeat a process or
instructions until our condition is true.
15
start
declare variable x as interger
assign value 1 to x variable.
if x<=5 then goto step5
print value of x.
add 1 to x (x++)
goto step4
End
Prepared by: Miss Humera Gull, Dept of
Information System,KKU
16
Output:
Variable
declaration
Assignment
of a value.
for(x=1;x<=5;x++)
{
printf(\n x=%d,x);
}
}
Format string
check
condition
(value of x)
Prepared by: Miss Humera Gull, Dept of
Information System,KKU
x=1
x=2
x=3
x=4
x=5
Increament
statement ( add 1
to x)
17
OUTPUT:
Data Structure
Data Structure
while(x!=1)
{
printf(\nData Structure);
x--;
}
same as
x = x- 1;
18
Complexity of an Algorithm:
The analysis of algorithm is a major task in computer
19
Best Case
Worst Case
Average Case
20
21
FLOWCHART
22
FLOWCHART
A program flowchart is a graphical representation of
of a building.
23
Meaning of a flowchart:
A flowchart is a diagrammatic representation that
24
25
Symbols of FlowChart
Terminator: An oval
26
Symbols of Flowchart:
Decision: A diamond shape
27
Example:
Program:
Start
Void main()
{
int a=1;
Int a=1
NO
if(a=1)
{
printf(a is equal to 1);
}
}
If a=1
YES
Print a=1
End
28