Handling Data in Algorithms

GCSE Algorithms Resources (14-16 years)

  • An editable PowerPoint lesson presentation
  • Editable revision handouts
  • A glossary which covers the key terminologies of the module
  • Topic mindmaps for visualising the key concepts
  • Printable flashcards to help students engage active recall and confidence-based repetition
  • A quiz with accompanying answer key to test knowledge and understanding of the module

A-Level Introduction to Algorithms (16-18 years)

  • An editable PowerPoint lesson presentation
  • Editable revision handouts
  • A glossary which covers the key terminologies of the module
  • Topic mindmaps for visualising the key concepts
  • Printable flashcards to help students engage active recall and confidence-based repetition
  • A quiz with accompanying answer key to test knowledge and understanding of the module

Candidates should be able to:

  • define the terms variable and constant as used in an imperative language
  • use variables and constants
  • describe the data types integer, real, Boolean, character and string
  • select and justify appropriate data types for a given program
  • perform common operations on numeric and Boolean data
  • use one-dimensional arrays.

What are constants and variables?

Constants

Constants are symbolic names given to data where the value of the data cannot change during the execution of the program. Many constants such as PI are built into programming languages.

Variables

Variables are symbolic names given to data where the value of the data stored may change during the execution of the program. In effect, a variable is a named area of memory used to store data.

Declaring constants and variables

Many programming languages require constants and variables to be ‘declared’ at the start of the program, this defines the variable type (text, integer etc.) and sets the value of any constants.

  • The value of a constant is set when it is declared and cannot change when the program is executed.
  • A variable has no value when it is declared but it’s value can change when the program is executed.

Naming constants and variables

The name used to label a variable or a constant can be a single letter but many programming languages allow meaningful names to be used which make a program far easier to understand. In almost all languages, variable names cannot start with a digit (0–9) and cannot contain whitespace characters.

An example of a statement using constants and variables:

  • The value of PI would be very tedious to have to keep entering in a program if it was used repeatedly in calculations. If a named constant is used to store the value of PI then this can be used instead.
CONSTANT PI = 3.142
INPUT circle_radius
circle_area = PI * circle_radius * circle_radius
  • Here the constant PI is used in a statement that calculates the area of a circle. The variable circle_radius is used to store the radius of the circle and the variable circle_area is used to store the result of the calculation.

What data types are used in programming?

Most programming languages require the user to declare the type of data stored in the variable or constant. Some of the most common examples are described below:

Character/string

A character is a single letter, number or symbol and is treated a text, even if it is a number.

A string is made up of two or more characters.

Example 1:

name = “James”
PRINT name + name

The output would be JamesJames

Example 2:

number = “7”
PRINT number + number

The output would be 77

Real number

This data type is used to store real numbers i.e. values which might have decimal places.

Example:

number = 7.42
PRINT number + number

The output would be: 14.84

Integer number

This data type is used to store whole numbers. i.e. values with no decimal places.

Example:

number = 7
PRINT number + number

The output would be: 14

Boolean

This data type is used to store the logical data TRUE/FALSE

Example:

open = TRUE
PRINT open

The output would be: TRUE


What is an array?

An array is a variable that can be indexed. The variables in the array have a common name and each one has an index to identify it within the array. As with normal variables, arrays can be text, real number, integer etc. The index is typically an integer number or integer variable.

The example below uses a text array named colour to store a series of named colours. The numbers 1-5 are the array index:

colour[1] = “Red”
colour[2] = “Blue”
colour[3] = “Green”
colour[4] = “Orange”
colour[5] = “Yellow”

The statements:

FOR count = 5 TO 1 STEP -1
OUTPUT colour[count]
ENDFOR

Would produce the following output:

Yellow
Orange
Green
Blue
Red

The advantage of using a numerical index in an array is that if the index is a variable it is easy to input and output data using an iteration loop as in the use of the variable count in the example above.

Some programming languages also allow the use of a text index as in the examples below:

contact[name] = "James"
contact[age] = "27"
contact[phone] = "07654852816"

The following statement would produce the output 27

OUTPUT contact[age]