1

mrahmedcomputing

KS3, GCSE, A-Level Computing Resources

Lesson 1. Arrays, Tuples, Records


Lesson Objective

  • Be familiar with the concept of a data structure.
  • Understand how data is represented and stored within different structures including.
    • arrays up to three dimensions
    • tuples
    • records
  • Use 1, 2, and 3 dimensional arrays in the design of solutions to simple problems.

Lesson Notes

Data Structures

In programming languages there are primitive data types such as char, real, integer and Boolean.

There may be built-in structured (non-primitive) data types such as strings, arrays, lists and records.

Structured data types are usually made up of primitive data types.


Arrays

If you need to sort, for example, 100 names of fruits into alphabetical order, it is inconvenient to have different names fruit1, fruit2, fruit3 to hold them.

Instead, they are all given one name, and referred to by an index as, for example, fruit[0], fruit[1] ... fruit[n].

Note:


Arrays vs Lists

Arrays and lists are both data structures that store ordered sequences of elements. However, there are some key differences between the two.

Arrays:

Lists: - Usually used in Python

In general, arrays are a good choice for storing and manipulating large amounts of data of the same type, while lists are a good choice for storing and manipulating smaller amounts of data of different types.


Array Properties

Arrays need the following:

  1. An identifier
  2. A data type
  3. The size of the array
  4. The dimensions of the array

Once declared an Array cannot change it's size or dimension whilst the program is running.

1 Dimensional Array

A 1 dimensional array is used to store multiples items of data. A List can also be stored in a Variable. Items in a List do not need to be the same data type.

Code:

shopping = ["Cheese","Apples","Milk","Goat"]
print (shopping)

Cmd Output:

[‘Cheese’,‘Apples’,‘Milk’,‘Goat’]

To refer to just one item in the list, you would refer to its index position. Each item is given a number. The first item is referred to as position "0".

Code:

shopping = ["Cheese","Apples","Milk","Goat"]
print (shopping[1])

Cmd Output:

Apples

2 Dimensional Array

2 dimensional arrays are used to structure data in a table format.

Code:

stutests = [["Ahmed","23"],["Abu","46"],["Bushra","12"],["Hafsa","76"]]
print (stutests)

Cmd Output:

[‘Ahmed’,‘23’],[‘Abu’,‘46’],[‘Bushra’,‘12’],[‘Hafsa’,‘76’]

2 Dimensional Array - Indexing

Code:

stutests = [["Ahmed","23"],["Abu","46"],["Bushra","12"],["Hafsa","76"]]
print (stutests[0])
print (stutests[3][0]) 

Cmd Output:

["Ahmed","23"]
Hafsa

3 Dimensional Arrays

3D arrays are essential for handling data that exists in three dimensions. Unlike 1D arrays which represent data along a single line and 2D arrays which form a flat grid, 3D arrays provide a structure for storing data with depth, width, and height. This makes them ideal for representing real-world objects, images, or complex datasets. For example, they are used in game development to model game worlds, in image processing to store color pixel information, and in scientific simulations to represent physical phenomena.

1D Array

array myArray[5]

myArray[2]

0
1
2 *
3
4

2D Array

array myArray[5,5]

myArray[2,2]

0 1 2 3 4
0
1
2 *
3
4

3D Arrays

array myArray[5,5,5]

myArray[4,4,1]

4D Arrays???

array myArray[5,5,5,5]

myArray[2,1,3,3]

Arrays - Problem?

The problem with using arrays to store data is that they are hard to read and are Homogeneous data structures - meaning they hold only one data type.

It is possible to define arrays with any number of dimensions.

The array is a set of elements with the same data type. You could use a 4-dimensional array to hold sales for branches of a supermarket, by country, store, department, and year.


Tuples

A tuple is an ordered sequence of elements. It is similar to a list, but it is immutable, meaning that it cannot be changed once it is created. Tuples are enclosed in parentheses (), and the elements are separated by commas.

For example, the following is a tuple:

myTuple = (1, 2, 3, "hello", [1, 2, 3])

Tuples can be used to store any type of data, including numbers, strings, lists, and other tuples. They are often used to represent fixed collections of data. They can be put into arrays.

Tuples have a number of advantages over lists:

Here are some examples of how tuples can be used:


Records

The problem with using arrays to store data is that they are hard to read and are Homogeneous data structures - meaning they hold only one data type.

Record are Heterogeneous data structures and allow you to store multiple data types. Once the data is captured as a record, it can be stored in an array.

Code:

RECORD scores
    studentname
    score1
    score2
ENDRECORD
class_score[0] = scores(“Ahmed”,23,28)
class_score[1] = scores(“Abu”,46,39)

Cmd Output:



Static and Dynamic

Data structures are characterised according to their ability to grow and shrink on demand.

Dynamic data structures change size when required.

Static data structures cannot change size.


Summary

Characteristics of the different data structures.

List Array Tuple Record
Fixed size (Static) X X
Number of items can change X
All items must be the same data type X
Fixed number of items X X X
Can have more than 1 dimension ? X
Items can be stored as different data types X X X
Can hold the item of the tuple and record structure. X X

3