0% found this document useful (0 votes)
197 views

10.2 As Arrays Data Structure

Inqilab Patel is an experienced computer teacher who has taught at several schools in Karachi, Pakistan. He is currently pursuing his MPhil in computer studies. Patel maintains a website to provide educational resources for students studying computer science. He regularly contributes materials to support teachers and has received training in innovative teaching methods.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
197 views

10.2 As Arrays Data Structure

Inqilab Patel is an experienced computer teacher who has taught at several schools in Karachi, Pakistan. He is currently pursuing his MPhil in computer studies. Patel maintains a website to provide educational resources for students studying computer science. He regularly contributes materials to support teachers and has received training in innovative teaching methods.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 82

About the developer of this workbook

Inqilab Patel is an O & A Level Computer Teacher. Currently he is teaching A & O Level Computer Science
at The City School PAF Chapters, Hira Foundation School and Intellect. He has taught in many other
schools including Yaqeen Model School, Karachi Cadet School, KN Academy, Hexis A Level, Verge and
Nakhlah Boys Campus Society. Cambridge has selected him as a Member of Cambridge Editorial
Review Board. He is also associated with Aga Khan University Examination Board in the capacity of
Chief Examiner, Item Writer, E-Marker, Karachi Board of Secondary Education the capacity of
Deputy Head Examiner and Sindh Board of Technical Education.

His entire career path revolves around computer science; either he was a student or a teacher.
He got a chance to polish his skills of teaching and studying more about computers at various
levels which has given him great confidence in presenting himself for any senior level position of
transferring his knowledge to the youth.

He has not stopped; he is continuing with his education at the higher levels. It is his second
semester of MPhil computer studies from a well-known university of Pakistan; The Institute of
Business & Technology.

Inqilab Patel knows a lot of methods of teaching computers and has developed tutorial notes,
worksheets and assignments for my students. He also maintains a website
(www.inqilabpatel.com) which is specifically designed for the support of those who want to excel
in GCSE computer science. He also regularly contributes material to CIE teacher support website,
for which he receives appreciation from different people across the world.

He has also received various training in innovative and special methods of teaching this subject.

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


10.2 Arrays: Data Structure
Syllabus requirements
The Cambridge International AS & A Level syllabus (9618) requires candidates to understand and
use both one-dimensional and two-dimensional arrays.
Arrays
Arrays are data structure used to store multiple data items of same data type under one identifier name.
Arrays are considered to be fixed-length structures of elements of identical data type, accessible by
consecutive index (subscript) numbers. It is good practice to explicitly state what the lower bound of the array
(i.e. the index of the first element) is because this defaults to either 0 or 1 in different systems. Generally, a
lower bound of 1 will be used.
Square brackets are used to indicate the array indices.
Each element in the array is identified using its subscript or index number. The largest and smallest index
numbers are called the upper bound and lower bound of the array.
Example
StudentName[30] StudentName[1:30] StudentName[0:29]

For illustration, let's take array declaration to store marks of 10 students.


Marks[10] Marks[1:10] Marks[0:9]
After storing values in array

As per the above illustration, following are the important points to be considered.
Index starts with 1.
Array length is 10 which means it can store 10 elements.
Each element can be accessed via its index. For example, we can fetch an element at index 6 as 19.

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


COMPUTER SCIENCE WITH INQILAB PATEL
Declaring arrays
Arrays are considered to be fixed-length structures of elements of identical data type, accessible by
consecutive index (subscript) numbers. It is good practice to explicitly state what the lower bound of the array
(i.e. the index of the first element) is because this defaults to either 0 or 1 in different systems.
Generally, a lower bound of 1 will be used.
Square brackets are used to indicate the array indices.
A One-dimensional array is declared as follows:
DECLARE <identifier> : ARRAY[<lower>:<upper>] OF <data type>
A two-dimensional array is declared as follows:
DECLARE <identifier> : ARRAY[<lower1>:<upper1>,<lower2>:<upper2>] OF <data
type>

Example – array declaration


DECLARE StudentNames : ARRAY[1:30] OF STRING
DECLARE NoughtsAndCrosses : ARRAY[1:3,1:3] OF CHAR
Using arrays
Array index values may be literal values or expressions that evaluate to a valid integer value.
Example – Accessing individual array elements
StudentNames[1] ← "Ali"
NoughtsAndCrosses[2,3] ← ꞌXꞌ
StudentNames[n+1] ← StudentNames[n]
Arrays can be used in assignment statements (provided they have same size and data type). The following is
therefore allowed:

Example – Accessing a complete array


SavedGame ← NoughtsAndCrosses
A statement should not refer to a group of array elements individually. For example, the following construction
should not be used.
StudentNames [1 TO 30] ← ""

Instead, an appropriate loop structure is used to assign the elements individually. For example:
The terms associated with Arrays
Name: The identifier of the array is called Array Name. E.g. StudentName[]
Element: Each data item stored in arrayis called element. Array can store only single types of elements.
Size: The number elements the array can store. E.g. StudentName[1:30] can store 30 names while
StudentName[30] can store 31 names as by default it is 0 to 30.
Index: The position of each element is referred as Index Number. Index of Abdullah in array example is 1.
Type: Data type of all elements in a single array have same data types.

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


Dimension: Dimension is the organisational structure of array. It may be 1D that has single index or 2D that
has two indices.

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


COMPUTER SCIENCE WITH INQILAB PATEL
1D Array:
An array with single index is referred as 1D Array. It may consist of a single row or a single column.
To Declare 1D Array only upper and lower bound of rows or columns are mentioned.

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


A One-dimensional array is declared as follows:
DECLARE <identifier> : ARRAY[<lower>:<upper>] OF <data type>
Example - To declare 1D Array to store 30 name of type string
DECLARE StudentName[1:30] : STRING

Using arrays
Array index values may be literal values or expressions that evaluate to a valid integer value.
Example – Accessing individual array elements
StudentNames[1] ← "Ali"
StudentNames[n+1] ← StudentNames[n]

Example - To input and store data in specific location in arrays like at 20:
INPUT Name[20]

Example – assigning a group of array elements


FOR Index ← 1 TO 30
StudentNames[Index] ← ""
NEXT Index
Example - To input and store values in whole arrays
FOR Index 1 TO 30
INPUT Name[Index]
NEXT Index

