Algorithms and Flowcharts
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
language
Algorithm
Algorithm is a procedure for performing some calculation. Algorithm gives the logic of a program that is a step by step description of how to arrive at a solution. An effective way for solving a problem in finite number of steps.
It should be simple.
It should be clear with no ambiguity.
It should have the capability to handle some unexpected situations which may arise during the solution of a problem(for eg: Division with zero).
Algorithm
Example 1: Write an algorithm 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.
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
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
Example 4
Algorithm: Input the coefficients (a, b, c) of the quadratic equation Calculate d Calculate x1 Calculate x2 Print x1 and x2
Example 5.
Design an algorithm and the corresponding flowchart for adding the test scores as given below: 26, 49, 98, 87, 62, 75
1.
Start 2. Sum = 0 3. Get a value 4. sum = sum + value 5. Go to step 3 to get next Value 6. Output the sum 7. Stop