CSC 204 - Study Session 3
CSC 204 - Study Session 3
Introduction
Computer programs often manage many objects of the same type. For instance a bank's accounting
program must manage hundreds or thousands of customer accounts. It is inconvenient and usually
impossible to declare distinctly named variables for each of the customer accounts; instead, you can
construct a new form of object (a data structure) to collectively hold and name the customer
accounts. This is done through arrays.
In this study session, you will learn about arrays, their types, their functions, how to create, declare
and access arrays. Also you will get to know the importance of arrays and areas where their
applications are useful.
Page 1 of 16
CSC 204: Fundamentals of Data Structures
Arrays group data elements of the same type i.e. an array consists of components which are all
of the same type, called its base type; it is therefore called a homogeneous structure or
homogeneous collection of components.
An array consists of elements and dimensions. Elements are the data that make up the array while a
dimension is the length, height, or depth of an array. The simplest arrays are linear: they have only
one dimension and are called vectors, n-tuples, single subscripted variables, or more simply, one-
dimensional arrays. An array can have one or more dimensions.
An array is a random-access structure, because all components can be selected at random and are
equally quickly accessible. In order to denote an individual component, the name of the entire
structure is augmented by the index selecting the component. This index is to be an integer between
0 and n-1, where n is the number of elements, the size, of the array.
Note: You cannot create an array of arrays. However, you can create an array of clusters, where
each cluster contains one or more arrays.
A key problem with arrays is that they have fixed size. Hence, they are called static data structures.
Another problem of arrays is that they are statically typed, i.e., cannot be used to store any type of
data, but only the type of data assigned to them in the declaration statement in which they were
specified. It is interesting to note that certain languages, such as SNOBOL and ICON, have
circumvented this difficulty by providing a TABLE data structure that can hold data of any type.
You are not responsible for knowing about the TABLE data structure, however, since it is not
available in Java, or in most modern high-level programming languages.
Page 2 of 16
CSC 204: Fundamentals of Data Structures
i. One-dimensional array
ii. Two-dimensional array
iii. Multi-dimensional array
An array type is written as the name of an element type followed by one or more empty pairs of
square brackets. Array elements are indexed or subscripted, just like x1, x2... xn in mathematics. You
can build arrays of numeric, Boolean, path, string, waveform, and cluster data types.
Consider using arrays when you work with a collection of similar data and when you perform
repetitive computations. Arrays are ideal for storing data you collect from waveforms or data
generated in loops, where each iteration of a loop produces one element of the array.
type anArrayName [ x ]
int chuck[10];
Page 3 of 16
CSC 204: Fundamentals of Data Structures
Two-dimensional arrays are also called tables, or matrices. A table may be viewed as a vector
whose components are themselves vectors. Just as is the case of one-dimensional arrays, all values
in a two-dimensional array must be of the same kind, since arrays are homogeneous.
A 2D array stores elements in a grid. It requires a column index and a row index, both of which are
zero-based, to locate an element. Arithmetic operations can be performed on these kind of arrays
such as addition, multiplication etc.
To declare a two-dimensional integer array of size x,y you would write something as follows:
type arrayName[x][y];
Assume jimmy represents a bi-dimensional array of 3 per 5 elements of type int. The C++ syntax
for this array is:
int jimmy[3][5];
Page 4 of 16
CSC 204: Fundamentals of Data Structures
This array can be interpreted that it has 3 rows and 5 columns. It can also be represented in the form
of matrix as shown below:
Multidimensional arrays are not limited to two indices (i.e., two dimensions). They can contain as
many indices as needed. Although be careful: the amount of memory needed for an array increases
exponentially with each dimension. Thus, a two-dimensional array can also be categorised as a
multidimensional array.
char century[100][365][24][60][60];
Figure 3.5 declares an array with an element of type char(character) for each second in a century.
This amounts to more than 3 billion char! So this declaration would consume more than 3 gigabytes
of memory.
Multidimensional arrays are just an abstraction for programmers, since the same results can be
achieved with a simple array, by multiplying its indices. The only difference is that with
multidimensional arrays, the compiler automatically remembers the depth of each imaginary
dimension.
Page 5 of 16
CSC 204: Fundamentals of Data Structures
An array declaration is very similar to a variable declaration. First a type is given for the elements
of the array, then an identifier for the array and, within square brackets, the number of elements in
the array. The number of elements must be an integer.
Arrays are declared with the bracket punctuators [ ] but the syntax can be in two forms:
I. The first form defines an array variable. The constant-expression argument within the
brackets specifies the number of elements in the array. The constant-expression, if present,
must have integral type, and a value larger than zero. Its syntax is shown below
int score[5];
Page 6 of 16
CSC 204: Fundamentals of Data Structures
II. The second form declares a variable that has been defined elsewhere i.e if the array is
external. It omits the constant-expression argument in brackets, but not the brackets. You
can use this form only if you previously have initialized the array, declared it as a
parameter, or declared it as a reference to an array explicitly defined elsewhere in the
program. The syntax is:
There are several ways of initializing an array. One of them is by using the assignment operator (=)
to initialize the array. This method is called the "drudge" method.
In Figure 3.7, the first section consisting of “int [] num” is used in the declaring the array. The
assignment operator “=” is used to initialize the array while the word new is used to create a new
array assigned to the previous one.
Example 3.1
Page 7 of 16
CSC 204: Fundamentals of Data Structures
temps[1] = 88;
temps[2] = 53;
This method works fine until you have an array that needs to contain a large amount of data.
Another way to initialise an array is by using a ‘for’ loop and user’s input to initialize an array.
Example 3.2
Consider the code below:
Page 8 of 16
CSC 204: Fundamentals of Data Structures
It is possible to initialize an array at the time of declaration. This is done by filling the list
Example 3.2:
The array length (size) will be automatically set to the minimum that will hold the given
values. The statement above is equivalent to the following statements:
i. Declare an array consisting of 10 elements for all these data types: integer, character, real,
strings using your name as the array name.
Page 9 of 16
CSC 204: Fundamentals of Data Structures
Page 10 of 16
CSC 204: Fundamentals of Data Structures
1. An array is a sequenced collection of elements of the same data type with a single identifier
name. Arrays group data elements of the same type i.e. an array consists of components
which are all of the same type, called its base type.
2. You cannot create an array of arrays. However, you can create an array of clusters, where
each cluster contains one or more arrays
3. The types of array depend on the number of dimension an array has. We have the one-
dimensional array, multi-dimensional array. Two-dimensional array is a type of
multidimensional array whose dimension is two.
4. Before you can access an array, it must be first declared. Arrays are declared with the bracket
punctuators [ ] but the syntax can be in two forms. The Arrays will either declare a variable
of constant expression stated in the array or declares a variable that has been defined
elsewhere i.e. if the array is external.
5. In creating an array, the ‘new’ operator is used.
6. Initialising an array can be done in 3 forms: The drudge method (using the assignment
operator (=)); using the ‘for….’ Loop with input data and initialising the array at the time of
declaration.
7. Arrays are used in many fields and areas of specialisations such as in accounting, in
computer science, in mathematics etc.
Page 11 of 16
CSC 204: Fundamentals of Data Structures
Pilot Answers
i.
int chuck[10];
char chuck[10];
real chuck[10];
str chuck[10]
Page 12 of 16
CSC 204: Fundamentals of Data Structures
Page 13 of 16
CSC 204: Fundamentals of Data Structures
Glossary of Terms
Array is a sequenced collection of elements of the same data type with a single identifier name
Page 14 of 16
CSC 204: Fundamentals of Data Structures
Page 15 of 16
CSC 204: Fundamentals of Data Structures
int score[10];
Page 16 of 16