Example -To output content of specific location line 20


OUTPUT Name[20]

Example - To output all contents of arrays


FOR Index 1 TO 30
OUTPUT Name[Index]
NEXT Index

Arrays can be used in assignment statements (provided they have same size and data type). The following is
therefore allowed:

Example – Accessing a complete array


SavedGame ← NoughtsAndCrosses

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


COMPUTER SCIENCE WITH INQILAB PATEL
A statement should not refer to a group of array elements individually. For example, the following construction
should not be used.
StudentNames [1 TO 30] ← ""
Instead, an appropriate loop structure is used to assign the elements individually. For example:

Example – assigning a group of array elements


FOR Index ← 1 TO 30
StudentNames[Index] ← ""
NEXT Index

Arrays in Python
Different programming languages have different statements for initialising the array but they all do the
same thing. In Python List is used as Arrays and declaration statement is:
Name = [“ “] * 30
This statement declares:
the identifier name is Name
the lower bound is 0 while upper bound is 29, means 30 is not inclusive
the data type is String. [ ] for string, [0] for int, [0.0] for float(real), [True] or [False] for
boolean
The upper bound of 29 specifies that there can be a maximum of 30 data items, since Python
starts with a subscript of zero. We do not have to fill the array; the upper bound of 30
indicates the maximum size.
The array that has been described is one-dimension array so far is really only a list of single data
items. It is possible to have an array which can be visualised as a two-dimensional table with rows
and columns and a data value in each cell.
Reading data into an array
To assign data values to the elements of the array, we do this with assignment statements such as:
Name[6] = “Patel”
This places the string Patel at index position 6 in the array.
Similarly, the following statement places the string Rashid at index position 3 in the array.
Name[3] = “Rashid”

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


Linear Search
Linear Search - is a method in which each element of the array is checked in order,
from the lower bound to the upper bound, until the item is found or the upper bound is
reached.
For example, to search marks in an 1D array Marks[1:30], user has to input data to be searched.
Algorithm compares this search data with all elements of the array, starting from lower bound. If
search data matches with the element then it displays the location of data first found and if data is not
found, it is searched at next location in the conditional loop until the data is found or it reaches at the
upper bound.
Example Code for Single Searching using Linear Search
DECLARE SearchData: REAL
DECLARE Found: BOOLEAN
DECLARE MaxIndex : INTEGER
INPUT SearchData
Found FALSE
MaxIndex 30
Index 1
WHILE Index <=30 AND Found = FALSE
IF SearchData = Marks[Index]
THEN
Found TRUE
ELSE
Index Index + 1
ENDIF
ENDWHILE
IF Found = TRUE
THEN
OUTPUT “Found at”, Index
ELSE
OUTPUT “Not found”
ENDIF

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


COMPUTER SCIENCE WITH INQILAB PATEL
Example Code for Multiple Searching, using Linear Search
To search and output all locations where the search data is found, a count controlled loop is
used instead of conditional loop.
DECLARE SearchData: REAL
DECLARE Found: BOOLEAN
DECLARE MaxIndex : INTEGER
INPUT SearchData
Found FALSE
FOR Index 1 TO 30
IF SearchData = Marks[Index]
THEN
Found TRUE
OUTPUT Index
ENDIF
NEXT Index
IF Found = FALSE
THEN
OUTPUT “not found.”
ENDIF

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


Bubble sort – is a method of sorting data in an array into alphabetical or numerical
order by comparing adjacent items and swapping them if they are in the wrong order.
Each element of the array is compared with the next element and swapped if the elements are in the
wrong order, starting from the lower bound and finishing with the element next to the upper bound.
The element at the upper bound is now in the correct position. This comparison is repeated with one
less element in the list, until there is only one element left or no swaps are made.

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


COMPUTER SCIENCE WITH INQILAB PATEL
DELCARE Swapped: BOOLEAN
DECLARE MaxIndex, UpperBound: INTEGER
DELCARE Temp: INTEGER
Swapped TRUE
UpperBound 5
MaxIndex UpperBound - 1
WHILE Swapped = TRUE
Swapped FALSE
FOR Index 1 TO MaxIndex
IF Marks[Index] > Marks[Index + 1]
THEN
Temp Marks[Index]
Marks[Index] Marks[Index + 1]
Marks[Index + 1] Temp
Swapped TRUE
ENDIF
NEXT Index
MaxIndex MaxIndex – 1
ENDWHILE

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


2D Arrays
An array that has two indices is known as 2D arrays.
In 2D arrays data is stored in many rows and columns. There is need of two indices, one for row and
other for columns. Like:

TestMarks[] is an 1D Array, as it has only one index (i.e. for rows)


ExamMarks[] is a 2D Array, as it has two indices, one index for rows, and the second one for column.
2D arrays are accessed using two nested loops.
The outer loop is for rows. The loop counter is mostly ‘J’ or Row.
The inner loop is for columns. The loop counter is mostly ‘K’ or Column.

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


COMPUTER SCIENCE WITH INQILAB PATEL
Work Example:
Write down pseudo code that:
Declare a 2D arrays ExamMarks to store marks of 5 students in 3 subjects.
Input and store marks in the arrays
Output the marks.
DECLARE ExamMarks : ARRAYS[1:5, 1:3] OF REAL
DECLARE Row, Column : INTEGER

FOR Row 1 TO 5
FOR Column 1 TO 3
INPUT ExamMarks[Row, Column]
NEXT Column
NEXT Row

FOR Row 1 TO 5
FOR Column 1 TO 3
PRINT ExamMarks[Row,Column]
NEXT Column
NEXT Row

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


Assignment
Q 1a) Declare an array called Marks whose index values ranges from 0 to 999 and whose element
type is integer

.........................................................................................................................................................[1]

b) Initialise the array declared in part a, all values in the array should be equals to 0.
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.......................................................................................................................................................... [2]
Q 2) Consider the following code:
DECLARE NextChar : ARRAY[1:30] OF CHAR

Write a code to store the letter ‘A’ at 1st and ‘Z’ at 26th location of the Array.

.................................................................................................................................................................
.......................................................................................................................................................... [2]
Q3a) Write a pseudo code that uses an array to store marks of 10 students of a class. Enter marks of
each student. After input all the marks output the list of marks.
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
...........................................................................................................................................................[4]

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


