10.2 As Arrays Data Structure
10.2 As Arrays Data Structure
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.
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.
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.
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]
Arrays can be used in assignment statements (provided they have same size and data type). The following is
therefore allowed:
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”
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
.........................................................................................................................................................[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]
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
........................................................................................................................................................... [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]
..................................................................................................................
..................................................................................................................
..................................................................................................................
..................................................................................................................
.................................................................................................................
.................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
..........................................................................................................................................................[3]
b) Write a program to display 1st and 3rd telephone number stored in array.
.................................................................................................................................................................
........……….......................................................................................................................................... [2]
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
............................................................................................................................................................ [3]
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
...........................................................................................................................................................[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]
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]
NextChar[1] ‘A’
NextChar[26] ‘Z’ [2]
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
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]
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]
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.
FOR Count 1 TO 5
INPUT Num1[Count]
NEXT Count
FOR Count 1 TO 5
Num2[Count] Num1[Count]
NEXT Count [3]
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]
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.
...............................................................................................................................................[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
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:
aracter.
A typical input character sequence, stored as InputString is:
13*156*9*86*1463*18*#
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]
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]
(ii) State the value returned when the function is called using the expression shown. Justify your
answer.
Value .................................................................................................................................
Justification .......................................................................................................................
...........................................................................................................................................
........................................................................................................................................... [2]
Test 2 – Boat is returned during the hour after the rental starts
Start time value ....................................... Duration value .......................................
Expected new time value ....................................... [2]