Writing the Algorithm, and Drawing the Flowchart
Chapter Three
Syllabus topic Module 1, Practical 1(a), (b) and (c): "Write algorithm & draw flowchart for the same"
Pages 9 to 12 of 206
In one line
An algorithm is the solution written out as numbered steps in plain English. A flowchart is the same solution drawn as boxes joined by arrows. You write both before you write any C, and both go in your journal.
In the wording you can use in a viva: an algorithm is a finite sequence of unambiguous steps which, carried out in order, solves a given problem in a finite time. A flowchart is its diagrammatic representation, drawn with standard symbols connected by flow lines.
Why both, when the program says the same thing
Because they say it to different people, and because writing them first is what stops you writing the wrong program.
The algorithm is language free. It does not know what C is. A student who can write the algorithm for sorting an array can write that program in C this year and in Python next year, and the thinking is done once. The flowchart shows the shape of the solution at a glance: you can see a loop in a flowchart from across the room, and you cannot see it in fifty lines of code.
There is also the plain examination reason. MU asks for them. Three of Practical 1's programs say "Write algorithm and draw flowchart for the same" in her own words, which means the examiner may ask for either in any practical, and your journal is marked on them all semester.
The five properties an algorithm must have
| Property | What it means | What breaks it |
|---|---|---|
| Finiteness | it stops | a loop with no way out |
| Definiteness | every step means exactly one thing | "calculate the interest somehow" |
| Input | zero or more values are given to it | a step that uses a value nobody supplied |
| Output | at least one value comes out | a program that computes and prints nothing |
| Effectiveness | every step can actually be carried out | "guess the answer and check it" |
Definiteness is the one students lose marks on. "Add the numbers" is not a step if there are three numbers and you have not said in what order or into what. "Set sum to a plus b plus c" is a step.
How an algorithm is written
Numbered steps. One instruction each. It begins with Start and ends with Stop, and everything in between is either a value read, a value computed, a value printed, or a decision.
For the simple interest program the whole thing is six steps:
1. Start
2. Read P, R and N
3. Set SI to (P into R into N) divided by 100
4. Print SI
5. Stop
Notice what is not there. No float, no scanf, no semicolons. Those are C's problem, and C comes after.
Writing the Algorithm, and Drawing the Flowchart
Where a step depends on a condition, say so with if and otherwise, and number the branches:
3. If a is greater than b, go to step 4, otherwise go to step 6
Where steps repeat, say what makes them repeat and what makes them stop:
4. While n is greater than 0, repeat steps 5 and 6
5. Set rev to rev into 10 plus the remainder of n divided by 10
6. Set n to n divided by 10
The symbols, and what each one is for
There are six. You will not need a seventh in this paper.
Figure 3.1 The six symbols, and what each one is for
The terminator is the rounded box. A chart has exactly one Start and, in this paper, one Stop. Two Stops is not wrong in general, but a chart with three of them usually means the solution has not been thought through.
Input and output is the parallelogram, leaning right. Anything read from the user, anything printed to the screen. Some colleges teach a separate symbol for a printed document; you will not need it.
The process is the plain rectangle. A calculation, or a value put into a variable. One rectangle can hold two or three closely related assignments, as the loop body in this book's charts does.
The decision is the diamond. One arrow in, exactly two out, and the two must be labelled. Label them Yes and No, and write the question in the diamond with a question mark, so that reading the chart out loud makes sense: "a greater than b? Yes, go left."
The connector is the small circle with a letter in it. Use it when the chart will not fit in one column: put a circle marked A where the flow leaves, and another circle marked A where it arrives. It is a label, not a step.
The flow line is the arrow. It carries the order. Arrows go down and to the right by default, and an arrow that goes up is a loop returning, which is exactly what you want a reader to notice.
Worked example: from problem to chart, in four steps
The problem: read a number and print it with its digits reversed.
Step 1: write down what is given and what is wanted. Given: a whole number n. Wanted: the same digits, printed in the opposite order.
Step 2: solve it by hand once, and watch what you do. Take 4271. You peel the last digit off, 1, and start building the answer. Then 7, and the answer so far becomes 17. Then 2, giving 172. Then 4, giving 1724. You stop when there is nothing left to peel. Two operations keep appearing: the last digit of n, and n with its last digit removed.
Writing the Algorithm, and Drawing the Flowchart
Step 3: write the steps.
1. Start
2. Read n
3. Set rev to 0
4. While n is greater than 0, repeat steps 5 and 6
5. Set rev to rev into 10 plus the remainder of n divided by 10
6. Set n to n divided by 10
7. Print rev
8. Stop
Step 4: draw it. Every read and print becomes a parallelogram, every assignment a rectangle, and the "while" becomes a diamond with the loop body hanging under its Yes arm and an arrow returning from the bottom of the body to just above the diamond.
Figure 3.2 The shape every while loop has: test at the top, body below, arrow returning above the test
That returning arrow is the whole point of the picture. It goes back to a point above the diamond, not into the diamond's side, because the test is performed again before the body runs again. A chart whose arrow returns into the body instead of above the test is drawing a different program, and it is the commonest drawing mistake in a first-semester journal.
The two loop shapes, and how to tell them apart in a chart
| Test at the top | Test at the bottom | |
|---|---|---|
| In C | while | do while |
| In the chart | diamond above the body, arrow returns above the diamond | body first, diamond below it, arrow returns above the body |
| Body runs | zero or more times | one or more times, always at least once |
| Use it when | the loop may not need to run at all | the loop must run once before the question can be asked, as a menu must |
What beginners get wrong
Writing C in the algorithm. scanf("%d", &n); is not a step. "Read n" is. An algorithm with semicolons in it has not done the job the algorithm exists to do.
A decision with one arrow out. A diamond always has two, and both are labelled. A single unlabelled arrow out of a diamond is a decision that decides nothing.
Unlabelled branches. Yes and No must be written on the arrows. A reader cannot guess, and neither can an examiner.
Forgetting Stop. A chart that runs off the bottom of the page is not finished.
Drawing the chart after the program. Then it is a picture of your code, which is not what it is for. Draw it first and the code writes itself.
What goes in your journal
For every practical where MU asks for them, and for any other where you have room: the numbered algorithm in ink, then the flowchart drawn with a ruler. Use a stencil if your college gives you one. The symbols must be the right shapes: a rectangle where a diamond belongs is marked wrong even when the logic is right.
Writing the Algorithm, and Drawing the Flowchart
Quick revision
- Algorithm: numbered steps, plain English, Start and Stop, no C.
- Five properties: finiteness, definiteness, input, output, effectiveness.
- Six symbols: terminator, input and output, process, decision, connector, flow line.
- A decision has exactly two exits and both are labelled Yes and No.
- A loop's returning arrow goes back to above the test, never into the body.
whiletests at the top and may run zero times;do whiletests at the bottom and runs at least once.- Both go in the journal, before the program.
Test yourself
1. Define an algorithm. A finite sequence of unambiguous steps which, carried out in order, solves a given problem in a finite time.
2. Which symbol is used for reading a value from the user, and which for a calculation? A parallelogram for input and output, a rectangle for a process such as a calculation.
3. How many arrows leave a decision box, and what must they carry? Exactly two, and each carries a label, Yes and No.
4. Your flowchart's loop arrow returns into the middle of the loop body. What is wrong? The test is then never performed again, so the chart shows a loop that never ends. The arrow must return above the decision.
5. Give the algorithm for finding the largest of two numbers.
- Start. 2. Read a and b. 3. If a is greater than b, print a, otherwise print b. 4. Stop.
6. Why is an algorithm written before the program and not after? Because it settles the solution in language anybody can check, and because the program is then a translation rather than an invention. It is also what MU asks for in the journal.
The rest of this subject
These notes are cut from the University's printed syllabus. Open the syllabus itself for the same subject.