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:
- the use of [ ] to hold the index
- Some programming languages may use ( ) instead
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:
- Arrays have a fixed size, which must be specified when the array is created.
- Arrays can only store elements of the same data type.
- Arrays are more efficient for performing mathematical operations on arrays of data.
- Arrays are typically implemented as contiguous blocks of memory, which makes them more efficient for memory usage.
Lists: - Usually used in Python
- Lists have a dynamic size, which means that their size can grow and shrink as needed.
- Lists can store elements of different data types.
- Lists are less efficient for performing mathematical operations on arrays of data.
- Lists are typically implemented as linked lists, which makes them less efficient for memory usage.
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:
- An identifier
- A data type
- The size of the array
- 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])
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]
2D Array
array myArray[5,5]
myArray[2,2]
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.
- e.g. sales[c, s, d, y] = 23450
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:
- They are more efficient, because they do not need to store any information about their size or structure.
- They are more secure, because they cannot be accidentally modified.
- They are more versatile, because they can be used in contexts where lists cannot, such as as the keys in a dictionary.
Here are some examples of how tuples can be used:
- To store the coordinates of a point:
point = (10, 20)
- To store the dimensions of a rectangle:
rectangle = (100, 50)
- To store a user's name and email address:
user = ("Alice", "alice@example.com")
- To store the results of a database query:
results = (1, "Alice", 20)
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)
Static and Dynamic
Data structures are characterised according to their ability to grow and shrink on demand.
Dynamic data structures change size when required.
- Lists (arraylists) grow when items are added and shrink when items are removed.
Static data structures cannot change size.
- Arrays in most languages do not change size automatically.
- Some languages provide functions to resize, but this has to be programmed, it is not done automatically.
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 |
|
|