8.3: Writing Algorithms

Exam Board:

Eduqas / WJEC

Specification:

2020 + 

Pseudocode Reminder

Generally, pseudocode can be written in any way that is readable and clearly shows its purpose. However, the Eduqas exam board advises that pseudocode for the programming exam should follow the conventions below:

Annotation

{Write your comment in curly brackets}

Define data type

price is integer

firstname is string

Declare a variable's value

set price = 100

set firstname = "Marcella"

Input / output

output "Please enter your first name"

input firstname

Selection (must have indentation)

if firstname = "Steven" then​

       output "Hello" + firstname

elif firstname = "Steve" then

      output "Please use full name"

else output "Who are you?"

end if

Iteration (while loop)

while firstname != "Steven"

      output "Guess my name."

      input firstname

repeat

Iteration (for loop)

for i in range 10

      input item

next i

Define a subroutine

Declare Sub1

     [Subroutine content indented]

End Sub1

Call a subroutine

call Sub1

Writing Algorithms

In an exam you may be asked to write an algorithm using pseudocode.

 

Previous exams have offered up to 10 marks for a single algorithm.

 

While this may seem daunting, it means you can still gain marks for an incomplete program, so don't leave it blank no matter what!

You must decompose the problem and break it down into more manageable chunks.

 

Here's an example question:

“A teacher is marking tests. Write an algorithm that allows the teacher to input the number of tests to mark and then the mark of each test. Output the average mark, highest mark and lowest mark. The tests are marked out of 100.”

This specific algorithm can be broken down into pre-code and three main parts:

  • Part 0: Declare and assign variables.

  • Part 1: Input the number of tests to mark.

  • Part 2: Input the mark of each test.

  • Part 3: Output the average, lowest and highest marks.

Part 0: Variables

Read the question carefully and work out the variables you will need in your algorithm. I have highlighted them in blue below:

“A teacher is marking tests. Write an algorithm that allows the teacher to input the number of tests to mark and then the mark of each test. Output the average mark, highest mark and lowest mark. The tests are marked out of 100.”

There is an additional variable to track as the average mark can only be worked out if we also know the total marks.

number_of_tests is integer

test_mark is integer

average_mark is real

highest_mark is integer

lowest_mark is integer

total is integer

number_of_tests = 0

test_mark = 0

average_mark = 0

highest_mark = -1

lowest_mark = 101

total = 0

Before you write the actual program, you must declare the variables you will need and assign values to them.

Firstly, declare the data type of each variable. A whole number is an integer and a decimal number is a real. The average must be a real data type because it is the result of division (total ÷ number_of_tests) and could be a decimal number.

 

When assigning values, most numerical variables will be 0. Most string values would be " ".

However this question is a bit more complicated - the highest mark must start as a really low value and the lowest mark must start as a really high value. This is ensure the first mark entered becomes the highest and lowest mark - this will make sense later.

Part 1: Input Number of Tests

output “Enter the number of tests to mark: ”

input number_of_tests

After declaring and assigning your variables the next parts will depend on the algorithm you need to write. This example requires the user to input the number of tests.

Part 2: Input Each Mark (Loop)

for i = 1 to number_of_tests

     output “Enter the test mark: ”

     input test_ mark

For part 2 we need the teacher to enter each test’s mark.

 

This is best done as a loop as we do not know how many tests the teacher has to mark until they have typed it in (part 1). All code within the loop must be indented.

     if test_mark > highest_mark then

          highest_mark = test_mark

     endif

     if test_mark < lowest_mark then

          lowest_mark = test_mark

     endif

We also need to work out what the highest and lowest marks are. This must be done within the loop as the test marks are entered. 


The test mark is compared to the current highest and lowest marks.

 

  • If it is higher than the current highest mark it becomes the new highest mark.

  • If it is lower than the current lowest mark it becomes the new lowest mark.

 

This is why we set the highest_mark and lowest_mark to extreme values at the start - so the first mark entered becomes the new highest and lowest.

     total = total + test_mark

next i

The final steps of part 2 are to update the total marks and to close the loop.

 

The total is increased by the test mark that has been entered. 

 

The ‘next i’ command states that the current iteration has ended. The indentation has now stopped.

Part 3: Outputs

average_mark = total / number_of_tests

 

output “The average mark is:” , average_mark

output “The highest mark is:” , highest_mark

output “The lowest mark is:” , lowest_mark

Before the average can be output, it must be calculated by dividing the total by the number of tests.

 

Then the average, highest and lowest marks can be output.

Full Answer

number_of_tests is integer

test_mark is integer

average_mark is real

highest_mark is integer

lowest_mark is integer

total is integer

number_of_tests = 0

test_mark = 0

average_mark = 0

highest_mark = -1

lowest_mark = 101

total = 0

output “Enter the number of tests to mark: ”

input number_of_tests

for i = 1 to number_of_tests

     output “Enter the test mark: ”

     input test_ mark

     if test_mark > highest_mark then

          highest_mark = test_mark

     endif

     if test_mark < lowest_mark then

          lowest_mark = test_mark

     endif

     total = total + test_mark

next i

average_mark = total / number_of_tests

 

output “The average mark is:” , average_mark

output “The highest mark is:” , highest_mark

output “The lowest mark is:” , lowest_mark

This example is slightly more complicated than some of the recent previous exam questions for writing algorithms.

Remember to decompose the problem by identifying the variables you need first.

Monochrome on Transparent.png

Questo's Questions

8.3 - Writing Algorithms:

1. A violin player performs a piece of music 8 times. They record a score out of 5 how well they think they performed after each attempt.

Write an algorithm using pseudocode that allows the violinist to enter the 8 scores and displays the highest score, lowest score and average score.

 

An example score is 3.7. [10]

2. A cyclist wants a program to be made that allows them to enter how many laps of a circuit they have made and the time in seconds for each lap.

 

For example they may enter 3 laps, with times of 20.3, 23.4 and 19.8 seconds.

The program should output the quickest lap time, slowest lap time, total amount of time spent cycling and the average lap time. Create an algorithm using pseudocode for this scenario. [10]