COMPUTER SCIENCE WITH INQILAB PATEL
b) Calculate the average marks of the class by traversing the array.

.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
........................................................................................................................................................... [3]
c) Output the smallest and the greatest marks of the class by traversing the array.

.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
........................................................................................................................................................... [3]
d) Create a second array to input and store name of students of the class. Output the list of name of
students and their marks by traversing the two arrays. At the end of list print average marks of the
class.

.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
............................................................................................................................ [4]

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


Q 4a) Write a program to input name and telephone of your friends in two one dimension arrays.
Each array can store up to 5 elements.

..................................................................................................................
..................................................................................................................
..................................................................................................................
..................................................................................................................
.................................................................................................................
.................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
..........................................................................................................................................................[3]

b) Write a program to display 1st and 3rd telephone number stored in array.
.................................................................................................................................................................
........……….......................................................................................................................................... [2]

c) Write a program to print list of all telephone numbers stored in array.


.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
........................................................................................................................................................... [3]
d) Write a program that inputs the name of your friend search in the array and output telephone
number.

.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
............................................................................................................................................................ [3]

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


COMPUTER SCIENCE WITH INQILAB PATEL
e) After the user selects a name, give him option to display and change the telephone number in the
array. After change of telephone number, output the entire list.

.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
...........................................................................................................................................................[4]
Q 5a ) Declare an array to input and store 5 integers from user. Then
declare another array and copy data items from 1 st array into 2nd
array.
............................................................................................................
............................................................................................................
............................................................................................................
.................. .........................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
............................................................................................................................................................[3]
Q 5b ) Declare 3rd array and copy data items from 1st array into 3rd array in reverse order.
.............................................................................................................
..............................................................................................................
...............................................................................................................
..............................................................................................................
...............................................................................................................
...............................................................................................................
......................……………………………………………………………………………………………………
…………………........................................................................................................................................
.................................................................................................................................................................
................................................................................................................[3]

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


Q 6) An array has 10 integers. Create two new arrays and copy 1 st 5 integers from 1st array into 2nd
array and last integers in 3rd array.
..........................................................................................................
.........................................................................................................
.........................................................................................................
........................................................................................................
..........................................................................................................
..........................................................................................................
..........................................................................................................
.........................................................................................................
.........................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
........................................................................................................................ [3]

Random Number Generator

RAND(x : INTEGER) RETURNS REAL


returns a real number in the range 0 to x (not inclusive of x)
Example: RAND(87) could return 35.43
To create generate a number between 1 to 10
RAND (10) generates a number between 0 to 9.999999 for example 7.6754
1 will be added in this generated value to obtain numbers between 1 and 10.99999
(RAND(10)+1)
Then this number is converted into integer using INT Functions, so the range become between 1
and 10.
INT(RAND(10) + 1)

Q 7a) Write pseudocode to show how the RAND() function can be used to generate a single integer
in the range 1 to 150.
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
........................................................................................................................................................... [3]

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


COMPUTER SCIENCE WITH INQILAB PATEL
b) Write an algorithm to generate and store 10 random integers between 1 to 50 in a 1D array
MyNum.
................................................................................................................................................................................
........................................................ ....................................................................................................................
................................................................................................................................................................................
................................................................................................................................................................................
................................................................................................................................................................................
................................................................................................................................................................................
................................................................................................................................................................................
................................................................................................................................................................................
......................................................................................................................................................[4]
c) Declare another array to store 10 integers then store random numbers between 1 to 50 in the array
using RAND() functions. All random numbers must be unique.
................................................................................................................................................................................
................................................................................................................................................................................
................................................................................................................................................................................
................................................................................................................................................................................
................................................................................................................................................................................
................................................................................................................................................................................
......................................................................................................................................................[4]
Assignment Answer Key

DECLARE Marks: ARRAY[0:999] OF INTEGER [1]

DECLARE Index : INTEGER


FOR Index 0 TO 999
Marks[Index] 0
NEXT Index

DECLARE NextChar : ARRAY[1:30] OF CHAR

NextChar[1] ‘A’
NextChar[26] ‘Z’ [2]

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


DECLARE Index : INTEGER
FOR Index 1 TO 30
INPUT Marks[Index]
NEXT Index
FOR Index 1 TO 30
OUTPUT Marks[Index]
NEXT Index [4]
b) Calculate the average marks of the class by traversing the array.

DECLARE Sum, Average : REAL


Sum 0
FOR Index 1 TO 30
Sum Sum + Marks[Index]
NEXT Index
Average Sum / 30
PRINT Sum, Average [3]

c) Output the smallest and the greatest marks of the class by traversing the array. [3]
DECLARE Smallest, Greatest: INTEGER
Smallest 9999
Greatest 0
FOR Index 1 TO 30
IF Marks[Index]> Greatest THEN Greatest Marks[Index]
IF Marks[Index] < Smallest THEN Smallest Marks[Index]
NEXT Index
OUTPUT Greatest, Smallest

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


COMPUTER SCIENCE WITH INQILAB PATEL
d) Create a second array to input and store name of students of the class. Output the list of name of
students and their marks by traversing the two arrays. At the end of list print average marks of the
class. [4]
DECLARE Name: ARRAY[1:30] OF STRING
FOR Index 1 TO 30
INPUT Name[Index]
NEXT Index
FOR Index 1 TO 30
PRINT Name[Index], Marks[Index]
NEXT Index

Q 4a) Write a program to input name and telephone of your friends in two
one dimension arrays. Each array can store up to 5 elements. [3]

DECLARE Name : ARRAY[1:5] OF STRING


DECLARE Tel : ARRAY[1:5] OF STRING
DECLARE Count : INTEGER

FOR Count 1 TO 5
INPUT Name[Count], Tel[Count]
NEXT Count

b) Write a program to display 1st and 3rd telephone number stored in array. [2]
OUTPUT Tel[1], Tel[3]

c) Write a program to print list of all telephone numbers stored in array. [3]
FOR Count 1 TO 5
OUTPUT Tel[Count]
NEXT Count
d) Write a program that inputs the name of your friend search in the array and output telephone number. [3]

DECLARE Found: BOOLEAN


DECLARE SearchName: STRING
INPUT SearchName
FOR Count 1 TO 5
IF SearchName=Name[Count]
THEN
OUTPUT Tel[Count]
Found True
ENDIF
NEXT Count
IF Found = False

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


