2.6 Arrays
2.6 Arrays
Chapter 14
Example
StudentName[1:30]
For illustration, let's take array declaration to store marks of 10 students.
Marks[1:10]
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.
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.
Dimension: Dimension is the organisational structure of array. It may be 1D that has single column
or 2D that have multiple columns.
Example
DECLARE StudentName[1:30] : STRING
StudentName[1] “Abdullah”
Array Name
StudentName
1 Abdullah
2 Rumaisa
Array Elements
3 Rashid
4 Afeera
5 Laiba
6 Patel
Index No
7 Salahuddin
29 Mani
30 Muzna
Declaring an array
It is important declare the arrays before assigning values in it so that program can reserve that
amount of space in its memory; otherwise, there may not be enough space when the program
uses the data.
Declaration consists of telling the computer program:
the identifier name of the array
the sort of data that is going to be stored in the array, i.e. its data type
How many items of data are going to be stored, so that it knows how much space to reserve.
Different programming languages have different statements for initialising the array but they all do
the same thing. In Visual Basic, the statement is:
Dim Name(20) As String
This Dim statement declares:
● the identifier name: Name
● the upper bound: 20
● the data type: String.
The upper bound of 20 specifies that there can be a maximum of 21 data items, since Visual
Basic starts with a subscript of zero. We do not have to fill the array; the upper bound of 20
indicates the maximum size.
The array that has been described in 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.
COMPUTER SCIENCE WITH Ali Raza
Q 17.2) Explain with the help of examples when arrays are used in programming.
…………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………
……………………………………………………………………….…………………………………………
………………………………………………………………………….………………………………………
………………………………………………………….……………………………………………………
Q 17.3) Declare arrays to Explain with the help of examples when arrays are used in programming
.
Past paper flowchart for same type of question in Winter 2017 P21 Q5
The flowchart below represents a program routine.
(a) The array used in the flowchart contains the following data:
Name[1] Name[2] Name[3] Name[4]
COMPUTER SCIENCE WITH Ali Raza
Jamal Amir Eve Tara
Complete the trace table using the data given in the array. [5]
3 The global trade item number (GTIN-8) barcode has seven digits and a check digit.
This pseudocode algorithm inputs seven digits and calculates the eighth digit, then outputs
theGTIN-8.
DIV(X,Y), finds the number of divides in division for example DIV(23,10) is 2.
MOD(X,Y), finds the remainder in division for example MOD(23,10) is 3.
(a) Complete the trace table for the input data: 5, 7, 0, 1, 2, 3, 4 [5]
(b) Explain how you would change the algorithm to input eight digits (seven digits and the check-
digit) and output if the check digit entered is correct or not.
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
...................................................................................................................................................... [3]
Q 17.6) An algorithm to reset the contents of the array Coins after each sale is shown below. There
are 10 different coins. This algorithm contains a logic error.
COMPUTER SCIENCE WITH Ali Raza
i=1
REPEAT
Coins(i) = 0
i=i+1
UNTIL i = 10
The most challenging aspect was the correct management of the loop counter.
5 A programmer writes a program to store a patient’s temperature every hour for a day. State the
data structure that would be most suitable to use and give the reason for your choice.
Data structure ......................................................................................................................
Reason ..............................................................................................................................
.......................................................................................................................................[2]
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
.......................................................................................................................................................[2]
(b) Identify, using pseudocode, another loop structure that the algorithm in part (a) could have
used.
............................................................................................................................................................
.....................................................................................................................................................[1]
(c) Write an algorithm, using pseudocode, to input a number between 0 and 100 inclusive. The
algorithm should prompt for the input and output an error message if the number is outside this
range.
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
........................................................................................................................................................[3]
Q 17.10Winter 2017 P22
3 The following diagram shows four data structures and four descriptions. [3]
Draw a line to connect each data structure to the correct description.
Data structure Description
Constant A collection of related data
COMPUTER SCIENCE WITH Ali Raza
A value that can change whilst a program is
Array
running
A restaurant table will have its data stored in its own booking record. Alessio decides to use
an array of records.
Write program code to declare the array TableBookingsfor the 12 table records.
Programming language.......................................................................................................................
Code ...................................................................................................................................................
........................................................................................................................................................[1]
Summer 2016 P21 &P23
(ii) The swimming club has 50 members.
State the data structure that would be most suitable to use and give a reason for your choice.
Data structure .....................................................................................................................
Reason ...............................................................................................................................
.......................................................................................................................................[2]
COMPUTER SCIENCE WITH Ali Raza
Practice Questions
............................................................................................................................................................
.....................................................................................................................................................[1]
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
.......................................................................................................................................................[2]
‘ ’ ‘ ’
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
.......................................................................................................................................................[2]
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
......................................................................................................................................................[4]
b) Calculate the average marks of the class by traversing the array.
COMPUTER SCIENCE WITH Ali Raza
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
........................................................................................................................................................[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]
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.
COMPUTER SCIENCE WITH Ali Raza
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
........................................................................................................................................................[3]
b) Write a program that inputs the name of your friend search in the array and output telephone
number.
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
........................................................................................................................................................[3]
c) 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 5) John works in a supermarket, where he is given a task to find which item has the highest pric
e and which item has the lowest price. There are 900 items in the supermarket.
a. Declare suitable arrays to store name and price of each product.
b. In put price of each product with its name.
COMPUTER SCIENCE WITH Ali Raza
c. Output the number of items which have a price greater than 100and number of items which
have price less than 50.
d. Output the highest and the lowest price.
……………………………………………………....................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
.................................................................... [4]