BUILDING BLOCKS OF ALGORITHMS Dr P Ganesh Kumar
BUILDING BLOCKS OF ALGORITHMS Dr. P. Ganesh. Kumar Dept. of Information Technology Anna University Regional Campus, Coimbatore
Introduction • Building blocks – Any components or constituents or ingredients that make up the concept • Algorithm – Statement – State – Control flow – Function
Statement • Should be easily executable code. – Indicative statement • Start and Stop – Sequential statement – Decisive statement – Repetition statement converted to a
Sequential statement • Executed one by one. • Example: – Summation of two numbers • • • Step 1: Start Step 2: Read number 1 in variable ‘a’ Step 3: Read number 2 in variable ‘b’ Step 4: Add ‘a’ and ‘b’ in variable ‘c’ Step 5: Display ‘c’ Step 6: Stop
Decisive statement • Execution based on a conditional decision. • Example – Eligibility to Vote • Step 1: Start • Step 2: Read age in variable ‘a’ • Step 3: check a is greater than 18 – True: » Step 3 a: Display “Eligible” – False: » Step 3 a: Display “Not Eligible” • Step 4: Stop
Repetition statement • Execution of statements repeatedly • Example: – Sum of N natural numbers • Step 1: Start • Step 2: Read number of natural numbers to be summed in variable ‘N’ • Step 3: Initialize the variable ‘sum’ to 0 • Step 4: For each ‘i‘ starts with 0 to N do – sum=sum+i – i=i+1 • Step 5: Stop
State • Denotes the execution status of the algorithm. – Starting • Start – Initialization • Variable declaration and initialization. – Computation • Operation – Termination • Display – Ending • Stop
Control flow • Denotes the execution path of the instructions in an algorithm. • Deals with the kind of instruction flow in an algorithm during problem solving • Linear – Sequential • Non-linear – Conditional • Decisive – Iterative • Repetition
Function • Algorithm on the whole can be said as main function. • However collection of some set of instructions in algorithm intended for a specific purpose can be treated as subfunction or simply function. • Control flow is from main function to sub function and then returned to main function for termination.
Function Algorithm – Main function • Step 1: Start • Step 2: Statement 1 • Step 3: Statement 2 • Step 4: Statement 3 • Step 5: Statement 4 • Step 6: Function Call • Step 7: Statement 6 • Step 8: Statement 7 • Step 9: Stop Sub - Function • • • Step 1: statement 1 Step 2: statement 2 Step 3: statement 3 Step 4: statement 4 Step 5: statement 5
- Slides: 10