THEN
PRINT “Not Found”
ENDIF

e) After the user selects a name, give him option to display and change the telephone number in the array.
After change of telephone number, output the entire list.

DECLARE Choice : STRING


INPUT SearchName
FOR Count 1 TO 5
IF SearchName=Name[Count]
THEN
OUTPUT Tel[Count]
PRINT “Do you want to change it (Yes/No)”
INPUT Choice
IF Choice=TRUE
THEN
PRINT “Enter new telephone number”
INPUT Tel[Count]
ENDIF
Found True
ENDIF
NEXT Count
IF Found = False
THEN
PRINT “Not Found”
ENDIF
FOR Count 1 TO 5
PRINT Name[Count], Tel[Count]
NEXT Count [4]

DECLARE Num1 : ARRAY[1:5] OF INTEGER


DECLARE Num2 : ARRAY[1:5] OF INTEGER
DECLARE Count : INTEGER

FOR Count 1 TO 5
INPUT Num1[Count]
NEXT Count
FOR Count 1 TO 5
Num2[Count] Num1[Count]
NEXT Count [3]

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


COMPUTER SCIENCE WITH INQILAB PATEL
DECLARE Num3 : ARRAY[1:5] OF INTEGER
Count3 5
FOR Count 1 TO 5
Num3[Count3] Num1[Count]
Count3=Count3-1
NEXT Count
Q 6) An array has 10 integers. Create two new arrays and copy 1st 5 integers from 1st array into 2nd array and
last 5 integers in 3rd array.
DECLARE Num2 : ARRAY[1:5] OF INTEGER
DECLARE Num3 : ARRAY[1:5] OF INTEGER
FOR Count 1 TO 5
Num2[Count] Num1[Count]
Num3[Count] Num1[Count+5]
NEXT Count [3]

Q 7a) Write pseudocode to show how the RAND() function can be


used to generate a single integer in the range 1 to 150. [3]
INT(RAND(150)+1)

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


b) Declare an array to store 10 integers then store random numbers between 1 to 50 in the array
using RAND() functions. [4]
DECLARE Num : ARRAY[1:10] OF INTEGER
DECLARE Count : INTEGER
FOR Count 1 TO 10
Num[Count] INT(RAND(50)+1)
NEXT Count

c) Declare another array to store 10 integers then store random numbers between 1 to 50 in the array
using RAND() functions. All random numbers must be unique.
................................................................................................................................................................................
................................................................................................................................................................................
................................................................................................................................................................................
................................................................................................................................................................................
................................................................................................................................................................................
................................................................................................................................................................................
......................................................................................................................................................[4]
Q 8) John works in a supermarket, where he is given a task to find which item has the highest price
and which item has the lowest price. There are 900 items in the supermarket.
Declare suitable arrays to store name and price of each product.
Input price of each product with its name.
Output the number of items which have a price greater than 100and number of items which have
price less than 50.
Output the highest and the lowest price.
…………………………………………………….........................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
......................................................................................................................................................... [4]

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


COMPUTER SCIENCE WITH INQILAB PATEL
(b) The following pseudocode assigns a value to an element of an array:
ThisArray[n] ← 42
Complete the following table by writing the answer for each row. [3]
Answer
The number of dimensions of ThisArray
The technical terms for minimum and maximum values that the
variable n may take
The technical term for the variable n in the pseudocode statement

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


1a 9618 S21 P21
5 A global 2D array Result of type INTEGER is used to store a list of exam candidate numbers
together with their marks. The array contains 2000 elements, organised as 1000 rows and 2 columns.
Column 1 contains the candidate number and column 2 contains the mark for the corresponding
candidate. All elements contain valid exam result data.
A procedure Sort() is needed to sort Result into ascending order of mark using an efficient bubble sort
algorithm. Write pseudocode for the procedure Sort().
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
.................................................................................................................................................... [8]

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


COMPUTER SCIENCE WITH INQILAB PATEL

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


9618 S 21 P22
5 (a) A student is learning about arrays.
She wants to write a program to:
• declare a 1D array RNum of 100 elements of type INTEGER
• assign each element a random value in the range 1 to 200 inclusive
• count and output how many numbers generated were between 66 and 173 inclusive.
(i) Write pseudocode to represent the algorithm.
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
..................................................................................................................................... [6]
(ii) The student decides to modify the algorithm so that each element of the array will contain a
unique value.
Describe the changes that the student needs to make to the algorithm.
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
..................................................................................................................................... [3]

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


COMPUTER SCIENCE WITH INQILAB PATEL
(b) The following is a pseudocode function.
Line numbers are given for reference only.
01 FUNCTION StringClean(InString : STRING) RETURNS STRING
02
03 DECLARE NextChar : CHAR
04 DECLARE OutString : STRING
05 DECLARE Counter : INTEGER
06
07 OutString ““
08
09 FOR Counter 1 TO LENGTH(InString)
10 NextChar MID(InString, Counter, 1)
11 NextChar LCASE(NextChar)
12 IF NOT((NextChar < 'a') OR (NextChar > 'z')) THEN
13 OutString OutString & NextChar
14 ENDIF
15 NEXT Counter
16
17 RETURN OutString
18
19 ENDFUNCTION
(i) Examine the pseudocode and complete the following table. [4]
Answer
Give a line number containing an example of an initialisation statement.
Give a line number containing the start of a repeating block of code.
Give a line number containing a logic operation.
Give the number of parameters to the function MID().

(ii) Write a simplified version of the statement in line 12.


...........................................................................................................................................
..................................................................................................................................... [2]

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/
COMPUTER SCIENCE WITH INQILAB PATEL
9618 W21 P23
2 (a) An algorithm to sort a 1D array into ascending order is described as follows:
• move the largest value to the end
• keep repeating until the array is sorted.
Apply the process of stepwise refinement to this algorithm in order to produce a more detailed
description.
Write the more detailed description using structured English. Your explanation of the algorithm should
not include pseudocode statements.
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
............................................................................................................................................. [6]

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


(b) The program flowchart shown describes a simple algorithm.

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


COMPUTER SCIENCE WITH INQILAB PATEL
Write pseudocode for the simple algorithm shown on previous page.
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
........................................................................................................................................................... [6]

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


