Vous êtes sur la page 1sur 6

Chapter 5 Algorithms and flowcharts:

Algorithms : The fundamental knowledge necessary to solve problems using a computer is the notion of an algorithm. An algorithm is a precise specification of a sequence of instructions to be carried out in order to solve a given problem. Each instruction tells us what task is to be performed. The example below will make you understand the specifications: Example: Recipe for Mutter paneer Ingredients: kg fresh peas, 400 gms paneer, 2 onoins grated, 2 tomatoes, peeled and chopped 1tsp chilli powder, cup coriander leaves,1/2 turmeric powder,1tsp garlic/ginger paste,4 tsp oil,salt to taste. Method: Step 1: Pressure cook peas in Pressure Pan with 1 cups of water. Drain out excess water, keep peas aside. Step 2: Heat oil in the pressure pan and add the onions. Saute till they turn brown then add tomato pulp. Step 3: Add spices, garlic and ginger paste, saute till the gravy is well blended. Step 4: Add cooked peas,paneer and salt. Step 5: Simmer for 5 minutes. Step 6: Garnish with coriander leaves. Result: Mutter paneer ready to serve for 4 people. The recipe given above is similar to an algorithm but it does not technically qualify as one as the instructions given above are not precise it depends on the individual whether the person has to wait for the gravy to be blended. We will look at another example to examine another sequence of instructions: Instructions to knit Sweater: Input Needles No.12=2 Wool 4 ply = 9 balls Method Step1 : Cast on 133 stitches Step2: Repeat Steps 3 and 4, 11 times

Step3: Knit 2, *Purl 1, Knit 1, Repeat from * to last stitch, Knit 1. Step 4: Knit, *Purl 1, Knit 1, Repeat from * to End ( Similar steps) Result : A Sweater The above example shows: 1. The instructions are much more precise and unambiguous when compared to the recipe for mutter paneer. 2. The number of different types of actions to be carried out are very few. 3. By a proper permutation and combination of this elementary set of actions a virtually infinite number of patterns may be created. Computers are built to carry out a small variety of elementary instructions. A computer may thus be thought of as a servant who would carry out instructions at very high speed obediently and uncritically. There is a need to give the computer extensive, detailed and correct instructions for solving problems. In order to do this there is need for algorithms which have to be precise, concise and unambiguous. Flowcharts: A flowchart depicts pictorially the sequence in which instructions are carried out in an algorithm. Flow charts are used not only as aids in developing algorithms but also to document algorithms. For visual recognition a standard convention is used in drawing flowcharts. In this standard convention (i) Parallelograms are used to represent input and output operations. (ii) Rectangles are used to indicate any processing operation such as storage and arithmetic (iii) Diamond shaped boxes are used to indicate questions asked or conditions tested based on whose answers appropriate exits are taken by a procedure. (iv) Rectangles with rounded ends are used to indicate the beginning or end points of a procedure. (v) A circle is used to join different parts of a flowchart. This is called a connector. (vi) Arrows indicate the direction to be followed in a flowchart. Algorithm and flowchart for finding biggest of three numbers: Finding the biggest of three numbers Step1: Read three numbers A, B, C Step2: Compare A with B Step3: If A is larger compare it with C Step4: If A is larger than C then A is the largest otherwise C is the largest.

Step5: If A is smaller than or equal to B in the first step then B is compared with C. Step6: If B is larger than C then B is the largest number otherwise C is the largest number. Step7: Stop The above algorithm may be expressed much more clearly and concisely using a flowchart. Algorithm to solve the given quadratic equation: Step1: Read three numbers a, b, c Step2: Multiply number b twice Step3: Multiply number 4 with a and c Step4: Subtract the result of step2 from the result of step3 Step5: Find the square root of the result obtained in Step4 Step6: Add the result of step5 with the negative of number b Step7: Multiply the number 2 with a Step 8: Divide the result of step 6 by the result of step 7 Step9: Similarly subtract the result of step5 with the negative of number b Step10: The result is obtained for the quadratic equations for values x1 and x2. Step11: Stop. Flowchart depicting the solution for the quadratic equation.? Algorithm to find the biggest and smallest of given set of numbers Step1: Read Number Step2: Assign the number to the value Largest Assign the number to the value Smallest Step3: Repeat steps 4 and 5 as long as numbers are there. Step4: Read number Step5: If the number is greater than largest then the number is largest. If the number is lesser than smallest then the number is smallest. Step6: Write Largest number Write Smallest number Step 7: Stop Flowchart to depict the biggest and smallest of given set of numbers. - B

Algorithm to count the number of non-zero data Step1: Initialize the non-zero data counter nzdcount to 0 Step2: Repeat 100 times all steps up to Step4 Step3: Read d Step4: If d is not equal to 0 then nzdcount =nzdcount+1 Step5: Print nzdcount Step6: Stop Flowchart depicting the number of non-zero data - C

Summary Algorithms : The fundamental knowledge necessary to solve problems using a computer is the notion of an algorithm . An algorithm is precise specification of a sequence of instructions to be carried out to solve a given problem. Flowcharts: Pictorial representation of the task to be carried out. A flowchart depicts pictorially the sequence in which instructions are carried out in an algorithm.

Flow charts are used not only as aids in developing algorithms but also to document algorithms.

Vous aimerez peut-être aussi