Arrays

Teach any GCSE Computer Science class

We have put together a full GCSE Computer Science curriculum that will give you all the teaching materials you need to teach any topic.

Whether you’re a brand new Computer Science teacher, or you’ve been teaching ICT for years, our resources will save you hours and hours of lesson preparation every single week.

View the resources →

An array is a data framework that is composed of a group of elements.  These elements have similar data types, for example all are integers, or all are strings.  Arrays are utilised in computer programs to arrange data where interrelated set of values can be categorised and located.
An example of a practical use of array is in search engines.  A search engine can make use of an array to save the web pages that were stumbled upon in a search carried out by a user.  To show the search results, the program will display an element of the array at a time.  This can be done for a specific number of values or until all values have been displayed.  The program has an option to assign a new variable for each search result found, but making use of an array is an efficient way to control memory usage.
Another situation that calls for the practical use of array is in storing integer numbers.  If you need to store 3 integer numbers, you can create 3 variables with integer data type.  That would be simple.  But what if, you need to store 300 integer numbers?  Is it practical to create 300 variables with integer data type?  Or is it more practical to create an array variable number with integer data type from 0 to 299?

Characteristics of an Array

  • Each element has similar data type while they may have dissimilar values.
  • The whole array is saved contiguously in memory, meaning, there are no spaces between elements.

Dimensions of an Array

  • Vector is a one-dimensional array.
  • Matrix is a two-dimensional array.

Components of an Array

A vector has the following components:

  • Name is a valid identifier.
  • Type is a valid data type such as int, float, etc.  This is the data type of all the array elements.
  • Extent is the range of indices of array elements.
    • For example, the range of an array can be 0 to 4 (element 0, element 2, …, element 4)
    • The indices must be integers within the range.
    • The smallest index is referred to as the lower bound.
    • The largest index is referred to as the upper bound.
    • The extent of an array is smaller-integer:larger-integer
      • where smaller-integer is the lower bound
      • where larger integer is the upper bound
      • In the example above, the extent is 0:4

Creating an Array

To create an array, you need to specify the type of elements and the number of elements to be saved in the array.  Here’s a simple syntax in creating an array in C programming:

type arrayName[arraySize];

Creating an array in C

where type is any valid C data type
where arrayName is a valid identifier
where arraySize is a constant integer > 0
Example:

int number[10];

This is a variable array called number that can hold up to integer numbers.

Declaring an Array

The syntax for declaring arrays is as follows:

type, DIMENSION(extent) :: name-1, name-2, …, name-n

Declaring an array in C

where type is the data type of the arrays
where DIMENSION is a required keyword
where extent gives the range of the array indices
where name-1, name-2, …, name-n are the array names
Example:

REAL, DIMENSION(-2:2) :: b, Total
INTEGER, DIMENSION(0:50) ::  DataEntry

Full example of declaring an array in C

The elements of arrays b and Total are real numbers and the indices are in the range of -2 and 2.
The elements of array DataEntry are integers and the indices are in the range of 0 and 50.
The integers in a certain degree can be parameters.
Example:

INTEGER, PARAMETER :: MaximumSize = 50
LOGICAL, DIMENSION(1:MaximumSize) :: AnswerKey
INTEGER, PARAMETER :: LowerBound = -5
INTEGER, PARAMETER :: UppperBound = 5
REAL, DIMENSION(LowerBound:UpperBound) :: Points, James

The range of array AnswerKey is 1 and 50, while the range of arrays Points and James is -5 and 5.