0% found this document useful (0 votes)
9 views

Array

Uploaded by

Yevin Dulkith
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Array

Uploaded by

Yevin Dulkith
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Activity 16

Describing and Implementing a linear search algorithm

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

One Dimensional Array


A One-Dimensional Array is a group of elements having the same data
type which are stored in a linear arrangement under a single variable
name.
Two Dimensional Array
A two-dimensional array, also known as a 2D array, is a collection of data elements
arranged in a grid-like structure with rows and columns. Each element in the array
is referred to as a cell and can be accessed by its row and column indices/indexes.

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

Each column stores one item of information about learner


they are number,age,form etc. (these are the fields)
all the values have same data type
e.g.
LearnNum and Age ---> Integer
firstName , lastName and ‘form’ ---> Strings
Activity 19
1. A record data structure is to be used to store the details of music
albums. provide the appropriate data types of thses fields.
1. the title of the album
2. the name of the artist
3. the year of release
4. the genre
2. Develop a program that uses a record structure for stroing the detaisl of
music albums. it must:
a. have fields for title,artist,year of release and genre
b. allow the user to input the detaisl of new albums
c. allow the user to search for an album by bname and display its details
Answers
1.
a. string
b. string
c. integer
d. string
albumCollection = [[‘Where Rivers Meet’, ‘Z Rahman’, 2008, ‘World’],
[‘Best of Cat Stevens’, ‘C Stevens’, 1984, ‘Pop’], [‘Come Away With Me’, ‘N
Jones’, 2012, ‘Pop’],
[‘Shine’, ‘Bond’, 2002, ‘Instrumental’], [‘Blessing’, ‘J Rutter’, 2012, ‘Classical’]]
anotherGo = ‘y’
while anotherGo == ‘y’:
while True:
choice = input(“Press ‘e’ to enter details of a new album, or ‘s’ to search
for an album.”)
if choice == ‘e’ or choice == ‘s’:
break
if choice == ‘e’:
newAlbum = []
album = input(‘Enter the name of the album:’)
artist = input(‘Enter the name of the artist:’)
year = int(input(‘Enter the year of release:’))
genre = input(‘Enter the genre:’)
newAlbum = [album, artist, year, genre]
albumCollection.append(newAlbum)
else:
searchAlbum = input(‘Enter the title of the album:’)
found = False
index = 0
while found == False and index <= (len(albumCollection) - 1):
if albumCollection[index][0] == searchAlbum:
print(‘Artist:’, albumCollection[index][1], ‘\nYear of release:’,
albumCollection[index][2])found = True
else:
index = index + 1
if found == False:
print(‘This album is not in your collection.’)
anotherGo = input(“Press ‘y’ if you want another go.”)

You might also like