You are on page 1of 11

Introduction to Programming

FSC1331/FDN1120
Topic 9 : Arrays

Topic’s / Unit’s Learning Outcome:


1. Array Declaration
2. The Variably Defined Array
3. Sample Programs

17/01/2016 1
9.1 Array Declaration

• Array is a consecutive storage location in computer’s memory


that stores similar type of data.
• Array is something like a table or list that hold a group of
items / elements of the same type.
• A name is normally assigned to an array and the required slots
would be defined in the array’s declaration.
• Additionally, array could be initialized to some values prior to
operations or manipulation.

17/01/2016 2
9.1 Array Declaration
• Once an array is declared, individual slot could be used for
storage and these slots are referenced via index.
• The first slot starts at zero.
• Each slot in the array could be referred by specifying the array
name and the index.
• For example, the statement SalesArray[2] would refer to third
slot’s stored value

17/01/2016 3
9.2 The Variably Defined Array
• In some situations, it is important for us to access different
elements, essentially different variables, by changing the
index.
• The advantage of using variably defined variable in the array
index is, it would reduce/simplifies the array operations.
• Consider an array with hundred elements, and if the value of
each element were to be displayed/printed, individual access
would require the same /statement code to be repeated
hundred times in a program.
• This would not happen if the index were declared variably.

17/01/2016 4
9.2 The Variably Defined Array

17/01/2016 5
9.3 Sample Program

17/01/2016 6
9.3 Sample Program

17/01/2016 7
9.3 Sample Program

Error?
Correction?

17/01/2016 8
9.3 Sample Program

Idx+1

17/01/2016 9
9.3 Sample Program

17/01/2016 10
9.3 Sample Program

17/01/2016 11

You might also like