Winter 21 P22
3 A programmer is writing a program to help manage clubs in a school.
Data will be stored about each student in the school and each student may join up to three clubs.
The data will be held in a record structure of type Student.
The programmer has started to define the fields that will be needed as shown in the following table.
Field Typical value Comment
StudentID "CF1234" Unique to each student
Email "[email protected]" Contains letters, numbers and certain symbols
Club_1 1 Any value in the range 1 to 99 inclusive
Club_2 14 Any value in the range 1 to 99 inclusive
Club_3 27 Any value in the range 1 to 99 inclusive
(a) (i) Write pseudocode to declare the record structure for type Student.
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
..................................................................................................................................... [3]

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


COMPUTER SCIENCE WITH INQILAB PATEL
(ii) A 1D array Membership containing 3000 elements will be used to store the student data.
Write pseudocode to declare the Membership array.
...........................................................................................................................................
..................................................................................................................................... [2]
(iii) Some of the elements of the array will be unused.
Give an appropriate way of indicating an unused array element.
...........................................................................................................................................
..................................................................................................................................... [1]
(iv) Some students are members of less than three clubs.
State one way of indicating an unused club field.
...........................................................................................................................................
..................................................................................................................................... [1]
(b) A procedure GetIDs() will:
• prompt and input the number of a club
• output the StudentID of all the students who are members of that club
• output a count of all students in the given club.
Write pseudocode for the procedure GetIDs().
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
............................................................................................................................................. [7]

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/
COMPUTER SCIENCE WITH INQILAB PATEL
Summer 2019 P21
5 (a) Programming languages usually contain a range of built-in functions, such as a random number
generator.
State three advantages of using built-in functions.
1 ................................................................................................................................................
2 ................................................................................................................................................
3 ................................................................................................................................................[3]
(b) A student is learning about random number generation.
She is investigating how many times the random function needs to be called before every number in
a given series is generated.
She is using pseudocode to develop a procedure, TestRand(), which will:
• use the random number function to generate an integer value in the range 1 to 50 inclusive
• count how many times the random function needs to be called before all 50 values have been
generated
• output a message giving the number of times the random function was called.
Write pseudocode for the procedure TestRand(). [8]

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/
COMPUTER SCIENCE WITH INQILAB PATEL
Q 8) John works in a supermarket, where he is given a task to find which item has the highest price
and which item has the lowest price. There are 900 items in the supermarket.
Declare suitable arrays to store name and price of each product.
Input price of each product with its name.
Output the number of items which have a price greater than 100 and number of items which have
price less than 50.
Output the highest and the lowest price.
…………………………………………………….........................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
......................................................................................................................................................... [4]

5 A company creates two new websites, Site X and Site Y, for selling bicycles.
Various programs are to be written to process the sales data.
These programs will use data about daily sales made from Site X (using variable SalesX) and Site Y
(using variable SalesY).
Data for the first 28 days is shown below.

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


(a) Name the data structure to be used in a program for SalesX.

...............................................................................................................................................[2]

(b) The programmer writes a program from the following pseudocode design.
x 0
FOR DayNumber 1 TO 7
IF SalesX[DayNumber] + SalesY[DayNumber] >= 10
THEN
X X + 1
OUTPUT SalesDate[DayNumber]
ENDIF
NEXT dayNumber
OUTPUT x
Trace the execution of this pseudocode by completing the trace table below. [4]
x DayNumber OUTPUT
0

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


COMPUTER SCIENCE WITH INQILAB PATEL
5 A program is to process a set of integers.
The integers are stored in an array, Num. The first N elements are to be processed.
The pseudocode for this program is shown below:
FOR i ← 1 TO (N - 1)
j ← 1
REPEAT
IF Num[j] >Num[j + 1]
THEN
Temp ← Num[j]
Num[j] ← Num[j + 1]
Num[j + 1] ← Temp
ENDIF
j ← j + 1
UNTIL j = (N – i + 1)
NEXT i
(a) (i) Trace the execution of the pseudocode for the value N = 5 and the given array of integers.[8]
Num
N i J Temp 1 2 3 4 5
5 11 16 13 7 8

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


(ii) State the purpose of the algorithm.
............................................................................................................................................................[1]
(iii) Describe what evidence from the trace table suggests that the given pseudocode is inefficient.
............................................................................................................................................................[1]
(b) Complete the identifier table documenting the use of each of the variables. [5]
Identifier Data type Description
Num ARRAY[1:100] OF INTEGER The array of numbers.
N
i
J
Temp

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


COMPUTER SCIENCE WITH INQILAB PATEL
5 A firm employs workers who assemble amplifiers. Each member of staff works an agreed number
of hours each day.
The firm records the number of completed amplifiers made by each employee each day.
Management monitor the performance of all its workers.
Production data was collected for 3 workers over 4 days.

A program is to be written to process the production data.


(a) The production data is to be stored in a 2-dimensional array ProductionData, declared as follows:

DECLARE ProductionDataARRAY[1:4, 1:3] : INTEGER


(i) Describe two features of an array.
1 ........................................................................................................................................
...........................................................................................................................................
2 ........................................................................................................................................
.......................................................................................................................................[2]
(ii) Give the value of ProductionData[3, 2].
.......................................................................................................................................[1]
(iii) Describe the information produced by the expression:
ProductionData[2, 1] + ProductionData[2, 2] + ProductionData[2, 3]
...........................................................................................................................................
.......................................................................................................................................[2]

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


(b) Complete the trace table for the pseudocode algorithm below.
FOR WorkerNum 1 TO 3
WorkerTotal[WorkerNum] 0
NEXT WorkerNum
FOR WorkerNum 1 TO 3
FOR DayNum 1 TO 4
WorkerTotal[WorkerNum] WorkerTotal[WorkerNum] +
ProductionData[DayNum, WorkerNum]
NEXT DayNum
NEXT WorkerNum
FOR WorkerNum 1 TO 3
WorkerAverage WorkerTotal[WorkerNum]/(4 * DailyHoursWorked[WorkerNum])
IF WorkerAverage< 2 THEN
OUTPUT “Investigate“, WorkerNum
ENDIF
NEXT WorkerNum [8]
WorkerTotal
WorkerNum DayNum WorkerAverage OUTPUT 1 2 3

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


