Array
Array
Linear Search is defined as a sequential search algorithm that starts at one end and goes through
each element of a list until the desired element is found, otherwise the search continues till the
end of the data set.
Multidimensional Arrays
• A multidimensional array is an ‘array of arrays’
• each item at an index is another array
0 1 2 3
0 80 59 34 89
1 31 11 47 64
2 29 56 13 91
Accessing 2D array
examresult[1,2] --> 47
examresult[0,0] --> 80 ( each item of data has two indexes)
examresult=[[80,59,34,89],[31,11,47,64],[29,56,13,91]]
Question
A teacher has stored the surnames and test scores of a class
students in na two-dimensional array.
e.g. results[[‘smith’, ‘69’],[‘Jakson’, ‘90’], etc.
create a program that would print out the names and test
scores of all the students who have scored 50 or over in the
test.
Answer
Activity 17
Develop a program tthat creates and intialises an array to hold these
five sets of marks.
80,59,34,89
31,11,47,64
29,56,13,91
55,61,48,0
75,78,81,91
Extend your program so that it calculates and displays the highest
mark,lowest mark and average mark achived.
marks = [[80, 59, 34, 89], [31, 11, 47, 64], if column > highestMark:
[29, 56, 13, 91], [55, 61, 48, 0], highestMark = column
[75, 78, 81, 91]] elif column < lowestMark:
highestMark = marks[0][0] lowestMark = column
lowestMark = marks[0][0] print(‘The highest mark is’,
#Initialises highest and lowest mark to highestMark)
first mark in the list print(‘The lowest mark is’,
lowestMark)
total = 0
print(‘The average mark is’,
count = 0 total/count)
for row in marks:
for column in row:
total += column
count +=1
Activity 18
This two-dimensional array holds highest score achived by each player in each of
the three levels of an online game.
1.Develop a program that intialises the array and then searches through it to find
the player with highest score in each of three levels.
PLAYER LEVEL SCORE
Alexis 1 19
Seema 1 29
Seema 2 44
Lois 1 10
Alexis 2 17
Alexis 3 36
Dion 1 23
Emma 1 27
Emma 2 48
highestL1Score = 0
highestL1Player = ‘ ’
highestL2Score = 0
highestL2Player = ‘ ’
highestL3Score = 0
highestL3Player = ‘ ’
for row in gameScores:
player = row[0]
level = row[1]
score = row[2]
if level == 1 and score > highestL1Score:
highestL1Score = score
highestL1Player = player
elif level == 2 and score > highestL2Score:
highestL2Score = score
highestL2Player = player
elif level == 3 and score > highestL3Score:
highestL3Score = score
highestL3Player = player
print(‘The highest score in Level 1 was’, highestL1Score, ‘achieved by’,
highestL1Player)
print(‘The highest score in Level 2 was’, highestL2Score, ‘achieved by’,
highestL2Player)
print(‘The highest score in Level 3 was’, highestL3Score, ‘achieved by’,
highestL3Player)
RECORDS
The elements of an array must all be the same data type.
But Records data structure stores a set of related values of different data types.
Each element in a record is known as a field and is referenced using a field name.
LearnerNum firstName lastName Age FORM
1 Isla smith 15 10H
2 Shinji Fujita 14 10B
3 Anita khan 15 10A
4 Abdur Rahman 15 10G