D1 1.1 - Following an algorithm

D1 1.1 - Following an algorithm

Follow instructions written as a flowchart or a numbered procedure, recording how each value changes. You will also learn the middle-item convention used throughout searching and sorting, where choosing the wrong middle item changes the whole trace.

Inputs operations decisions and outputs

If you start with 33, add 22, then double, do you get the same result as doubling first and adding 22? The first route gives 1010; the second gives 88. An algorithm depends on the order of its instructions.

Algorithm

An algorithm is a finite sequence of precise instructions for solving a problem. Its input is the information supplied; its output is the result produced after the instructions have been followed.

A variable stores a value which can change. The instruction x←x+2x\leftarrow x+2 means “replace the current value of xx by that value plus 22”. It is an assignment, not an equation to solve. At x=3x=3, the new stored value is 55; a following instruction uses 55.

An operation changes or calculates a value. A decision selects a route by testing a condition, such as x>4x>4. Exactly one of its Yes and No branches is followed. An output instruction reports a value; it does not change it unless a separate instruction says so.

For input x=3x=3, the instructions “set y=2xy=2x; if y>5y>5, output y−1y-1; otherwise output y+1y+1” first store y=6y=6, then take the Yes branch and output 55. The unused branch is not another calculation to carry out.

Key idea

Read one instruction at a time, using the most recently stored values. A condition is tested when you reach it, not using the original input throughout.

The next check separates the stored value from the value reported.

Tracing flowcharts

A flowchart makes the route through an algorithm visible, including instructions which are repeated.

An oval marks the start or stop. A parallelogram is an input or output; a rectangle is an operation; a diamond is a decision. Follow the arrow leaving the shape. When an arrow returns to an earlier decision, test it again with the updated values. This repeated section is a loop.

[DIAGRAM: asset_name: A repeated addition flowchart; asset_slug: repeated-addition-flowchart; caption: Add the current value of n, reduce n by two, and test the updated value.; description: Start leads to input positive integer n, then s equals zero, then a decision n greater than zero. The Yes branch adds n to s and then subtracts two from n before returning to the decision. The No branch outputs s and stops.]
Add the current value of n, reduce n by two, and test the updated value.

The flowchart adds every other positive integer downwards from the input. A trace table records the stored values at a consistent point: here, immediately before each visit to the decision.

Worked example 1: an even input

Trace the flowchart for n=6n=6.

Plan. Record nn and ss before each test; apply both operations only after a Yes result.

nnssIs n>0n>0?Action
6600YesAdd 66, then reduce nn
4466YesAdd 44, then reduce nn
221010YesAdd 22, then reduce nn
001212NoOutput 1212

Answer. The output is 1212.

Check. The positive values added are 6,4,26,4,2, and 6+4+2=126+4+2=12. The final 00 fails the test and is not added.

Change only the input from 66 to 77. The loop now visits odd values and finishes at −1-1.

Worked example 2: an odd input

Trace the same flowchart for n=7n=7.

Plan. Use exactly the same decision and updates; do not stop merely because the values become small.

nnssIs n>0n>0?
7700Yes
5577Yes
331212Yes
111515Yes
−1-11616No

Answer. The output is 1616.

Check. 7+5+3+1=167+5+3+1=16. Subtracting 22 from the last positive value gives −1-1, which correctly takes the No branch.

The order of the two updates matters: the flowchart adds the old nn before reducing it.

Watch out

Do not write s←s+(n−2)s\leftarrow s+(n-2) in place of the two given operations. That would miss the first term and add a different sequence.

Use the same trace layout with a fresh input.

Continue this lesson.

You’re previewing NovaLearn without an account. Create a free account to read the rest.

Create free account