An Algorithm (pronounced AL-go-rith-um) is a se quence of instructions specifying the steps required to accomplish some task.
An algorithm is a finite step by step list of well defined instruction for solving a particular problem.
Algorithm -- Examples
A cooking recipe.
The rules of how to play a game.
Directions for driving from A to B.
A car repair manual.
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.
From Algorithms to Programs Problem C Program Algorithm : A sequence of instructions describing how to do a task (or process)
Suppose Add two number a & b and store in c.
Step1: Initialize variable a and b [a=1, b=1]
Step2: Declare variable c.
Steps3: Add variables and store in other variable [ c= a+b].
Steps4:Print output [c].
Implement of Algorithm in C
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
1. find car keys
2. disable car alarm
3. open car door
4. get in car
5. shut car door
6. put keys in ignition
7. start car
8. back car out of driveway
9. drive to end of street
10. turn right
11. drive to end of street
12. turn left
...etc...etc...etc... 52. find parking space 53. pull into parking space 54. turn off engine 55. remove keys from ignition 56. open car door 57. get out 58. shut car door 59. lock car door 60. enable alarm }
A control structure or logic structure is a structure that controls the logical sequences in which programs instructions are executed.
Control flow or Control structures refers to the order in which the individual statements, instructions, are executed.
Algorithms and their equivalent computer programs are more easily understood if they mainly use these types of control structures.
An algorithm or computer program is usually not limited to a linear sequence of instructions. During its process it may bifurcate, repeat code or take decisions.
In algorithm design structure, three types of control structure are used to form the logic of program.
Control Structures Cont.
Sequential Logic or sequential Flow.
Selection Logic or Conditional Flow.
Iteration Logic or Repetitive Flow.
Sequential Logic : In sequence control structure, one program statement following another logical order.
Steps are executed one after one
i.e Step1 Step2 Step3 Step4 So on
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 IF-THEN-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:
Selection control structure types:
Single Alterative : (One Condition):
This structure has the form:
IF condition, then
[Module A ]
End of IF structure.
step2: if today is wed then goto step3.
Step3: print today is wed
This structure has the form
IF condition, then
End of IF Structure
Step2: if a=1 then goto step3
Step3: Print a=1
Step4: print a!=1
This structure has the form.
IF condition1, then
[Module A] Else
IF condition2, then
[Module B] Else
IF condition3, then
Algorithm: 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.
3. Iteration Logic OR Repetitive Flow
In this iteration or loop control structure a process may be repeat as long as the condition remain true.
In this control structure, we use either two types of structure involving loops.
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.