 # Algorithm Specification

## Learning Intentions

• Understand what an algorithm is

• Describe, exemplify and implement the standard algorithms required for National 5

## Success Criteria

• Be able to describe the steps of the standard algorithms at National 5
• Be able to implement the standard algorithms in Python

## What is an algorithm?

• An algorithm is a series of instructions that can be used to solve a problem.
• A standard algorithm is an algorithm that is used commonly in the programming community.
• For National 5 you need to know what the purpose of and how to implement the following standard algorithms:
• Input validation
• Running total
• Traverse an array
• In theory, a standard algorithm should be easily transferrable to another programming language.

## Input validation

• The input validation algorithm is used to check that some data entry is valid to a certain criteria or within a certain range.
• An input validation algorithm works by asking the user for a value, checking it is valid and meets the rules then deciding whether to ask the user for the information again (until it is valid) or to accept the data

RECIEVE input FROM KEYBOARD

REPEAT

RECIEVE input FROM KEYBOARD

UNTIL input > 10 AND input < 20

## Input validation

• To implement this in Python, we use the following code:
``````print("Please insert a number between 10 and 20")

print("The number needs to be between 10 and 20")
print("Please insert a number between 10 and 20")

## Input validation

• The following is an example of username authentication
``````print("Please insert your username")

print("Welcome to the system")``````

The program should display how many times the person got it wrong at the end.

## A solution in Python

``````attempts = 1

attempts = attempts + 1

print("You took", attempts, "attempts to login")``````

## Running total

• Running total is another algorithm you need to know how to implement at National 5.
• The running total algorithm works by going through an array and adding each item to the total variable. ## Running total

• In SQARL a running total algorithm looks like:

items = [11, 22, 33, 44, 55]

total = 0
FOR counter FROM 1 TO LENGTH(items) DO

SET total TO total + 1

END FOR

SEND total TO DISPLAY

## Running total in Python

• In Python a running total algorithm looks like:
``````items = [34, 12, 54, 30, 90, 54, 43, 30, 94, 34]
total = 0

for i in range(0, len(items)):
total = total + items[i]

print(total)``````

Copy the code from the previous running total.

Modify the running total so that it counts all numbers above 50 and displays the total.

## Traversing an array

• It is fairly common to need to traverse an array.
• It's a very straightforward algorithm that will make things like displaying an array easier

FOR counter FROM 0 TO LENGTH(list) DO
SEND list[counter] TO DISPLAY
END FOR

## Traversing an array in Python

• It is fairly common to need to traverse an array.
• It's a very straightforward algorithm that will make things like displaying an array easier
``````items = [34, 12, 54, 30, 90, 54, 43, 30, 94, 34]
for i in range(0, len(items)):
print(items[i])``````
1. Write a program to take in 10 test marks and then calculate and display the average mark.
2. A test is carried out with a 6/10 pass mark.
1. Ask the user to enter 5 test marks and ensure the mark is between 5 and 10.
2. Add a running total that increases each time a person passes the test
3. Modify the program so that if the person passed it displays their name and mark.
3. Ask the user for what day of the month and the number of the month they were born. Ensure that the day and the month are within a suitable range using input validation.

Presentation Overview
Algorithm Specification
14:47 | 07-12-2023
!
Keywords
Random selector
Sections
Binary conversion
Denary to binary conversion
Binary to denary conversion
Feedback 👍
Accessibility

Apply a filter:

×
All slideshow files