Arrays Flashcards
array
a data structure that can store multiple items of data, called elements, which are all the same data type, under the same identifier
elements
an item of data in an array at a particular index
they are all the same data type
they are stored under one identifier
How is an array created?
by specifying the elements stored in the array
this is called declaring the array
e.g.
array names = [“Alice”, “Catherine”]
array scores = [10, 13, 17]
indexes
the location of an element in an array
two dimensional arrays
- what they are
- how they are used
an array structured like a table
each element has two indices to indicate its position
an array of arrays which forms a matrix
e.g.
an array with 5 rows and 4 columns would be declared like this:
array scores [5, 4]
data would be added like this:
scores [3,3] = 15
A student recorded their resting pulse three times a day (8am, 12:30pm and 8pm) for four days and stored the data in a 2D array:
0/day 1: 0 = 65 1 = 73 2 = 70
1/day 2: 0 = 65 1 = 75 2 = 68
2: 0 = 70 1 = 80 2 = 65
3: 0 = 68 1 = 79 2 = 69
a) to output the pulse rate at noon on day 1 the student wrote the following code:
print(pulse[0, 1])
i) state the pulse rate output from this statement [1]
ii) write the code to output the pulse rate in the evening on day 3 [1]
b) write the code that would output the average pulse rate for each day [5]
a) i) 73
a) ii) print(pulse[2, 2])
b) for day = 0 to 3 total = 0 for time = 0 to 2 total = total + pulse[day, time] next time print("Average = " + total/3) next day
A student has stored the first name and surname of each of their friends (in that order) into a 2D array named ‘friends’.
Write an algorithm that would allow the student to enter a friends name and check whether that friend is in the array. [5]
firstName = input("Please enter the first name") lastName = input("Please enter the last name")
index = 0 found = false
while found == false AND index <= friends.length - 1
if friends[index, 0] == firstName and friends[index, 1] ==
lastName then
found == true
endif
index = index + 1
endwhile
if found == true then print("This friend is in the array") else print("This friend is not in the array") endif