introdution to algorithms

Upload: ankit-khambhatta

Post on 03-Apr-2018

226 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/29/2019 Introdution to Algorithms

    1/24

    What is Algorithm?

    It is a sequence of instructions to be carried

    out in order to solve a specific problem. In

    designing a solution, we write step by step

    procedures for solving the problem. Such

    process is called algorithm. Algorithm can be

    written in any language according to the

    convenience of the program developer.

  • 7/29/2019 Introdution to Algorithms

    2/24

    To add two numbers.

    Read the Value of A and B.

    SUM = A+B.

    Display SUM. Stop.

  • 7/29/2019 Introdution to Algorithms

    3/24

    To Multiply two numbers.

    Read the Value of A and B.

    PRODUCT = A*B.

    Display PRODUCT. Stop.

  • 7/29/2019 Introdution to Algorithms

    4/24

    1. Start

    2. sum = 0

    3. Get the first number

    4. Add first number to sum (sum= sum+ first number)

    5. Get the second number

    6. Add to sum 7. Get the third number

    8. Add to sum

    9. Get the Forth number

    10. Add to sum

    11. Get the fifth number

    12. Add to sum 13. Get the sixth number

    14. Add to sum

    15. Output the sum

  • 7/29/2019 Introdution to Algorithms

    5/24

    Example : Write an algorithm to determine astudents final grade and indicate whether it ispassing or failing. The final grade is calculated asthe average of four marks.

    Start Step 1: Input M1,M2,M3,M4

    Step 2: GRADE (M1+M2+M3+M4)/4Step 3: if (GRADE < 50) then Print FAIL

    Step 4: if (GRADE < 50) then Print PASS

    Stop

  • 7/29/2019 Introdution to Algorithms

    6/24

    Write an algorithm and draw a flowchart to

    find Greatest of three numbers.

    AlgorithmStartStep1: Read A, B, C

    Step2: If A > B is True, then check whether A > C,if yes then Print A is greatest otherwise C isgreatest.Step3: If A > B is False, then check whether B > C,

    if yes then Print B is greatest otherwise C isgreatest.

    Stop.

  • 7/29/2019 Introdution to Algorithms

    7/24

    characteristics of algorithm

    1. Finiteness : An algorithm must terminate after a finite number of steps andfurther each steps must be executable in finite amount of time.

    2. Each step of an algorithm must be precisely defined; the action to becarried out must be rigorously and unambiguously specified for each

    case.

    3. Input: An algorithm has zero or more, but only finite number of inputs.zero input example: ASCII chart of 0-255

    4. Output: An algorithm has one or more output.

    5. Effectiveness: Should be effective that means each of the operation to beperformed in an algorithm must be sufficiently basic that it can, inprinciple, be done exactly and in a finite length of time, by a person usingpencil and paper and should be computer programming languageindependent

  • 7/29/2019 Introdution to Algorithms

    8/24

    The Flowchart

    A Flowchart

    shows logic of an algorithm

    emphasizes individual steps and their

    interconnections

    e.g. control flow from one action to the next

  • 7/29/2019 Introdution to Algorithms

    9/24

    INTRODUCTION FLOWCHART

    The flowchart is a means of visually presenting

    the flow of data, the operations performed and the

    sequence in which they are performed to solve a

    problem.

    A flowchart is a diagrammatic representation that

    represents the sequence of operations to be

    performed to get the solution of a problem.Flowcharts are generally drawn in the early stages

    of formulating computer solutions.

  • 7/29/2019 Introdution to Algorithms

    10/24

    INTRODUCTION FLOWCHART

    These flowcharts play a vital role in the

    programming of a problem and are quite

    helpful in understanding the logic of

    complicated and lengthy problems. Once the

    flowchart is drawn, it becomes easy to write

    the program in any high level language.

    flowcharts are helpful in explaining theprogram to others.

  • 7/29/2019 Introdution to Algorithms

    11/24

    Flowcharts are usually drawn using

    some standard symbols;

    1. Start or end of the program

    2. Computational steps or

    processing function of a

    program

    3. Input or output operation

    4. Decision making and

    branching5. Connector or joining of two

    parts of program

  • 7/29/2019 Introdution to Algorithms

    12/24

    Flowcharts symbols

    6.Magnetic Tape

    7.Magnetic Disk8.Offpageconnector

    9.Flow line

  • 7/29/2019 Introdution to Algorithms

    13/24

    Flowchart Symbols

    Basic

    Oval

    Parallelogram

    Rectangle

    Diamond

    Hybrid

    Name Symbol Use in Flowchart

    Denotes the beginning or end of the program

    Denotes an input operation

    Denotes an output operation

    Denotes a decision (or branch) to be made.The program should continue along one of

    two routes. (e.g. IF/THEN/ELSE)

    Denotes a process to be carried out

    e.g. addition, subtraction, division etc.

    Flow line Denotes the direction of logic flow in the program

  • 7/29/2019 Introdution to Algorithms

    14/24

    Example

    Draw a flowchart to find the sum of first 50natural numbers.

    Algorithm

    Start. Initialize n and sum to zero(n=0;sum=0).

    Add 1 to n(n=n+1).

    Add n to sum(sum=sum+n).

    Is n=50? If no go to step 3.

    Print sum.

    Stop.

  • 7/29/2019 Introdution to Algorithms

    15/24

  • 7/29/2019 Introdution to Algorithms

    16/24

    Advantages of Using Flowchart

    Communication: Flowcharts are better way ofcommunicating the logic of a system to all concerned.

    Effective analysis: With the help of flowchart, problemcan be analyzed in more effective way.

    Proper documentation: Program flowcharts serve as agood program documentation, which is needed forvarious purposes.

    Efficient Coding: The flowcharts act as a guide orblueprint during the systems analysis and programdevelopment phase.

    Proper Debugging: The flowchart helps in debuggingprocess.

  • 7/29/2019 Introdution to Algorithms

    17/24

    LIMITATIONS OF USING FLOWCHARTS

    Complex logic: Sometimes, the program logic

    is quite complicated. In that case, flowchart

    becomes complex and clumsy.

    Alterations and Modifications: If alterations

    are required the flowchart may require re-

    drawing completely.

    The essentials of what is done can easily be

    lost in the technical details of how it is done.

  • 7/29/2019 Introdution to Algorithms

    18/24

    Example :Draw a flowchart to determine a students final

    grade and indicate whether it is passing or failing. The final

    grade is calculated as the average of four marks.

    PRINT

    PASS

    Step 1: Input M1,M2,M3,M4

    Step 2: GRADE (M1+M2+M3+M4)/4

    Step 3: if (GRADE

  • 7/29/2019 Introdution to Algorithms

    19/24

    Example 3

    Write an algorithm and draw a flowchart that will

    read the two sides of a rectangle and calculate its

    area.

  • 7/29/2019 Introdution to Algorithms

    20/24

    Example 3

    Algorithm

    Step 1: Input W,L

    Step 2: A L x W

    Step 3: Print A

    START

    Input

    W, L

    A L x W

    PrintA

    STOP

  • 7/29/2019 Introdution to Algorithms

    21/24

    Example 4

    Write an algorithm and draw a flowchart to findGreatest of three numbers.

    Algorithm

    Start

    Step1: Read A, B, CStep2: If A > B is True, then check whether A > C,if yes then Print A is greatest otherwise C isgreatest.

    Step3: If A > B is False, then check whether B > C,if yes then Print B is greatest otherwise C isgreatest.

    Stop.

  • 7/29/2019 Introdution to Algorithms

    22/24

  • 7/29/2019 Introdution to Algorithms

    23/24

  • 7/29/2019 Introdution to Algorithms

    24/24

    To find volume and surface area of a

    cylinder