Lect 3-4 Zaheer Abbas

  • View
    2.054

  • Download
    6

Embed Size (px)

DESCRIPTION

 

Text of Lect 3-4 Zaheer Abbas

  • Created By Zaheer Abbas Aghani
  • Data Structure (ITC-322) Lecture 2 Algorithms
  • Algorithms (Definition)
    • An Algorithm (pronounced AL-go-rith-um) is a se quence of instructions specifying the steps required to accomplish some task.
    • OR
    • 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.
    • VCR instructions.
    • Directions for driving from A to B.
    • A car repair manual.
  • 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.
  • From Algorithms to Programs Problem C Program Algorithm : A sequence of instructions describing how to do a task (or process)
  • Examples:
    • Suppose Add two number a & b and store in c.
    • Steps:
      • 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
    • 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
    • {
    • 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
    ...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 }
  • Controls Structures
    • A control structure or logic structure is a structure that controls the logical sequences in which programs instructions are executed.
    • OR
    • 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.
    • OR
    • 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.
    • -------------------------------------
    • Algorithm:
    • step1: start
    • step2: if today is wed then goto step3.
    • Step3: print today is wed
    • Step4: End/Exit
  • 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
    • Step5: End/Exit.
  • Multiple Alterative:
    • This structure has the form.
    • IF condition1, then
    • [Module A] Else
    • IF condition2, then
    • [Module B] Else
    • IF condition3, then
    • [Module C]
    • Else
    • [Module D]
    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
      • 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.
  • Iteration logic Example:
    • Algorithm:
    • Step1: start
    • Step2: declare variable x as interger
    • for{
    • Step3: assign value 1 to x variable.
    • Step4: if x