10.2 Arrays
AS Level · 8 questions found
What this topic covers
Section titled “What this topic covers”- Array terminology: index, upper and lower bound
- Choose 1D or 2D array for a given task; write pseudocode for both
- Bubble sort in pseudocode; linear search in pseudocode
Past paper questions
Section titled “Past paper questions” Q4


4
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]
BLANK PAGE
Show mark scheme
4
Example:
FUNCTION Check() RETURNS STRING
DECLARE Odd, Even, Index : INTEGER
Odd
0
Even
0
FOR Index
1 TO 100
IF Index MOD 2 = 0 THEN
Even
Even + Data[Index]
ELSE
Odd
Odd + Data[Index]
ENDIF
NEXT Index
ENDFUNCTION
Mark as follows:
1. Function heading, ending and return type
2. Declare local variables
and
as integers
Odd, Even
Index
3. Initialise
and
Odd
Even
4. Loop for 100
// through array
iterations
5. Sum
and
element values
in a loop
Odd
Even
6. Compare
and
after the loop and Return appropriate string
Odd
Even
FUNCTION Check() RETURNS STRING
DECLARE Odd, Even, Index : INTEGER
Odd
0
Even
0
FOR Index
1 TO 100
IF Index MOD 2 = 0 THEN
Even
Even + Data[Index]
ELSE
Odd
Odd + Data[Index]
ENDIF
NEXT Index
ENDFUNCTION
Mark as follows:
1. Function heading, ending and return type
2. Declare local variables
and
as integers
Odd, Even
Index
3. Initialise
and
Odd
Even
4. Loop for 100
// through array
iterations
5. Sum
and
element values
in a loop
Odd
Even
6. Compare
and
after the loop and Return appropriate string
Odd
Even
Q5


5
A global 1D array of strings contains three elements which are assigned values as shown:
Data[1]
"aaaaaa"
Data[2]
"bbbbbb"
Data[3]
"cccccc"
Procedure Process() manipulates the values in the array.
The procedure is written in pseudocode as follows:
PROCEDURE Process(Format : STRING)
DECLARE Count, Index, L : INTEGER
DECLARE Result : STRING
DECLARE C : CHAR
Result
"****"
FOR Count
1 TO LENGTH(Format) STEP 2
C
MID(Format, Count, 1)
L
STR_TO_NUM(MID(Format, Count + 1, 1))
Index
(Count + 1) DIV 2
CASE OF C
'X' : Result
TO_UPPER(Data[Index])
'Y' : Result
TO_LOWER(Data[Index])
'Z' : Result
"**" & Data[Index]
ENDCASE
Data[Index]
LEFT(Result, L)
NEXT Count
ENDPROCEDURE
(a) Complete the trace table by dry running the procedure when it is called as follows:
CALL Process("X3Y2W4")
Count
C
L
Index
Result
Data[1]
Data[2]
Data[3]
[6]
(b) The procedure is to be modified. If variable C is assigned a value other than 'X', 'Y' or 'Z',
then procedure Error() is called and passed the value of variable C as a parameter.
This modification can be implemented by adding a single line of pseudocode.
(i) Write the single line of pseudocode.
..................................................................................................................................... [1]
(ii) State where this new line should be placed.
..................................................................................................................................... [1]
Show mark scheme
5(a) [6 marks]
One mark per zone.
OTHERWISE : CALL Error(C)
OTHERWISE : CALL Error(C)
5(b)(ii) [6 marks]
After the '
' clause in the
construct // before the
Z
CASE
ENDCASE
FUNCTION IsRA(x1, y1, x2, y2, x3, y3 : INTEGER) RETURNS
' clause in the
construct // before the
Z
CASE
ENDCASE
FUNCTION IsRA(x1, y1, x2, y2, x3, y3 : INTEGER) RETURNS
Q5


