levo

Software Development

Writing and following algorithms

  • 1 thing to know
  • Component 01
Start revising · £5/month

Overview

Recall questions on writing algorithms before code, pseudocode and flowcharts, trace tables, following short algorithms, divide-by-zero and off-by-one faults, syntax, logic and runtime errors, validation and verification, test data and subroutines.

What you need to know

Answer each one before you reveal it.

Your recallNot started
Process

Why is an algorithm written down before any code, and how can it be checked before it is coded?

Questions from this subtopic

Each one with the model answer underneath.

  1. 1

    Why is writing the algorithm down before writing any code worth the time it takes?

    Model answer

    Because the algorithm is where the thinking happens and code is only its expression, so a fault in the algorithm survives however carefully the code is written. Setting the steps out in pseudocode or a flowchart lets the logic be checked by dry running it on paper, discussed with someone who does not know the language, and corrected while the cost of correcting it is a rubbed-out line rather than a rewritten module. It also separates two kinds of error that are much harder to untangle together: a program that does the wrong thing correctly and a program that does the right thing incorrectly. And because the algorithm is language-independent, the same design can be implemented in whatever language the project settles on.

  2. 2

    Why is an algorithm written before code, and why can it be written in pseudocode or as a flowchart rather than in a real language?

    Model answer

    Writing the algorithm first separates the question of what steps solve the problem from the question of how to express them in one particular language, so mistakes in the logic are found before any code is committed. Pseudocode and flowcharts are language-independent and readable by anyone on the team, so they can be reviewed and discussed, and the same algorithm can then be implemented in whichever language the project uses.

  3. 3

    What is a trace table, and why is it used?

    Model answer

    A table with a column for each variable, in which each row records the values after one step or one pass of the algorithm, together with any output produced. It is used to dry run an algorithm by hand, so that its behaviour can be checked without a computer and the exact point at which the values go wrong can be located.

  4. 4

    Follow this algorithm and give the output. Set total to 0, set n to 5. While n is greater than 0, add n to total and subtract 2 from n. Finally output total.

    Model answer

    The loop runs with n = 5, 3 and 1, since after that n is -1 and the condition fails. Total becomes 5, then 8, then 9. The output is 9. Tracing: after pass 1 total 5, n 3; after pass 2 total 8, n 1; after pass 3 total 9, n -1; the condition n > 0 is false, so the loop ends.

  5. 5

    Follow this algorithm on the list 4, 9, 2, 7. Set best to the first item. For each remaining item, if the item is less than best then set best to that item. Output best. What does the algorithm do, and what is the output?

    Model answer

    It finds the smallest value in the list. Best starts at 4; 9 is not less than 4 so best stays 4; 2 is less than 4 so best becomes 2; 7 is not less than 2 so best stays 2. The output is 2.

  6. 6

    An algorithm is meant to output the average of the numbers in a list. It sets total to 0, adds each number to total, and then outputs total divided by the number of items. What must be checked before the division, and why?

    Model answer

    That the list is not empty. If it holds no items, the count is zero and the division causes a divide-by-zero error at run time, crashing the program. The algorithm should test the count first and either report that no average exists or return a defined value. This is a logic error the compiler cannot catch, because the fault only appears for a particular input.

Other ways to work this

The same content, four other ways.

Everything in your subscription

Runs, plus a mode for every other kind of session.

1Flash
2Imposter
3Fade
4Brain dump

Learn every subtopic in runs of four questions

Facts flash and fade, a wrong word hides in some of them, and a brain dump decides which questions are locked in.

Processors: Structure, Performance and Types
Input, Output and Storage
Systems Software
Applications Generation

Cram the night before, weakest topic first

A quick scan finds your weakest topics. Recall each fact you missed three times in a row.

If it matches, the search .
succeedsfailsrestarts

Blank Blitz consolidates the facts

Rapid timed gap-fills on the terms and numbers you need to recall without thinking.

✓What it iscovered
~Why it happenspartly there
How long it lastsnot there yet

Explain It tests your understanding

Explain a concept in your own words and see which of its points you covered.

A global variable means a variable reachable from anywhere in the program.
TrueFalse

Test It catches misconceptions

Spot the fault in a statement. The faults are the mistakes students make most often.

721941163128510

Brain Breaks between sessions

Short focus games to reset attention, so the next set lands better than the last.

Every question in Software Development, on a schedule.

Learn it in runs of four questions, each ending on a brain dump against the clock. Every question you lock in comes back the next day, then at longer gaps, each time just before you would forget it.

Or £46 a year, about £3.83 a month. Cancel anytime.