• Write algorithm and draw flowchart for the following problem. o Take three numbers as input from the user, calculate and display their sum and average (using loop).
Answers
Answer:
Explanation:
A flow chart is but a pictorial representation of an algorithm and is meant to depict the flow of control in a program.
The first step is to decide on an algorithm and maybe write it in the form of a pseudo code, an informal way of writing statements of a program without bothering about details of syntax.
Algorithms for the given problem:
[I]
Initialize Counter=0, Sum=0
Input integer
Add integer to Sum
Increment Counter
If Counter > 3, divide Sum by 3, Output result, stop
Else go to 2
[II]
Input Integer1, Integer2, Integer3
Average= ( Integer1+ Integer2+Integer3)/3
Output average
The second algorithm would be clumsy to find the average of a large number of inputs.
The first one can be easily generalised to handle n inputs, by introducing step 0 and modifying step 5
0. Input n
5. If Counter > n , divide Sum by n, Output result, stop
Note : There can be more ways of writing an algorithm for the problem by invoking loop control statements.
Having decided on the algorithm one can draw the flow chart following diagrammatic conventions, like using diamond boxes for decision (If), rectangles for assignment statements and conventional symbols [ Flowchart Symbols and Notation] for input and output. The control flow is to be indicated by directed arrows
Answer:
Step 1: Set count = 0, sum = 0
Step 2: While count <5 , repeat steps 3 to 5
Step 3: Input a number to num
Step 4: sum = sum + num
Step 5: count = count + 1
Step 6: Compute average = sum/5
Step 7: Print average
Explanation:
Step 1: Set count = 0, sum = 0
Step 2: While count <5 , repeat steps 3 to 5
Step 3: Input a number to num
Step 4: sum = sum + num
Step 5: count = count + 1
Step 6: Compute average = sum/5
Step 7: Print average