5
A program is being designed in pseudocode.
The program contains a global 1D array Data of type string containing 200 elements.
The first element has the index value 1.
A procedure Process() is written to initialise the values in the array:
PROCEDURE Process(Label : STRING)
DECLARE Index : INTEGER
Index
0
INPUT Data[Index]
WHILE Index < 200
Index
Index + 1
CASE OF (Index MOD 2)
0 : Data[Index]
TO_UPPER(Label)
1 : Data[Index]
TO_LOWER(Label)
OTHERWISE : OUTPUT "Alarm 1201"
ENDCASE
NEXT Index
OUTPUT "Completed " & Index & " times"
ENDPROCEDURE
(a) (i) The pseudocode contains two syntax errors and one other error.
Identify the errors.
Syntax error 1 ....................................................................................................................
Syntax error 2 ....................................................................................................................
Other error .........................................................................................................................
[3]
(ii) The procedure contains a statement that is not needed.
Identify the pseudocode statement and explain why it is not needed.
Statement ..........................................................................................................................
Explanation .......................................................................................................................
[2]
(b) After correcting all syntax errors, the pseudocode is translated into program code which
compiles without generating any errors.
When the program is executed it unexpectedly stops responding.
Identify the type of error that has occurred.
............................................................................................................................................. [1]
Show mark scheme
5(a)(i) [2 marks]
One mark per error:
Syntax:
1.
(should be
NEXT Index
ENDWHILE)
2. '&' used to concatenate an integer (in
statement)
OUTPUT
Other:
3.
Accesses element outside range // Accesses element 0
Syntax:
1.
(should be
NEXT Index
ENDWHILE)
2. '&' used to concatenate an integer (in
statement)
OUTPUT
Other:
3.
Accesses element outside range // Accesses element 0
5(a)(ii)
One mark per point:
Statement:
The
statement
OTHERWISE
Explanation:
The result of MOD 2 can only be 0 or 1
Statement:
The
statement
OTHERWISE
Explanation:
The result of MOD 2 can only be 0 or 1
5(b) [7 marks]
Run-time
Q2


