0% found this document useful (0 votes)
2 views14 pages

26 Arrays

The document discusses arrays as a data structure used to store multiple data items of the same type, highlighting their fixed-length nature and indexing. It covers array declaration, elements, size, type, and dimensions, along with examples and practical applications in programming. Additionally, it includes questions and exercises related to arrays, flowcharts, and pseudocode algorithms.

Uploaded by

tawandamugorex
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views14 pages

26 Arrays

The document discusses arrays as a data structure used to store multiple data items of the same type, highlighting their fixed-length nature and indexing. It covers array declaration, elements, size, type, and dimensions, along with examples and practical applications in programming. Additionally, it includes questions and exercises related to arrays, flowcharts, and pseudocode algorithms.

Uploaded by

tawandamugorex
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

COMPUTER SCIENCE WITH INQILAB PATEL

Chapter 14

Arrays: Data Structure & Pre-Release Materials


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[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

@inqilab inqilab-patel /inqilabpatel inqilab patel


COMPILED BY: INQILAB PATEL
+923002724734 http://inqilabpatel.com/
COMPUTER SCIENCE WITH INQILAB PATEL
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.

@inqilab inqilab-patel /inqilabpatel inqilab patel


COMPILED BY: INQILAB PATEL
+923002724734 http://inqilabpatel.com/
COMPUTER SCIENCE WITH INQILAB PATEL

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(19) = “Mani”
Quick Revision Questions
Q 17.1) Explain the following terms regarding arrays:
Array: ……………………………………………………………….…………………………………………
………………………………………………………………………………………………………………….
Size of Array: ………………………………………………………………………………………………..
………………………………………………………………………………………………………………….
Element: ………………………………………………………………….…………………………..………
………………………………………………………………………………………………………………….
Index: ………………………………………………………………….………………………………………
………………………………………………………………………………………………………………….
Type: ………………………………………………………………….………………………………………
………………………………………………………………………………………………………………….
Dimension: ………..………………………………………………….………………………………………
………………………………………………………………………………………………………………….

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
.

a) Declare arrays to store name of 30 students


………………………………………………………………………………………………….....………
………………………………………………………………………………………………………………

b) Declare arrays to store basic pay of 50 Employees.

@inqilab inqilab-patel /inqilabpatel inqilab patel


COMPILED BY: INQILAB PATEL
+923002724734 http://inqilabpatel.com/
COMPUTER SCIENCE WITH INQILAB PATEL
……………………………………………………………………………………………………...………
……………………………………………………………………………………….……………………
c) Declare arrays to input and store status of 50 employee that they are permanent or not.
…………………………………………….………………………………………………………………
…………………………………………………….……………………………………………………
Q 17.4) Draw a flowchart that
Inputs name of 10 students in a class and store them in one dimension array
Display list of names of students

@inqilab inqilab-patel /inqilabpatel inqilab patel


COMPILED BY: INQILAB PATEL
+923002724734 http://inqilabpatel.com/
COMPUTER SCIENCE WITH INQILAB PATEL

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]

@inqilab inqilab-patel /inqilabpatel inqilab patel


COMPILED BY: INQILAB PATEL
+923002724734 http://inqilabpatel.com/
COMPUTER SCIENCE WITH INQILAB PATEL
Jamal Amir Eve Tara

Complete the trace table using the data given in the array. [5]

Flag Count Name[1] Name[2] Name[3] Name[4] Temp


Jamal Amir Eve Tara

(b) Describe what the algorithm represented by the flowchart is doing.


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

@inqilab inqilab-patel /inqilabpatel inqilab patel


COMPILED BY: INQILAB PATEL
+923002724734 http://inqilabpatel.com/
COMPUTER SCIENCE WITH INQILAB PATEL

Q 17.5Summer 2018 P21

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]

Digit Digit Digit Digit Digit Digit Digit Digit


Sum OUTPUT
(1) (2) (3) (4) (5) (6) (7) (8)

