Pakistan International School Jeddah - English Section
Cambridge International AS & A Level Computer Science (9618)
A2-CS Programming Task-3
Program 1
A student is developing an algorithm to search through a 1D array of 10
elements. Each element of the array, Result, contains a REAL value.
Result = [78,65,0,23,55,0,80,76,54,25]
The algorithm will output:
• the average value of all the elements
• the number of elements with a value of zero.
The structured English description of the algorithm is:
1. SET Total value to 0
2. SET Zero count to 0
3. SELECT the first element
4. ADD value of element to Total value
5. IF element value is 0 then INCREMENT Zero count
6. REPEAT from step 4 for next element, until element is last element
7. SET Average to Total / 10
8. OUTPUT a suitable message and Average
9. OUTPUT a suitable message and Zero count
Write Python Program for this algorithm.
Design a Trace Table for the above program
Page 1 of 6
#TotalValue : Real
#ZeroCount: Integer
TotalValue = 0
ZeroCount = 0
Result = [78,65,0,23,55,0,80,76,54,25]
for index in range (0, 10):
TotalValue = TotalValue + Result[index]
if (Result[index] == 0.0):
ZeroCount = ZeroCount + 1
print("The average is ", (TotalValue / 10))
print("The number of elements with a zero value is ",ZeroCount)
Output Average,
totalValue zeroCount index Result[index]
noZeros
78 0 0 78
143 0 1 65
143 1 2 0
166 1 3 23
221 1 4 55
221 2 5 0
301 2 6 80
377 2 7 76
431 2 8 54
456 2 9 25 45.6, 2
Page 2 of 6
Program 2
A function, ProcessMarks(), is required to analyse test marks for a class
of students.
• There are 10 students in the class.
• A mark is between 0 and 100.
• The marks for the class are stored in an array, Mark, which has 10
elements.
• The array is passed to the function as a parameter.
• The function will output a message stating the average and highest
marks. For example: "The average mark is 34 and the highest mark is
76"
• The function returns the subscript of the highest mark.
Write program code to implement the ProcessMarks() function.
Using Python: You should show a comment statement for each
variable used with its data type.
Design a Trace Table for the above program
Page 3 of 6
def ProcessMarks(mark):
#highest, i, position, total : integer
#average : real
highest = mark[0]
total = 0
position = 0
for i in range(0,10):
total = total + mark[i]
if mark[i] > highest:
highest = mark[i]
position = i
average = total/10
print('The average mark is ', str(average) + ' and the h
ighest mark is ' + str(highest))
return position
mark = [42,73,99,21,46,58,83,74,16,26]
ProcessMarks(mark)
Page 4 of 6
Output
highest total position i mark[i]
Average, highest,position
42 42 0 0 42
73 115 1 1 73
99 214 2 2 99
99 235 2 3 21
99 281 2 4 46
99 339 2 5 58
99 422 2 6 83
99 496 2 7 74
99 512 2 8 16
99 538 2 9 26 53.8 , 99 , 2
Program 3
A function, ValidatePassword(), is required to validate a password as
TRUE if it consists of more than 1 lower and upper cases and more than
2 digits
def ValidatePassword(regnum):
lCaseChar = 0
uCaseChar = 0
numChar = 0
returnFlag = True
for n in range(0, len(regnum)):
Page 5 of 6
nextChar = regnum[n]
if nextChar >= 'a' and nextChar <= 'z':
lCaseChar = lCaseChar + 1
else:
if nextChar >= 'A' and nextChar <= 'Z':
uCaseChar = uCaseChar + 1
else:
if nextChar >= '0' and nextChar <= '9':
numChar = numChar + 1
else:
returnFlag = False
if (lCaseChar >= 2 and uCaseChar >= 2 and numChar >= 3):
returnFlag = True
else:
returnFlag = False
print("Invalid Password...")
return returnFlag
regnum = input("Enter any string:")
print(ValidatePassword(regnum))
Page 6 of 6