2
(a) A program uses a global 1D array of type string and a text file.
An algorithm that forms part of the program is expressed as follows:
•
copy the first line from the file into the first element of the array
•
copy the second line from the file into the second element of the array
•
continue until all lines in the file have been copied into the array.
Stepwise refinement is applied to the algorithm.
Outline five steps for this algorithm that could be used to produce pseudocode.
Assume there are more elements in the array than lines in the file.
Do not use pseudocode statements in your answer.
Step 1 .......................................................................................................................................
Step 2 .......................................................................................................................................
Step 3 .......................................................................................................................................
Step 4 .......................................................................................................................................
Step 5 .......................................................................................................................................
[5]
(b) Sequence is one programming construct.
Identify one other programming construct that will be required when the algorithm from
part (a) is converted into pseudocode and explain its use.
Construct ..................................................................................................................................
Use ...........................................................................................................................................
[2]
BLANK PAGE
,
,
,
,
Show mark scheme
2(a)
One mark per point:
1. Open the file (in read mode and subsequently close)
2. Initialise an index variable (to 1 // 0)
3. Repeat (the next three steps) until the end of file is reached
4. Read a line from the file (into a string variable)
5. Store the line / variable in the array at the index
6. Increment the index
in a loop
Note: max 5 marks
1. Open the file (in read mode and subsequently close)
2. Initialise an index variable (to 1 // 0)
3. Repeat (the next three steps) until the end of file is reached
4. Read a line from the file (into a string variable)
5. Store the line / variable in the array at the index
6. Increment the index
in a loop
Note: max 5 marks
2(b) [3 marks]
One mark per point:
Construct: a conditional loop
Use: To keep repeating until the
end of the file is reached
ALTERNATIVE:
Construct: a selection statement
Use: To test / check the value returned by the
function
EOF()
Construct: a conditional loop
Use: To keep repeating until the
end of the file is reached
ALTERNATIVE:
Construct: a selection statement
Use: To test / check the value returned by the
function
EOF()
Q3


3
A record structure is declared to hold data relating to components being produced in a factory:
TYPE Component
DECLARE Item_ID : STRING
DECLARE Reject : BOOLEAN
DECLARE Weight : REAL
ENDTYPE
The factory normally produces a batch (or set) of 1000 components at a time. A global array is
declared to store 1000 records for a batch:
DECLARE Batch : ARRAY [1:1000] OF Component
Two global variables contain the minimum and maximum acceptable weight for each component.
The values represent an inclusive range and are declared as:
DECLARE Min, Max : REAL
(a) (i) A program uses a variable ThisIndex as the array index to access a record.
Write a pseudocode clause to check whether or not the weight of an individual component
is within the acceptable range.
..................................................................................................................................... [3]
(ii) When batches of less than 1000 components are processed, it is necessary to indicate
that certain elements in the array are unused.
Suggest how an unused array element could be indicated.
..................................................................................................................................... [1]
(b) A module InRange() will:
•
be called with an integer parameter representing an index value of a record in the Batch
array
•
check if the weight of the indexed component is within the acceptable range
•
return TRUE if the weight is in the range and FALSE if it is not.
A module BatchCheck() will:
•
iterate through a batch of 1000 component records
•
call module InRange() to check each individual component record
•
keep a count of the number of components that fail
•
output a suitable warning message and immediately stop if the number of failed
components exceeds 5.
Complete the program flowchart to represent the algorithm for module BatchCheck().
START
END
Is
Index = 1001 ?
Yes
No
[5]
,
,
,
,
Show mark scheme
3(a)(i)
Example solution using AND
IF Batch[ThisIndex].Weight >= Min AND
Batch[ThisIndex].Weight <= Max THEN
Alternative solution using OR
IF Batch[ThisIndex].Weight < Min OR
Batch[ThisIndex].Weight > Max THEN
Mark as follows:
1. Reference to
Batch[ThisIndex].Weight
2. A valid
check for one boundary
3. A valid check for other boundary with correct logic operator
IF Batch[ThisIndex].Weight >= Min AND
Batch[ThisIndex].Weight <= Max THEN
Alternative solution using OR
IF Batch[ThisIndex].Weight < Min OR
Batch[ThisIndex].Weight > Max THEN
Mark as follows:
1. Reference to
Batch[ThisIndex].Weight
2. A valid
check for one boundary
3. A valid check for other boundary with correct logic operator
3(a)(ii) [5 marks]
One mark for either:
Set the
field to an empty string / NULL / invalid value
Item_ID
Set
to <= 0 / zero
Weight
Set the
field to an empty string / NULL / invalid value
Item_ID
Set
to <= 0 / zero
Weight
3(b) [6 marks]
ne 1
Zo
ne 2
Zo
ne 3
Zo
ne 4
Zo
ne
5
Zo
One mark per zone
Zo
ne 2
Zo
ne 3
Zo
ne 4
Zo
ne
5
Zo
One mark per zone
Q5


5
A program is being designed in pseudocode.
The program contains the following declaration:
DECLARE Data : ARRAY[1:1000] OF STRING
A procedure ArrayInitialise() is written to initialise the values in the array:
PROCEDURE ArrayInitialise(Label : STRING)
DECLARE Index : INTEGER
Index
1
WHILE Index <= 1000
CASE OF (Index MOD 2)
0 : Data[Index]
FormatA(Label)
Index
Index + 1
1 : Data[Index]
FormatB(Label)
Index
Index + 1
ENDCASE
ENDWHILE
ENDPROCEDURE
Functions FormatA() and FormatB() apply fixed format case changes to the parameter string.
(a) The design of the procedure does not use the most appropriate loop construct.
Suggest a more appropriate construct that could be used and explain your choice.
Construct ..................................................................................................................................
Explanation ...............................................................................................................................
[2]
(b) The algorithm calls one of the functions FormatA() and FormatB() each time within the
loop.
Explain why this is not efficient and suggest a more efficient solution.
............................................................................................................................................. [4]
BLANK PAGE
,
,
,
,
Show mark scheme
5(a) [4 marks]
One mark per point
Count-controlled loop
the number of iterations is known
Count-controlled loop
the number of iterations is known
5(b) [6 marks]
Two mark for Statement of Problem:
1
The functions will return the same value every time they are called
2
... because
/ the parameter value does not change within the loop
Label
Two marks for Solution:
3
Assign
and
to two (local) variables
FormatA(Label)
FormatB(Label)
before the loop
4
Use the (new) variables in place of the function calls / in the loop //
Replace the references to
and
in the
FormatA()
FormatB()
CASE
clauses with the new variable names
1
The functions will return the same value every time they are called
2
... because
/ the parameter value does not change within the loop
Label
Two marks for Solution:
3
Assign
and
to two (local) variables
FormatA(Label)
FormatB(Label)
before the loop
4
Use the (new) variables in place of the function calls / in the loop //
Replace the references to
and
in the
FormatA()
FormatB()
CASE
clauses with the new variable names
Q1




1
A program needs to take integer numbers as input, sort the numbers and then search for a specific
number.
(a) The integer numbers will be stored in the global 1D array, DataStored, with space for up to
20 integers.
The global variable NumberItems stores the quantity of items the array contains.
Write program code to declare DataStored and NumberItems.
Save your program as Question1_J24.
Copy and paste the program code into part 1(a) in the evidence document.
[1]
(b) The procedure Initialise():
•
prompts the user to input the quantity of numbers the user would like to enter
•
reads the input and validates it is between 1 and 20 (inclusive)
•
prompts the user to input each number and stores each number in DataStored.
Write program code for Initialise().
Save your program.
Copy and paste the program code into part 1(b) in the evidence document.
[5]
(c) The main program stores 0 in NumberItems, calls Initialise() and then outputs the
contents of DataStored.
(i) Write program code for the main program.
Save your program.
Copy and paste the program code into part 1(c)(i) in the evidence document.
[2]
(ii) Test your program by inputting the following data in the order given:
30
5
3
9
4
1
2
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot into part 1(c)(ii) in the evidence document.
[2]
(d) The procedure BubbleSort() uses a bubble sort to sort the data in DataStored into
ascending numerical order.
(i) Write program code for BubbleSort().
Save your program.
Copy and paste the program code into part 1(d)(i) in the evidence document.
[4]
(ii) Write program code to amend the main program to call BubbleSort() and then output
the contents of DataStored.
Save your program.
Copy and paste the program code into part 1(d)(ii) in the evidence document.
[1]
(iii) Test your program by inputting the following data in the order given:
5
3
9
4
1
2
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot into part 1(d)(iii) in the evidence document.
[1]
(e) The function BinarySearch():
•
takes the integer parameter DataToFind to search for in the array
•
performs an iterative binary search on the array DataStored
•
returns the index where DataToFind is found in DataStored. If DataToFind is not
found, the function returns −1.
(i) Write program code for the iterative function BinarySearch().
Save your program.
Copy and paste the program code into part 1(e)(i) in the evidence document.
[6]
(ii) Write program code to amend the main program to:
•
take a number as input from the user
•
call BinarySearch() with the number input
•
output the value returned from the function call as its parameter.
Save your program.
Copy and paste the program code into part 1(e)(ii) in the evidence document.
[3]
(iii) Test your program twice with the following inputs:
Test 1: 5 1 6 2 8 10 2
Test 2: 5 1 6 2 8 10 7
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot into part 1(e)(iii) in the evidence document.
[2]
Show mark scheme
1(a) [1 mark]
1 mark for:
Declaration of (global) array with identifier
DataStored
(Integer and 20 spaces)
and
NumberItems
(Integer)
public static Integer[] DataStored = new Integer[20];
public static Integer NumberItems= 0;
VB.NET
Dim DataStored(19) As Integer
Dim NumberStored As Integer = 0
Python
global DataStored #integer
global NumberItems #Integer 20 items
Declaration of (global) array with identifier
DataStored
(Integer and 20 spaces)
and
NumberItems
(Integer)
public static Integer[] DataStored = new Integer[20];
public static Integer NumberItems= 0;
VB.NET
Dim DataStored(19) As Integer
Dim NumberStored As Integer = 0
Python
global DataStored #integer
global NumberItems #Integer 20 items
1(b)
Python
def Initialise():
global DataStored
global NumberItems
Valid = False
while(Valid == False):
NumberItems = int(input("How many numbers will you enter?")) #loop until < 20
if NumberItems > 0 and NumberItems< 21:
Valid = True
for Count in range(0, NumberItems):
DataStored.append(int(input("Enter number")))
def Initialise():
global DataStored
global NumberItems
Valid = False
while(Valid == False):
NumberItems = int(input("How many numbers will you enter?")) #loop until < 20
if NumberItems > 0 and NumberItems< 21:
Valid = True
for Count in range(0, NumberItems):
DataStored.append(int(input("Enter number")))
1(c)(i) [2 marks]
1 mark each:
Storing
0
in
NumberItems
and
then calling
Initialise()
Outputting all contents of array
DataStored
public static Integer NumberItems= 0;
Initialise();
for(Integer X = 0; X < NumberItems; X++){
System.out.println(DataStored[X]);
VB.NET
NumberItems = 0
Initialise()
For X = 0 To NumberItems - 1
Console.WriteLine(DataStored(X))
Python
NumberItems = 0
Initialise()
print(DataStored)
Storing
0
in
NumberItems
and
then calling
Initialise()
Outputting all contents of array
DataStored
public static Integer NumberItems= 0;
Initialise();
for(Integer X = 0; X < NumberItems; X++){
System.out.println(DataStored[X]);
VB.NET
NumberItems = 0
Initialise()
For X = 0 To NumberItems - 1
Console.WriteLine(DataStored(X))
Python
NumberItems = 0
Initialise()
print(DataStored)
1(c)(ii) [2 marks]
1 mark each
Output showing quantity entered twice (30 and 5) with first being invalid
Array output 3 9 4 1 2
Output showing quantity entered twice (30 and 5) with first being invalid
Array output 3 9 4 1 2
1(d)(i)
Python
def BubbleSort():
global DataStored
global NumberItems
for Count in range(0, NumberItems):
for Count2 in range(0, NumberItems-1):
if DataStored[Count2] > DataStored[Count]:
DataStored[Count2], DataStored[Count] = DataStored[Count],
DataStored[Count2]
def BubbleSort():
global DataStored
global NumberItems
for Count in range(0, NumberItems):
for Count2 in range(0, NumberItems-1):
if DataStored[Count2] > DataStored[Count]:
DataStored[Count2], DataStored[Count] = DataStored[Count],
DataStored[Count2]
1(d)(ii) [1 mark]
1 mark for calling
BubbleSort()
and
outputting array contents after
VB.NET
BubbleSort()
For X = 0 To NumberStored - 1
Console.WriteLine(DataStored(X))
e.g. Java
BubbleSort();
for(Integer X = 0; X < NumberItems; X++){
System.out.println(DataStored[X]);
e.g. Python
BubbleSort()
print(DataStored)
BubbleSort()
and
outputting array contents after
VB.NET
BubbleSort()
For X = 0 To NumberStored - 1
Console.WriteLine(DataStored(X))
e.g. Java
BubbleSort();
for(Integer X = 0; X < NumberItems; X++){
System.out.println(DataStored[X]);
e.g. Python
BubbleSort()
print(DataStored)
1(d)(iii) [1 mark]
1 mark for screenshot showing the inputs and the values in the correct order
1(e)(i)
VB.NET
Function BinarySearch(DataToFind)
Dim First As Integer = 0
Dim Last As Integer = NumberItems
Dim MidValue As Integer
While (First <= Last)
MidValue = (First + Last) / 2
If DataToFind = DataStored(MidValue) Then
Return MidValue
End If
If DataToFind < DataStored(MidValue) Then
Last = MidValue - 1
Else
First = MidValue + 1
End If
End While
Return -1
End Function
Python
def BinarySearch(DataToFind):
global DataStored
global NumberItems
First = 0
Last= NumberItems
while(First <= Last):
MidValue = int((First + Last) / 2)
if DataToFind == DataStored[MidValue]:
return MidValue
if DataToFind < DataStored[MidValue]:
Last = MidValue - 1
else:
First = MidValue + 1
return -1
Function BinarySearch(DataToFind)
Dim First As Integer = 0
Dim Last As Integer = NumberItems
Dim MidValue As Integer
While (First <= Last)
MidValue = (First + Last) / 2
If DataToFind = DataStored(MidValue) Then
Return MidValue
End If
If DataToFind < DataStored(MidValue) Then
Last = MidValue - 1
Else
First = MidValue + 1
End If
End While
Return -1
End Function
Python
def BinarySearch(DataToFind):
global DataStored
global NumberItems
First = 0
Last= NumberItems
while(First <= Last):
MidValue = int((First + Last) / 2)
if DataToFind == DataStored[MidValue]:
return MidValue
if DataToFind < DataStored[MidValue]:
Last = MidValue - 1
else:
First = MidValue + 1
return -1
1(e)(ii) [3 marks]
1 mark each:
Taking number as input
… calling
BinarySearch
with input
Outputting value returned
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number to find");
Integer Search = Integer.parseInt(scanner.nextLine());
System.out.println(BinarySearch(Search));
VB.NET
Console.WriteLine("Enter a number to find")
Dim Search As Integer = Console.ReadLine()
Console.WriteLine(BinarySearch(Search))
Python
Search = int(input("Enter a number to find"))
print(BinarySearch(Search))
Taking number as input
… calling
BinarySearch
with input
Outputting value returned
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number to find");
Integer Search = Integer.parseInt(scanner.nextLine());
System.out.println(BinarySearch(Search));
VB.NET
Console.WriteLine("Enter a number to find")
Dim Search As Integer = Console.ReadLine()
Console.WriteLine(BinarySearch(Search))
Python
Search = int(input("Enter a number to find"))
print(BinarySearch(Search))
1(e)(iii) [2 marks]
1 mark for each test
Test 1 – Accept found in index 16
Test 1 – Accept found in index 16
Q1




1
A program needs to take integer numbers as input, sort the numbers and then search for a specific
number.
(a) The integer numbers will be stored in the global 1D array, DataStored, with space for up to
20 integers.
The global variable NumberItems stores the quantity of items the array contains.
Write program code to declare DataStored and NumberItems.
Save your program as Question1_J24.
Copy and paste the program code into part 1(a) in the evidence document.
[1]
(b) The procedure Initialise():
•
prompts the user to input the quantity of numbers the user would like to enter
•
reads the input and validates it is between 1 and 20 (inclusive)
•
prompts the user to input each number and stores each number in DataStored.
Write program code for Initialise().
Save your program.
Copy and paste the program code into part 1(b) in the evidence document.
[5]
(c) The main program stores 0 in NumberItems, calls Initialise() and then outputs the
contents of DataStored.
(i) Write program code for the main program.
Save your program.
Copy and paste the program code into part 1(c)(i) in the evidence document.
[2]
(ii) Test your program by inputting the following data in the order given:
30
5
3
9
4
1
2
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot into part 1(c)(ii) in the evidence document.
[2]
(d) The procedure BubbleSort() uses a bubble sort to sort the data in DataStored into
ascending numerical order.
(i) Write program code for BubbleSort().
Save your program.
Copy and paste the program code into part 1(d)(i) in the evidence document.
[4]
(ii) Write program code to amend the main program to call BubbleSort() and then output
the contents of DataStored.
Save your program.
Copy and paste the program code into part 1(d)(ii) in the evidence document.
[1]
(iii) Test your program by inputting the following data in the order given:
5
3
9
4
1
2
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot into part 1(d)(iii) in the evidence document.
[1]
(e) The function BinarySearch():
•
takes the integer parameter DataToFind to search for in the array
•
performs an iterative binary search on the array DataStored
•
returns the index where DataToFind is found in DataStored. If DataToFind is not
found, the function returns −1.
(i) Write program code for the iterative function BinarySearch().
Save your program.
Copy and paste the program code into part 1(e)(i) in the evidence document.
[6]
(ii) Write program code to amend the main program to:
•
take a number as input from the user
•
call BinarySearch() with the number input
•
output the value returned from the function call as its parameter.
Save your program.
Copy and paste the program code into part 1(e)(ii) in the evidence document.
[3]
(iii) Test your program twice with the following inputs:
Test 1: 5 1 6 2 8 10 2
Test 2: 5 1 6 2 8 10 7
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot into part 1(e)(iii) in the evidence document.
[2]
Show mark scheme
1(a) [1 mark]
1 mark for:
Declaration of (global) array with identifier
DataStored
(Integer and 20 spaces)
and
NumberItems
(Integer)
public static Integer[] DataStored = new Integer[20];
public static Integer NumberItems= 0;
VB.NET
Dim DataStored(19) As Integer
Dim NumberStored As Integer = 0
Python
global DataStored #integer
global NumberItems #Integer 20 items
Declaration of (global) array with identifier
DataStored
(Integer and 20 spaces)
and
NumberItems
(Integer)
public static Integer[] DataStored = new Integer[20];
public static Integer NumberItems= 0;
VB.NET
Dim DataStored(19) As Integer
Dim NumberStored As Integer = 0
Python
global DataStored #integer
global NumberItems #Integer 20 items
1(b)
Python
def Initialise():
global DataStored
global NumberItems
Valid = False
while(Valid == False):
NumberItems = int(input("How many numbers will you enter?")) #loop until < 20
if NumberItems > 0 and NumberItems< 21:
Valid = True
for Count in range(0, NumberItems):
DataStored.append(int(input("Enter number")))
def Initialise():
global DataStored
global NumberItems
Valid = False
while(Valid == False):
NumberItems = int(input("How many numbers will you enter?")) #loop until < 20
if NumberItems > 0 and NumberItems< 21:
Valid = True
for Count in range(0, NumberItems):
DataStored.append(int(input("Enter number")))
1(c)(i) [2 marks]
1 mark each:
Storing
0
in
NumberItems
and
then calling
Initialise()
Outputting all contents of array
DataStored
public static Integer NumberItems= 0;
Initialise();
for(Integer X = 0; X < NumberItems; X++){
System.out.println(DataStored[X]);
VB.NET
NumberItems = 0
Initialise()
For X = 0 To NumberItems - 1
Console.WriteLine(DataStored(X))
Python
NumberItems = 0
Initialise()
print(DataStored)
Storing
0
in
NumberItems
and
then calling
Initialise()
Outputting all contents of array
DataStored
public static Integer NumberItems= 0;
Initialise();
for(Integer X = 0; X < NumberItems; X++){
System.out.println(DataStored[X]);
VB.NET
NumberItems = 0
Initialise()
For X = 0 To NumberItems - 1
Console.WriteLine(DataStored(X))
Python
NumberItems = 0
Initialise()
print(DataStored)
1(c)(ii) [2 marks]
1 mark each
Output showing quantity entered twice (30 and 5) with first being invalid
Array output 3 9 4 1 2
Output showing quantity entered twice (30 and 5) with first being invalid
Array output 3 9 4 1 2
1(d)(i)
Python
def BubbleSort():
global DataStored
global NumberItems
for Count in range(0, NumberItems):
for Count2 in range(0, NumberItems-1):
if DataStored[Count2] > DataStored[Count]:
DataStored[Count2], DataStored[Count] = DataStored[Count],
DataStored[Count2]
def BubbleSort():
global DataStored
global NumberItems
for Count in range(0, NumberItems):
for Count2 in range(0, NumberItems-1):
if DataStored[Count2] > DataStored[Count]:
DataStored[Count2], DataStored[Count] = DataStored[Count],
DataStored[Count2]
1(d)(ii) [1 mark]
1 mark for calling
BubbleSort()
and
outputting array contents after
VB.NET
BubbleSort()
For X = 0 To NumberStored - 1
Console.WriteLine(DataStored(X))
e.g. Java
BubbleSort();
for(Integer X = 0; X < NumberItems; X++){
System.out.println(DataStored[X]);
e.g. Python
BubbleSort()
print(DataStored)
BubbleSort()
and
outputting array contents after
VB.NET
BubbleSort()
For X = 0 To NumberStored - 1
Console.WriteLine(DataStored(X))
e.g. Java
BubbleSort();
for(Integer X = 0; X < NumberItems; X++){
System.out.println(DataStored[X]);
e.g. Python
BubbleSort()
print(DataStored)
1(d)(iii) [1 mark]
1 mark for screenshot showing the inputs and the values in the correct order
1(e)(i)
VB.NET
Function BinarySearch(DataToFind)
Dim First As Integer = 0
Dim Last As Integer = NumberItems
Dim MidValue As Integer
While (First <= Last)
MidValue = (First + Last) / 2
If DataToFind = DataStored(MidValue) Then
Return MidValue
End If
If DataToFind < DataStored(MidValue) Then
Last = MidValue - 1
Else
First = MidValue + 1
End If
End While
Return -1
End Function
Python
def BinarySearch(DataToFind):
global DataStored
global NumberItems
First = 0
Last= NumberItems
while(First <= Last):
MidValue = int((First + Last) / 2)
if DataToFind == DataStored[MidValue]:
return MidValue
if DataToFind < DataStored[MidValue]:
Last = MidValue - 1
else:
First = MidValue + 1
return -1
Function BinarySearch(DataToFind)
Dim First As Integer = 0
Dim Last As Integer = NumberItems
Dim MidValue As Integer
While (First <= Last)
MidValue = (First + Last) / 2
If DataToFind = DataStored(MidValue) Then
Return MidValue
End If
If DataToFind < DataStored(MidValue) Then
Last = MidValue - 1
Else
First = MidValue + 1
End If
End While
Return -1
End Function
Python
def BinarySearch(DataToFind):
global DataStored
global NumberItems
First = 0
Last= NumberItems
while(First <= Last):
MidValue = int((First + Last) / 2)
if DataToFind == DataStored[MidValue]:
return MidValue
if DataToFind < DataStored[MidValue]:
Last = MidValue - 1
else:
First = MidValue + 1
return -1
1(e)(ii) [3 marks]
1 mark each:
Taking number as input
… calling
BinarySearch
with input
Outputting value returned
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number to find");
Integer Search = Integer.parseInt(scanner.nextLine());
System.out.println(BinarySearch(Search));
VB.NET
Console.WriteLine("Enter a number to find")
Dim Search As Integer = Console.ReadLine()
Console.WriteLine(BinarySearch(Search))
Python
Search = int(input("Enter a number to find"))
print(BinarySearch(Search))
Taking number as input
… calling
BinarySearch
with input
Outputting value returned
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number to find");
Integer Search = Integer.parseInt(scanner.nextLine());
System.out.println(BinarySearch(Search));
VB.NET
Console.WriteLine("Enter a number to find")
Dim Search As Integer = Console.ReadLine()
Console.WriteLine(BinarySearch(Search))
Python
Search = int(input("Enter a number to find"))
print(BinarySearch(Search))
1(e)(iii) [2 marks]
1 mark for each test
Test 1 – Accept found in index 16
Test 1 – Accept found in index 16