A2.2.
2 1D Arrays
Task Pseudocode Phython code
Need to declare an Array in order to
DECLARE <identifier> : use it. The following code could be
1. Declare an ARRAY[<lbound>:<ubound>] OF <datatype> used to declare an array with a
Array Example: particular size.
DECLARE MyList:ARRAY[1:10] OF INTEGER MyList = [0]*4
MyList = [“” for i in range(5)]
MyList=[0]*4
FOR Index ← LowerBound To UpperBound
2. Initialize an
MyArray[Index] ← 0 for n in range(4):
Array
ENDFOR MyList[n]=0
MyList = [i for i in range(1,6)]
3. Input data to MyList = ["A", "B", "C"]
INPUT MyArray[Index]
an Array MyList[n]= input()
MyList=["A","B","C","D"]
for n in range(4):
FOR Index ← LowerBound To UpperBound print( MyList[n])
4. Output data
OUTPUT MyArray[Index]
from an Array
ENDFOR
Print(MyList)
5. linear Search FOUND←False
MyList=["A","B","C","D"]
INPUT SearchVal
Found = False
FOR Index ← LowerBound To UpperBound
SearchVal = input()
IF MyArray[Index] = SearchVal THEN
for n in range(4):
OUTPUT “Item found”
if MyList[n]== SearchVal:
FOUND ←True
print(“item found at”, n)
EXIT FOR
Found =True
ENDIF
break
ENDFOR
IF FOUND=FALSE THEN
if Found ==False:
OUTPUT(“Item not found”)
print("item not found")
ENDIF
FOUND←False MyList=["A","B","C","D"]
Index←1 Found = False
INPUT SearchVal Index=0
WHILE Index<= LENGTH(MyArray] AND SearchVal = input("pls enter a value
Found=False to search")
IF MyArray[Index] = SearchVal THEN while Index< =len(MyList)-1 and
OUTPUT “Item found” Found==False:
FOUND ←True if MyList[Index]== SearchVal:
ELSE print("item found at", Index)
Index←Index+1 Found =True
ENDIF else:
ENDWHILE Index=Index+1
1
IF FOUND=False THEN
if Found ==False:
OUTPUT(“Item not found”)
print("item not found")
ENDIF
Swapped←True MyList = [4,2,3,10,5,1]
WHILE Swapped =True swapped = True
Swapped←False Temp =0
FOR Index ←LowerBound To UpperBound-1 while swapped == True:
IF MyArray[Index]>MyArray[Index+1] swapped = False
Temp←MyArray[Index] for n in range(5):
6. Bubble Sort MyArray[Index] ←MyArray[Index+1] if MyList[n]>MyList[n+1] :
MyArray[Index+1] ←Temp Temp = MyList[n]
Swapped ←True MyList[n]= MyList[n+1]
ENDIF MyList[n+1]=Temp
ENDFOR swapped = True
UpperBound=UpperBound-1
ENDWHILE print(MyList)
Past paper questions
9618/22/O/N/23
2 An algorithm will:
1. input a sequence of integer values, one at a time
2. ignore all values until the value 27 is input, then sum the remaining values in the sequence
3. stop summing values when the value 0 is input and then output the sum of the values.
(a) Design a pseudocode to represent the algorithm.
9618/22/M/J/21
6 A procedure CountVowels() will:
• be called with a string containing alphanumeric characters as its parameter
• count and output the number of occurrences of each vowel (a, e, i, o, u) in the string
• count and output the number of occurrences of the other alphabetic characters (as a single
total).
The string may contain both upper and lower case characters. Each count value will be stored in a unique element
of a global 1D array CharCount of type INTEGER. The array will contain six elements. Write pseudocode for the
procedure CountVowels(). [8]
9618/21/M/J/24
4 A global 1D array Data contains 100 elements of type integer. A function Check() will:
• total the element values in odd index locations (1, 3, 5 ... 97, 99)
• total the element values in even index locations (2, 4, 6 ... 98, 100)
• return one of three strings ‘Odd’, ‘Even’ or ‘Same’ to indicate which total is the greater, or
whether the totals are the same.
Write pseudocode for the function Check(). [6]
2
October 2023 – 23 Question 5
October 2022-22 Question 7a and 7b
October 2022-21 Question 5
October 2021 – 22 Question 1b
June 2021 – 22 Question 8