OCR A-Level Computer Science H446
Processors: Structure, Performance and Types
Input, Output and Storage
Systems Software
Applications Generation
Software Development
Types of Programming Language
Exchanging Data
Networks
Web Technologies
Data Types and Number Representation
Data Structures
Boolean Algebra
Legal, Moral, Cultural and Ethical Issues
Elements of Computational Thinking
Problem Solving and Programming
Algorithms
Software Development
Writing and following algorithms
- 1 thing to know
- Component 01
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.
Questions from this subtopic
Each one with the model answer underneath.
- 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
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
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
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
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
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.
What is inside Levo
Six ways to work the same material.
Every question in Software Development, on a schedule.
Attempt a question from memory, write the correct version, then attempt it again. Levo records the jump between your first attempt and your last, and brings each question back on the day you are due to forget it.
Or £46 a year, about £3.83 a month. Cancel anytime.