# Data Structures and Algorithms

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
``````
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.

``````name = “James”
PRINT name + name
``````
Example 1: The output would be JamesJames.

``````number = “7”
PRINT number + number
``````
Example 2: The output would be 77.

### Real number

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

``````number = 7.42
PRINT number + number
``````
Example: The output would be: 14.84.

### Integer number

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

``````number = 7
PRINT number + number
``````
Example: The output would be: 14.

### Boolean

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

``````open = TRUE
PRINT open
``````
Example: 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.

``````colour = “Red”
colour = “Blue”
colour = “Green”
colour = “Orange”
colour = “Yellow”
``````
This example uses a text array named colour to store a series of named colours. The numbers 1-5 are the array index.

``````FOR count = 5 TO 1 STEP -1
OUTPUT colour[count]
ENDFOR
``````
For loop to output the text array items in reverse order.

``````Yellow
Orange
Green
Blue
Red
``````
Output of the above for loop.

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.
In some program languages allow the use of a text index, as seen below:
``````contact[name] = “James”
contact[age] = “27”
contact[phone] = “07654852816”
``````
Contact array using text index for name, age, and phone.

``````OUTPUT contact[age]
``````
This statement would produce the output 27.