COMPUTER SCIENCE WITH INQILAB PATEL
(c) An experienced programmer suggests that the pseudocode would be best implemented as a
procedure AnalyseProductionData.
Assume that both arrays, DailyHoursWorked and ProductionData, are available to the procedure from
the main program and they are of the appropriate size.
PROCEDURE AnalyseProductionData(NumDays : INTEGER, NumWorkers : INTEGER)
DECLARE .............................................
DECLARE .............................................
DECLARE .............................................
DECLARE .............................................
FOR WorkerNum 1 TO 3
WorkerTotal[WorkerNum] 0
NEXT WorkerNum
FOR WorkerNum 1 TO 3
FOR DayNum 1 TO 4
WorkerTotal[WorkerNum] WorkerTotal[WorkerNum]+ProductionDat
a[DayNum, WorkerNum]
NEXT DayNum
NEXT WorkerNum
FOR WorkerNum 1 TO 3
WorkerAverage WorkerTotal[WorkerNum]/(4 *
DailyHoursWorked[WorkerNum])
IF WorkerAverage< 2
THEN
OUTPUT "Investigate", WorkerNum
ENDIF
NEXT WorkerNum
ENDPROCEDURE
(i) Complete the declaration statements showing the local variables.
(ii) T
Circle all the places in the original pseudocode where changes will need to be made.
Write the changes which need to be made next to each circle. [3]

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


(iii) Write the statement for a procedure call which processes data for 7 days for 13 workers.
.......................................................................................................................................[1]

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


COMPUTER SCIENCE WITH INQILAB PATEL
3 A string encryption function is needed. The encryption uses a simple character-substitution method.
In this method, a new character substitutes for each character in the original string. This will create
the encrypted string.
The substitution uses the 7-bit ASCII value for each character. This value is used as an index for a1D
array, Lookup, which contains the substitute characters.
Lookup contains an entry for each of the ASCII characters. It may be assumed that the original string
and the substitute characters are all printable.

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


For example:

The programmer writes a function, EncryptString, to return the encrypted string. This function will
receive two parameters, the original, PlainText string and the 1D array.

(a) The first attempt at writing the pseudocode for this function is shown below.
Complete the pseudocode.
FUNCTION EncryptString(.............................) RETURNS STRING
DECLARE .................... , ............... : CHAR
DECLARE OldCharValue : ..........................
DECLARE n : INTEGER
DECLARE OutString : STRING
........................ //initialise the return string
//loop through PlainText to produce OutString
FOR n 1 TO ................. //from first to last character
OldChar ...................//get next character
OldCharValue ..............//find the ASCII value
NewChar ...................//look up substitute character
.............................//concatenate to OutString
NEXT n
.............................................................
ENDFUNCTION [10]
(b) Additional code needs to be written to allow the user to change some of the characters in the
array Lookup.
The user will input:

At the end, the program will finally output a confirmation message.


The first version of the algorithm is represented by the flowchart on the following page.
(i) Write pseudo code to declare the array Lookup.

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


COMPUTER SCIENCE WITH INQILAB PATEL
(ii) Write program code to implement the flowchart design.
In addition to the Lookup array, assume that the following variables have been declared:
StartPos, NumToChange, n, NewChar [6]

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/
COMPUTER SCIENCE WITH INQILAB PATEL

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


Winter 2016 P22
2 You will need to refer to the list of pseudocode string-handling functions in the Appendix.
(a) Give the value of the variables x, y and z for the following sequence of statements.
(i) x ONECHAR("Barcelona", 5) x ............................ [1]
(ii) y ONECHAR("Cool", 1) & ONECHAR("Ball", 2) & "t-food" y ............................ [1]
(iii) Temp1 "13"
Temp2 ONECHAR("One-2-One", 5)
z TONUM(Temp2 & Temp1) z ............................ [1]
A computer program is to simulate the reading and processing of a string of characters from an input
device. The character string consists of:

aracter.
A typical input character sequence, stored as InputString is:
13*156*9*86*1463*18*#

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


COMPUTER SCIENCE WITH INQILAB PATEL
Study this pseudocode.
01 DECLARE Numbers ARRAY [1:100] OF INTEGER
02 DECLARE InputString : STRING
03 DECLARE NextChar : CHAR
04 DECLARE NextNumberString : STRING
05 DECLARE i : INTEGER // Numbers array index
06 DECLARE j : INTEGER // InputString index
07
08 OUTPUT "String ... "
09 INPUT InputString
10 j 1
11 NextChar ONECHAR(InputString, j)
12
13 i 1
14 WHILE NextChar <> '#'
15 NextNumberString = ""
16 WHILE NextChar <> '*'
17 NextNumberString NextNumberString & NextChar
18 j j + 1
19 NextChar ONECHAR(InputString, j)
20 ENDWHILE
21
22 // store the next integer to the array
23 Numbers[i] TONUM(NextNumberString)
24 i i + 1
25 j j + 1
26 NextChar ONECHAR(InputString, j)
27 ENDWHILE
28
29 CALL DisplayArray()
(b) Write the line number for:
(i) A statement which declares a global variable used to store a single character. ........... [1]
(ii) A statement which runs code written as a procedure. ........... [1]
(iii) -
(iv) A statement which increments a variable. ........... [1]

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


(c) Copy the condition which is used to control the inner loop.
.............................................................................................................................................. [1]
(d) (i) Complete the trace table below for the given pseudocode as far as line 27.
The input string is: 23*731*5*# [5]
Numbers
i j NextChar NextNumberString
1 2 3
1 1 '2'
""
"2"
2 '3' "23"
3 '*' 23

(ii) Explain what this algorithm does.


...........................................................................................................................................
...................................................................................................................................... [2]

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


COMPUTER SCIENCE WITH INQILAB PATEL
2 A 1D array, ClassName, of type STRING contains 100 elements.
The following pseudocode represents a simple algorithm to process the array.
DECLARE SearchValue : STRING
DECLARE FoundFlag : BOOLEAN
DECLARE Index : INTEGER
INPUT SearchValue
FoundFlag FALSE
Index 1
WHILE Index < 101 AND FoundFlag = False
IF ClassName[Index] = SearchValue
THEN
OUTPUT Index
FoundFlag TRUE
ENDIF
Index Index + 1
ENDWHILE
IF FoundFlag = FALSE
THEN
OUTPUT "Not found"
ENDIF
(a) Describe the purpose of the algorithm.
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
.............................................................................................................................................. [2]

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


