Category: Algorithm specification

Higher – Algorithm Specification (Revision)

Input Validation: making sure that the data input by the user is acceptable e.g. in a suitable format and within the upper and lower limits of the data required by the software

RECEIVE userInput FROM (INTEGER) KEYBOARD
WHILE userInput < lowerLimit OR userInput > uperLimit DO
    SEND “Input must be between” & lowerLimit & “and” & upperLimit TO DISPLAY
    RECEIVE userInput FROM (INTEGER) KEYBOARD
END WHILE

Linear Search (efficient): sets a Boolean variable to false initially and uses an unconditional loop to set it to true when the item is found; the loop terminates when the item is found or the end of the array is reached

RECEIVE itemToFind FROM (INTEGER) KEYBOARD
SET found to false
SET arraySize to higestIndex
SET counter TO 0

WHILE counter <= arraySize AND found = false
    SET counter to counter + 1
    IF array[counter] = itemToFind THEN
        found=true
    END IF
END WHILE

IF found = true THEN
    SEND itemToFind & “found at position” & counter TO DISPLAY
ELSE
    SEND “Item not found” TO DISPLAY
END IF

Finding Minimum: sets an initial value to the first item in the array then compares it to the remaining items

SET maxiumValue to numbers[0]

FOR counter FROM 1 TO 9 DO
    IF maximumValue < numbers[counter] THEN
        SET maximumValue to numbers[counter]
    END IF
END FOR

SEND “The largest value was” & maximumValue to DISPLAY

Finding Maximum: sets an initial value to the first item in the array then compares it to the remaining items

SET miniumValue to numbers[0]

FOR counter FROM 1 TO 9 DO
    IF minimumValue > numbers[counter] THEN
        SET minimumValue to numbers[counter]
    END IF
END FOR

SEND “The smallest value was” & minimumValue to DISPLAY

Count Occurrences: sets a total to zero at the beginning and increments it as items are found to match the search item

RECEIVE itemToFind FROM (INTEGER) KEYBOARD
SET numberFound to 0

FOR EACH number FROM numbers DO
    IF number=itemToFind THEN
        SET numberFound to numberFound+1
    END IF
END FOR EACH

SEND “There were” & numberFound & “occurreneces of” & itemToFind & “in the list” TO DISPLAY

Thank you to C O’Toole & A Madill from Braidhurst High School for allowing me to edit and publish this here.

National 5 – Algorithm Specification (Revision)

Input validation – checking that what is entered by the user is acceptable.

The following program checks that age is a positive number.

1. SEND “Please enter age” TO DISPLAY
3. RECEIVE age FROM (INTEGER) KEYBOARD
4. WHILE age < 0 DO
5.         SEND “re-enter data” TO DISPLAY
6.         RECEIVE age FROM (INTEGER) KEYBOARD
7. END WHILE

Note that acceptable does not mean accurate, just sensible.

Higher – Reading Standard Reference Language

I would like to remind you that there is no requirement to write answers using Standard Reference Language (SRL). The SQA can sometimes refer to Standard Reference Language as “Pseudocode”, however, as I have previously explained this is not the case. Pseudocode is an informal design that explains a programs function using English like structure.

Examples

Read in a number representing a temperature in degrees Celsius and write it out as a value in degrees Fahrenheit. If the Celsius value is c, then the Fahrenheit value, f, is calculated as follows: f = ( 9 / 5 ) * c + 32.

RECEIVE c FROM (INTEGER) KEYBOARD 
DECLARE f INITIALLY ( 9.0 / 5.0 ) * c + 32 
SEND f TO DISPLAY

Read in 10 numbers and write out the average of those numbers.

DECLARE total INITIALLY 0 
DECLARE count INITIALLY 0 
WHILE count < 10 DO 
    RECEIVE nextInput FROM (INTEGER) KEYBOARD 
    SET total TO total + nextInput 
    SET count TO count + 1 
END WHILE 
SEND total / 10.0 TO DISPLAY

When reading SRL start at the top, working down each line. Take a note on scrap paper of the values given to variables. In SRL constructs start with their NAME and finish with an END and their name. Subprograms can either be a PROCEDURE or FUNCTION, these are named and have parameters (arguments) in parenthesises, functions will have one or more RETURN statements.

Tips

  • If you are asked for the results of the program then pay attention to the IF statements to make sure that you branch the program correctly. Check the number of repetitions on loops as well.
  • If you are asked to spot an error in the code it will more than likely be a logical error rather than a syntax error.

There is an online Haggis (SRL) checker here, remember you don’t need to write SRL just read it.

Python Tables

tableI was asked in class how we can create a table in Python. There are a number of ways to do this but the easiest is probably to use the .format method.

The program reads in data from a CSV file before displaying the information in a two column table. There is a lot of internal commentary you can strip out leaving just the code.

The main part of the program is line 28. Where the contents of names and scores are displayed one line at a time with padding to make them appear to be in a table.

Read more

N5 & Higher – Input Validation (Python)

Input validation is used in both National 5 and at Higher. It consists of 3 parts.

  1. Get the user to enter a value
  2. While the value is wrong
  3.     Get the user to enter a value

The use of the while construct allows the program to repeatedly ask the user to enter valid data.

1
2
3
4
5
6
7
8
9
age=0

#Input Validation
age=int(input('Please enter your age>'))
while age<0 or age>120: # Check that age is between 0 and 120
    print('Please check your age is between 0 and 120')
    age=int(input('Please enter your age>'))
    
print('Welcome')

This can also be used with strings, can you work out how to modify the code so that it would only take a ‘Yes’ ‘No’ answer?