Standard Algorithms

What is a Standard Algorithm?

A standard algorithm is a set of instructions that are used to solve a particular (and usually repeated) task.

At National 5 the three standard algorithms you need to be able to do write are:

  • Input Validation

  • Keep a Running total within a loop.

  • Traverse a 1D Array

Running Total - Using Variables

During some programs you will have to keep a running total. The logic for this is you keep a variable to store the total ( starting at 0) and then increment it with a value ( usually using a loop).

Example:

  • A program to ask for the amount of rainfall in a day for a week and then add them together.

You may sometimes also see the line:

  • totalrainfall = totalrainfall + dailyrainfall shortened to totalrainfall += dailyrainfall

This is just a shortened operator and is exactly the same.

Running Total - Using Arrays

It may be that the program already has the data inside an array so we will need to traverse the array and add all of the values together. For this example we have pre-populated the array with some values.

We have hard coded the value 7 in the code above but instead can use the len function to find the number of items in the array and use that instead.

So for index in range(len(rainfall)): will change to for index in range(len(rainfall)):

Calculating the Average

Although not specified in the course - if you also wanted to create the average of the running total we would only need to add one single line after the loop had finished.

averagerainfall = totalrainfall/7

So our finished could be

Running Example

Running Total Tutorial Video

SDD 8.3 Running Totals.mp4

Input Validation

The purpose of Input Validation is to ensure that the user inputs data that is in a specified format or range

There are various times you have to do this

  • You must be 18 or over to register

  • The price for an item must be greater than 0

  • You are allowed to purchase a maximum number of tickets

  • A percentage must be between 0 and 100

  • A password isn’t too long

Input Validation Pseudocode

GET input FROM (TYPE) KEYBOARD

WHILE input is invalid

SEND "Invalid Message" TO DISPLAY

GET input FROM (TYPE) KEYBOARD

END WHILE

Python Input Validation and Example

The flow chart below demonstrates how to validate that a score is between 0 to 9999. If it is out-with this range then an invalid message will be displayed and the user will need to input their score again.

Pseudocode

GET score FROM KEYBOARD

WHILE score <0 or score >9999

SEND "Invalid Message" TO DISPLAY

GET score FROM KEYBOARD

END WHILE

Flow Chart

The shaded yellow part is the Input Validation section

Python Example

The Python code above will validate the user input to be between 0 and 9999.

Python Input - Restricting Text Input

Sometimes you will want the user to only be able to enter certain characters such as 'Y' or 'N'. One way to to this is to use the not equals to operator. You would ask the user for input as usual. You would then have to check if the input isn't equal to N and it isn't equal to Y.

Example Python Code

If we look at the logic of the while condition while myinput !='Y' and myinput !='N': we can see how this works.

Validating data in an array

You can get all values into an array using a fixed loop and placing a value into each element. The len() function is used to find the length of the array. You will notice that the input is validated before placing it into the array.

The Python code demonstrates how to validate data that is being stored in an array and can be executed below.

Input Validation Tutorial Video

SDD 8.1 Input Validation.mp4

Traversing a 1D Array

There may be occasions when you have to perform some operation to every element of an array such as printing it or performing a calculation,

This means you will have to use a loop to traverse the array (this just means to iterate(go through) through every element in the array)

Example - Printing each Element

Example - Calculation on each element.

In this example we will go through each element of an array of prices to increase each element by 10.

In the code below this is achieved by the line prices[counter] = prices[counter] + 10

This code can be executed below.

Tutorial Video - Traversing an Array

07 - Arrays.mp4