(b) Draw a program flowchart to represent this algorithm.
Note that variable declarations are not required in program flowcharts. [9]

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


COMPUTER SCIENCE WITH INQILAB PATEL
3 A 1D array, Product, of type STRING is used to store information about a range of products in a
shop. There are 100 elements in the array. Each element stores one data item.
The format of each data item is as follows:
<ProductID><ProductName>
-character string of numerals
-length string
The following pseudocode is an initial attempt at defining a procedure, ArraySort, which will perform a
bubble sort on Product. The array is to be sorted in ascending order of ProductID.
Line numbers have been added for identification purposes only.
01 PROCEDURE SortArray
02 DECLARE Temp : CHAR
03 DECLARE FirstID, SecondID : INTEGER
04 FOR I 1 TO 100
05 FOR J 2 TO 99
06 FirstID MODULUS(LEFT(Product[J], 6))
07 SecondID MODULUS(LEFT(Product[J + 1], 6))
08 IF FirstID>SecondID
09 THEN
10 Temp Product[I]
11 Product[I] Product[J + 1]
12 Product[J + 1] Temp
13 NEXT
14 ENDIF
15 ENDFOR
16 ENDPROCEDURE

The pseudocode on page 8 contains a number of errors. Complete the following table to show:

Note:
ore than one line, you should only refer to it ONCE.
[8]

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


Line
Error Correction
number
01 Wrong procedure name PROCEDURE ArraySort

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


COMPUTER SCIENCE WITH INQILAB PATEL

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/
COMPUTER SCIENCE WITH INQILAB PATEL
4 A program controls a chemical process in a factory.
The temperature is monitored as part of the control process. The temperature is measured at fixed
time intervals and the value is stored in an array, PTemp. The array contains 100 elements,
representing 100 temperature values. The first element is PTemp[1].
The program will check whether the temperature is outside the acceptable range more than 20times.
This task is performed by a function, IsTempOK().
The algorithm for the function IsTempOK() is expressed in structured English as follows:
1. Examine each array element and count the number of times that a temperature is less than
MinTemp or more than MaxTemp.
2. If the count in step 1 exceeds 20, return FALSE, otherwise return TRUE.
Draw a program flowchart, on the next page, to represent the algorithm for the functionIsTempOK().
Assume:
the array contains 100 valid temperature values
PTemp, MinTemp and MaxTemp are global variables.
Note that variable declarations are not required in program flowcharts.

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/
COMPUTER SCIENCE WITH INQILAB PATEL
3 (c) A program needs to search through 1000 elements of an unsorted array to find a given value.
The program will output:

Outline the steps the program needs to follow.


Do not write pseudocode or program code.
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.........................................................................................................................................................[4]

(d) ResultArray is a 1D array of type STRING. It contains 100 elements.


Write program code to declare ResultArray and set all elements to the value "NO DATA".
Programming language .............................................................................................................
Program code
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.............................................................................................................................................................[3]

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


3 (a) A student is developing an algorithm to search through a 1D array of 100 elements. Each
element of the array, Result, contains a REAL value.
The algorithm will output:
e of all the elements

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 / 100
8. OUTPUT a suitable message and Average
9. OUTPUT a suitable message and Zero count
Write pseudocode for this algorithm.
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
............................................................................................................................................. [7]
(b) The student decides to change the algorithm and implement it as a procedure, ScanArray(),
which will be called with three parameters.
ScanArray(AverageValue, ZeroCount, ArrayName)
ScanArray() will modify the first two parameters so that the new values are available to the calling
program or module. Write the pseudocode procedure header for ScanArray().
............................................................................................................................................. [4]

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


COMPUTER SCIENCE WITH INQILAB PATEL
4 The following pseudocode is a string handling function.
FUNCTION Clean(InString : STRING) RETURNS STRING
DECLARE NewString : STRING
DECLARE Index : INTEGER
DECLARE AfterSpace : BOOLEAN
DECLARE NextChar : CHAR
CONSTANT Space = ' '
AfterSpace FALSE
NewString ""
FOR Index 1 TO LENGTH(InString)
NextChar MID(InString, Index, 1)
IF AfterSpace = TRUE
THEN
IF NextChar<> Space
THEN
NewString NewString&NextChar
AfterSpace FALSE
ENDIF
ELSE
NewString NewString&NextChar
IF NextChar = Space
THEN
AfterSpace TRUE
ENDIF
ENDIF
ENDFOR
RETURN NewString
ENDFUNCTION

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


(a) (i) Complete the trace table by performing a dry run of the function when it is called as follows:
Result Clean("X Y and Z")
The symbol ' ' represents a space character. Use this symbol to represent a space character in the
trace table. [6]
Index AfterSpace NextChar NewString

(ii) State the effect of the function Clean().


...........................................................................................................................................
..................................................................................................................................... [1]
(iii) The pseudocode is changed so that the variable AfterSpace is initialised to TRUE.
Explain what will happen if the function is called as follows:
Result Clean(" X Y and Z")
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
..................................................................................................................................... [2]

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


COMPUTER SCIENCE WITH INQILAB PATEL

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/
COMPUTER SCIENCE WITH INQILAB PATEL
4 (b) Write pseudocode to implement the program.
You should note the following:

nts of the array Result have all been initialised to zero.

You should assume the following lines of pseudocode have been written:
DECLARE InString : STRING
DECLARE Result : ARRAY [1:26] OF INTEGER
Declare any further variables you use. Do not implement the code as a subroutine.
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
............................................................................................................................................. [7]

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


5 The following pseudocode checks whether a string is a valid password.
FUNCTION CheckPassword (InString : STRING) RETURNS BOOLEAN
DECLARE Index, Upper, Lower, Digit, Other : INTEGER
DECLARE NextChar : CHAR
Upper ← 0
Lower ← 0
Digit ← 0
Other ← 0
FOR Index ← 1 TO LENGTH(InString)
NextChar ← MID(InString, Index, 1)
IF NextChar >= 'A' AND NextChar <= 'Z'
THEN
Upper ← Upper + 1
ELSE
IF NextChar >= 'a' AND NextChar <= 'z'
THEN
Lower ← Lower + 1
ELSE
IF NextChar >= '0' AND NextChar <= '9'
THEN
Digit ← Digit + 1
ELSE
Other ← Other + 1
ENDIF
ENDIF
ENDIF
NEXT Index
IF Upper>1AND Lower>=5 AND (Digit-Other)>0
THEN
RETURN TRUE
ELSE
RETURN FALSE
ENDIF
ENDFUNCTION
(a) Describe the validation rules that are implemented by this pseudocode. Refer only to the contents
of the string and not to features of the pseudocode.
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
............................................................................................................................................. [3]

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


