Array

From Higher Computing Science
Jump to: navigation, search

This article is unfinished. Please consider joining and adding to this article. Read about Page layout beforehand.

Key points

  • An array is an ordered sequence of simple data types, all of the same type
  • An array is used to store a list of items - a variable only stores one item.
  • Each item in the list is given an index number. Some languages index from 0 onwards, some index from 1. Haggis Pseudocode (which is used by the SQA in exams) is indexed from 0 onwards.
  • Arrays are a good way of storing lists as they can be processed using their index, making it easy to search and sort the list

Information

An array is an ordered sequence of simple data, such as numbers or strings. Every item in the array has an index number.

The easiest way to think about this is to imagine a series of boxes. The boxes represent each memory location. The boxes are numbered. Each number is the box's index.

Arrayboxes.png

In this example, the numbers are indexed from zero. Computers and programming languages like starting with zero instead of one, because to miss zero wastes data (for example, in graphics, black is usually represented by the number zero!).

In the code below, the array (which has been called numbers) is filled with values. Try the exercises to further understand arrays.

After the first set of lines, the array would look like this:

Filledarray.png

Arrays can only be accessed using the index number.

Videos



Further information

Khan Academy's interactive tutorial on arrays

Arrays on Wikipedia

Test yourself

1. How many items can an array hold?

Only one item.
No - arrays are designed to store lists of items
One or more items.
Yes, arrays are designed to store lists of items.
No items, only index numbers.
Index numbers are used for addressing each item.

Your score is 0 / 0


Teaching resources