Signed up recently but had issues with the confirmation email?
Click Forgot password and follow the reset steps. You can choose the same password you used when you signed up. This should then provide access.
Remember to check Spam and Junk. The email will be from auth@progressbytes.co.uk.
The use of arrays (or equivalent) when solving problems: two-dimensional arrays(2D)
2.2.3 Additional Programming Techniques
Practise exam questionsAdditional Programming Techniques — Note 7 of 13
Use of 2D arrays to emulate database tables of a collection of fields, and records
A two-dimensional (2D) array is an array of arrays, like a table or grid with rows and columns. You access elements using two indices: the first selects the row and the second selects the column. 2D arrays can be used to emulate database tables where each row is a record and each column is a field.
array[row, column] (OCR ERL) or array[row][column] (Python).array seats[3, 4] the rows are 0 to 2 and the columns 0 to 3, so seats[3, 0] and seats[0, 4] don't exist.journeys[3,1] = "3.5" (row 3, column 1).int() or float() before you use them in a calculation.scores[row][column]: the first index picks the row (the week), the second picks the column (the day). scores[1][2] is week 2, Wednesday.# 2D array (list of lists in Python)
seating = [
["pupil1", "pupil2"],
["pupil3", "pupil4"]
]
print(seating[0][1]) # "pupil2" (row 0, col 1)
print(seating[1][0]) # "pupil3" (row 1, col 0)
# Loop through all elements
for row in range(len(seating)):
for col in range(len(seating[row])):
print(seating[row][col], end=" ")
print() # New line after each row// Each row is a week; each column is a day (0 = Monday ... 4 = Friday)
array scores[4, 5]
scores[1, 2] = 9 // week 2 (row 1), Wednesday (column 2)
// ... the other 19 scores are stored the same way
print(scores[1, 2]) // 9
// Every score: outer loop = weeks (rows), inner loop = days (columns)
for i = 0 to 3
for j = 0 to 4
print("Week " + str(i + 1) + " Day " + str(j) + " Score: " + str(scores[i, j]))
next j
next i# Each row is a week; each column is a day (0 = Monday ... 4 = Friday)
scores = [
[4, 7, 8, 6, 5], # Week 1
[6, 5, 9, 4, 3], # Week 2
[10, 3, 6, 7, 2], # Week 3
[7, 8, 5, 6, 9] # Week 4
]
# Week 2 (row 1), Wednesday (column 2)
print(scores[1][2]) # 9
# Every score: outer loop = weeks, inner loop = days
for i in range(4):
for j in range(5):
print("Week", i + 1, "Day", j, "Score:", scores[i][j])// uses the scores array above, already filled
total = 0
count = 0
for i = 0 to 3 // each week
for j = 0 to 4 // each weekday
total = total + scores[i, j]
count = count + 1
next j
next i
average = total / count
print("Monthly total: " + str(total))
print("Monthly average: " + str(average))# uses the scores array defined above
total = 0
count = 0
for i in range(4): # each week
for j in range(5): # each weekday
total += scores[i][j]
count += 1
average = total / count
print("Monthly total:", total)
print("Monthly average:", average)// uses the scores array above
for i = 0 to 3 // for each week (row)
weekTotal = 0 // reset for EVERY week
for j = 0 to 4 // for each day in that week
weekTotal = weekTotal + scores[i, j]
next j
weekAverage = weekTotal / 5
print("Week " + str(i + 1) + " Total: " + str(weekTotal) + " Average: " + str(weekAverage))
next i# uses the scores array defined above
for i in range(4): # for each week (row)
week_total = 0 # reset for EVERY week
for j in range(5): # for each day in that week
week_total += scores[i][j]
week_average = week_total / 5
print("Week", i + 1, "Total:", week_total, "Average:", week_average)// uses the scores array above
for j = 0 to 4 // for each day (column)
dayTotal = 0 // reset for EVERY day
for i = 0 to 3 // for each week
dayTotal = dayTotal + scores[i, j]
next i
dayAverage = dayTotal / 4
print("Day " + str(j) + " (0 = Mon) Total: " + str(dayTotal) + " Average: " + str(dayAverage))
next j# uses the scores array defined above
for j in range(5): # for each day (column)
day_total = 0 # reset for EVERY day
for i in range(4): # for each week
day_total += scores[i][j]
day_average = day_total / 4
print("Day", j, "(0 = Mon) Total:", day_total, "Average:", day_average)total = total + float(prices[i, 2]).deliveries (20 rows; column 1 holds the van, column 3 the number of parcels): for i = 0 to 19, if deliveries[i, 1] == van then total = total + int(deliveries[i, 3]), endif, next i, then output total AFTER the loop. Compare one field, then add a DIFFERENT field, cast to a number.Ready to test yourself?
Practise 2.2.3 questions