COMPUTER SCIENCE WITH INQILAB PATEL
(b) (i) Complete the trace table by dry running the function when it is called as follows: [5]
Result ← CheckPassword("Jim+Smith*99")

Index NextChar Upper Lower Digit Other

(ii) State the value returned when the function is called using the expression shown. Justify your
answer.
Value .................................................................................................................................
Justification .......................................................................................................................
...........................................................................................................................................
........................................................................................................................................... [2]

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/
COMPUTER SCIENCE WITH INQILAB PATEL
5 (a) A 1D array, Directory, of type STRING is used to store a list of school internal telephone numbers. There are 1000
elements in the array. Each element stores a single data item. The format of each data item is as follows:
<Number><Name>
Number is a four-digit numeric string.
Name is a variable-length string.
For example:
"1024Collins Robbie"
The following pseudocode is an initial attempt at defining a procedure SortContacts() that will perform a bubble sort on
Directory. The array is to be sorted in ascending order of Name.
Fill in the gaps to complete the pseudocode.
PROCEDURE SortContacts ()
DECLARE Temp : STRING
DECLARE FirstName, SecondName : STRING
DECLARE NoSwaps : ……………………………………
DECLARE Boundary, J : INTEGER
Boundary ← ……………………
REPEAT
NoSwaps ← TRUE
FOR J ← 1 TO Boundary
FirstName ← …………………………(Directory[J], LENGTH(Directory[J]) – …………… )
SecondName ← RIGHT(Directory[J + 1], LENGTH(Directory[J + 1]) – 4)
IF FirstName ………………………………
THEN
Temp ← Directory[J]
Directory[J] ← Directory ……………………………
Directory[J + 1] ← Temp
NoSwaps ← ……………………………
ENDIF
NEXT J
Boundary ← ……………………………
UNTIL NoSwaps = TRUE
ENDPROCEDURE [8]
(b) The pseudocode contains a mechanism designed to make this an efficient bubble sort.
Describe the mechanism and explain why it may be considered to be efficient.
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
............................................................................................................................................. [4]

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


6 A company hires out rowing boats on a lake. The company has ten boats, numbered from 1 to 10.
The company is developing a program to help manage and record the hiring out process.
Hire information is stored in three global 1D arrays when a boat is hired out. Each array contains 10
elements representing each of the ten boats.
The three 1D arrays are summarised as follows:
Array Data type Description Example data value
HireTime STRING The time the boat was hired out "10:15"
Duration INTEGER The number of minutes of the hire 30
Cost REAL The cost of the hire 5.75
If an individual boat is not currently on hire, the corresponding element of the HireTime array will be
set to "Available".
The programmer has started to define program modules as follows:
FUNCTION AddTime ( ThisTime : STRING, ThisDuration: INTEER) RETURNS STRING
HourString = LEFT(ThisTime, 2)
MinuteString = RIGHT (ThisTime, 2)
HourInteger = TONUM(HourString)
MinuteInteger = TONUM(MinuteString)
NewMinutes = MinuteInteger + ThisDuration
IF NewMinutes=60
THEN
NewMinutes=0
HourInteger = HourInteger + 1
ELSEIF NewMinutes > 60 THEN
NewMinutes = NewMinutes – 60
HourInteger = HourInteger +1
ELSE
NewHour = HourInteger
ENDIF
NewHourString = NUM_TO_STRING(NewHour)
NewMinuteString= NUM_TO_STRING(NewMinutes)
NewTime = NewHourString & “:” & NewMinuteString
RETURN NewTime
END FUNCTION

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


COMPUTER SCIENCE WITH INQILAB PATEL
Module Description
Called with two parameters:
o a STRING value representing a time
AddTime() o an INTEGER value representing a duration in minutes
Adds the duration to the time to give a new time
Returns the new time as a STRING
Called with a STRING value representing the time the hire will start
Outputs the boat numbers that will be available for hire at
the given start time. A boat will be available for hire if it is either:
o currently not on hire, or
ListAvailable()
o due back before the given hire start time
Outputs the number of each boat available
Outputs the total number of boats available or a suitable message if there are
none
Called with four parameters:
o an INTEGER value representing the boat number
o a STRING value representing the hire start time
o an INTEGER value representing the hire duration in minutes
o a REAL value representing the cost of hire
RecordHire()
Updates the appropriate element in each array
Adds the cost of hire to the global variable DailyTakings
Converts the four input parameters to strings, concatenated using commas as
separators, and writes the resulting string to the end of the existing text file
HireLog.txt

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


(a) Write pseudocode for the module ListAvailable().
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
............................................................................................................................................. [8]

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


COMPUTER SCIENCE WITH INQILAB PATEL
(b) Write program code for the module RecordHire().
Visual Basic and Pascal: You should include the declaration statements for variables.
Python: You should show a comment statement for each variable used with its data type.
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
............................................................................................................................................. [7]

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


(c) The module description of AddTime()is repeated here for reference.
Module Description
• Called with two parameters:
o a STRING value representing a time
AddTime() o an INTEGER value representing a duration in minutes
• Adds the duration to the time to give a new time
• Returns the new time as a STRING
(i) Write program code for a statement that uses the AddTime() function to add a duration of 60
minutes to a start time contained in variable BeginTime and to assign the new time to variable
EndTime.
...........................................................................................................................................
..................................................................................................................................... [2]
(ii) The function AddTime() is to be tested using black-box testing.
Complete the following two tests that can be performed to check the operation of the function. Note
that test 1 and test 2 are different.
Test 1 – Boat is returned during the same hour as rental starts
Start time value ....................................... Duration value .......................................
Expected new time value .......................................

Test 2 – Boat is returned during the hour after the rental starts
Start time value ....................................... Duration value .......................................
Expected new time value ....................................... [2]

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


COMPUTER SCIENCE WITH INQILAB PATEL

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/
COMPUTER SCIENCE WITH INQILAB PATEL

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/

You might also like