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: one-dimensional (1D)
2.2.3 Additional Programming Techniques
Practise exam questionsAdditional Programming Techniques — Note 6 of 13
Arrays as fixed length or static structures
A one-dimensional (1D) array is a data structure that stores a fixed-size collection of elements of the same data type under a single name. Each element is accessed using an index (position number), starting from 0. Arrays are static, meaning their size cannot change once they are created.
array names[5] creates an array with 5 elements (index 0 to 4).names[0] = "player1" puts "player1" at position 0.for i = 0 to players.length - 1.len(scores) gives the number of items, so for i in range(len(scores)): visits every index. This is safer than hard-coding range(3), which breaks if the array changes size.found = False before the loop, set it to True inside the loop when a match is found, then report the result AFTER the loop has finished.// Create and access a 1D array
array scores = [10, 20, 30, 40, 50]
print(scores[0]) // 10 (first element)
print(scores[2]) // 30 (third element)
scores[1] = 25 // update the second element
// Loop through the array: the counter is the index
for i = 0 to 4
print("Index " + str(i) + ": " + str(scores[i]))
next i# Create and access a 1D array (list in Python)
scores = [10, 20, 30, 40, 50]
print(scores[0]) # 10 (first element)
print(scores[2]) # 30 (third element)
scores[1] = 25 # Update second element
# Loop through the array
for i in range(len(scores)):
print("Index " + str(i) + ": " + str(scores[i]))scores = [4, 7, 8] # define an array
print(scores[1]) # 7: index 1 is the SECOND element
for i in range(len(scores)): # index 0, 1, 2
print(scores[i]) # print each item
scores[0] = 10 # replace the first element (index 0) with 10array scores = [4, 7, 8]
total = 0 // running total
for i = 0 to 2 // indexes 0, 1, 2
total = total + scores[i]
next i
average = total / 3
print("Total: " + str(total)) // Total: 19
print("Average: " + str(average)) // Average: 6.333...scores = [4, 7, 8]
total = 0 # running total
for i in range(len(scores)): # loop through each index
total += scores[i] # add each score to the total
average = total / len(scores)
print("Total:", total) # Total: 19
print("Average:", average) # Average: 6.333...array scores = [12, 7, 5, 10, 3, 2, 7]
target = 7 // the value we are searching for
found = False // flag: nothing found yet
for i = 0 to 6
if scores[i] == target then
found = True // set the flag when a match is found
endif
next i
if found == True then // checked AFTER the loop
print("Found")
else
print("Not found")
endifscores = [12, 7, 5, 10, 3, 2, 7]
target = 7 # the value we are searching for
found = False # flag: nothing found yet
for i in range(len(scores)):
if scores[i] == target:
found = True # set the flag when a match is found
if found: # checked AFTER the loop
print("Found")
else:
print("Not found")array scores = [12, 5, 10, 6, 7, 12]
target = 12
count = 0 // how many times the target appears
for i = 0 to 5
if scores[i] == target then
count = count + 1 // add 1 on each match
endif
next i
print("Appears " + str(count) + " times") // Appears 2 timesscores = [12, 5, 10, 6, 7, 12]
target = 12
count = 0 # how many times the target appears
for i in range(len(scores)):
if scores[i] == target:
count += 1 # increase the count on each match
print("Appears", count, "times") # Appears 2 timesp, write players[p], NOT players[i].range(6) gives 0-5.players.length returns the number of elements. To loop from 0, use for i = 0 to players.length - 1.scores = [3, 6, 6, 9, 2, 8] → scores[2] = 6 (third element, 0-indexed). Remember arrays are ZERO-INDEXED.count = count + 1 INSIDE an if. Work through the array that already holds the data: don't input new values. Any attempt at a loop and at an if earns marks, so always try.else: print("Not found") inside the loop prints 'Not found' once for every item that doesn't match.array scores[6]: for i = 0 to 5, if scores[i] >= 80 then count = count + 1, endif, next i. The counter i is the array INDEX, so each pass checks a different element.Ready to test yourself?
Practise 2.2.3 questions