ALGORITHMS AND FLOWCHARTS

ALGORITHMS AND FLOWCHARTS   A typical programming task can be divided into two phases: Problem solving phase  produce an ordered sequence of steps that describe solution of problem  this sequence of steps is called an algorithm  Implementation phase  implement the program in some programming language .

Algorithm Algorithm is a procedure for performing some calculation.  An effective way for solving a problem in finite number of steps.  .  Algorithm gives the logic of a program that is a step by step description of how to arrive at a solution.

.   It should have the capability to handle some unexpected situations which may arise during the solution of a problem(for eg: Division with zero). It should involve finite number of steps. It should be clear with no ambiguity.   It should lead unique solution of the problem.Key Features of Algorithm  It should be simple.

Algorithm  Example 1: Write an algorithm to determine a student’s final grade and indicate whether it is passing or failing. The final grade is calculated as the average of four marks. .

Algorithm Algorithm:  Input a set of 4 marks  Calculate their average by summing and dividing by 4  if average is below 50 Print “FAIL” else Print “PASS” .

Example 2  Write an algorithm to convert the length in feet to centimeter. Input the length in feet (Lft)  Calculate the length in cm (Lcm) by multiplying LFT with 30  Print length in cm (LCM)  .

Example 3 Write an algorithm that will read the two sides of a rectangle and calculate its area.    Input the width (W) and Length (L) of a rectangle Calculate the area (A) by multiplying L with W Print A .

Example 4  Write an algorithm and draw a flowchart that will calculate the roots of a quadratic equation ax 2  bx  c  0  b2  4ac ). and the roots are: Hint: d = sqrt ( x1 = (–b + d)/2a and x2 = (–b – d)/2a .

Example 4 Algorithm:  Input the coefficients (a. c) of the quadratic equation  Calculate d  Calculate x1  Calculate x2  Print x1 and x2 . b.

49. 87. 98. 62.Example 5. Design an algorithm and the corresponding flowchart for adding the test  scores as given below:  26. 75  .

Output the sum  7. Get a value  4. sum = sum + value  5. Stop . Start  2. Sum = 0  3. Go to step 3 to get next Value  6. 1.

Sign up to vote on this title
UsefulNot useful