Complete the trace table for the input data: 4, 3, 1, 0, 2, 3, 1


Digit Digit Digit Digit Digit Digit Digit Digit
Sum OUTPUT
(1) (2) (3) (4) (5) (6) (7) (8)

(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. Ther
e are 10 different coins. This algorithm contains a logic error.

@inqilab inqilab-patel /inqilabpatel inqilab patel


COMPILED BY: INQILAB PATEL
+923002724734 http://inqilabpatel.com/
COMPUTER SCIENCE WITH INQILAB PATEL
i=1
REPEAT
Coins(i) = 0
i=i+1
UNTIL i = 10

(i) State what is meant by a logic error.


............................................................................................................................................................
....................................................................................................................................................... [1]
(ii) Explain why the algorithm above contains a logic error.
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
...................................................................................................................................................... [2]
(i) The program is written to do something other than what the programmer intended
(ii) It will only reset the first 9 elements / will not resetthe 10th element
After setting Coins(9) = 0, i will become10...
... and the loop will stop
It should be UNTIL i > 10 / or other working correction
Q 17.8Summer 2015 P22
5 (a) Write an algorithm, using pseudo code and a FOR … TO … NEXT loop structure, to input
1000 numbers into an array.
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
........................................................................................................................................................[2]

(b) Rewrite your algorithm using another loop structure.


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

Examiner’s comments on Question 5


(a) Most candidates attempted the loop structure, better candidates also showed the skill of being able to use the loop
counter as the array index. Some candidates misread the question and incorrectly provided program code rather than
pseudo code.
(b) Better candidates correctly used REPEAT … UNTIL or WHILE … DO … ENDWHILE structures.

@inqilab inqilab-patel /inqilabpatel inqilab patel


COMPILED BY: INQILAB PATEL
+923002724734 http://inqilabpatel.com/
COMPUTER SCIENCE WITH INQILAB PATEL

The most challenging aspect was the correct management of the loop counter.

Q 17.9Summer 2016 P22

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]

5 (a) Describe the purpose of each statement in this algorithm.


FORI 1 to 300
INPUT Name[I]
NEXT I

............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
.......................................................................................................................................................[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

@inqilab inqilab-patel /inqilabpatel inqilab patel


COMPILED BY: INQILAB PATEL
+923002724734 http://inqilabpatel.com/
COMPUTER SCIENCE WITH INQILAB PATEL
A value that can change whilst a program is
Array
running

A value that never changes whilst a program is


Table
running

Variable A series of elements of the same data type

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]

@inqilab inqilab-patel /inqilabpatel inqilab patel


COMPILED BY: INQILAB PATEL
+923002724734 http://inqilabpatel.com/
COMPUTER SCIENCE WITH INQILAB PATEL

Practice Questions

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

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

‘ ’ ‘ ’
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
.......................................................................................................................................................[2]

............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
......................................................................................................................................................[4]
b) Calculate the average marks of the class by traversing the array.

@inqilab inqilab-patel /inqilabpatel inqilab patel


COMPILED BY: INQILAB PATEL
+923002724734 http://inqilabpatel.com/
COMPUTER SCIENCE WITH INQILAB PATEL
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
........................................................................................................................................................[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.

@inqilab inqilab-patel /inqilabpatel inqilab patel


COMPILED BY: INQILAB PATEL
+923002724734 http://inqilabpatel.com/
COMPUTER SCIENCE WITH INQILAB PATEL

............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
........................................................................................................................................................[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.

@inqilab inqilab-patel /inqilabpatel inqilab patel


COMPILED BY: INQILAB PATEL
+923002724734 http://inqilabpatel.com/
COMPUTER SCIENCE WITH INQILAB PATEL
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]

@inqilab inqilab-patel /inqilabpatel inqilab patel


COMPILED BY: INQILAB PATEL
+923002724734 http://inqilabpatel.com/

You might also like