0% found this document useful (0 votes)
16 views16 pages

CSC 204 - Study Session 3

This document covers the fundamentals of arrays in data structures, explaining their definition, types (one-dimensional, two-dimensional, and multi-dimensional), and how to declare, initialize, and access them. It highlights the importance of arrays in programming, their applications in various fields, and basic operations that can be performed on them. Additionally, it provides examples and pilot questions to reinforce understanding of the concepts presented.
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)
16 views16 pages

CSC 204 - Study Session 3

This document covers the fundamentals of arrays in data structures, explaining their definition, types (one-dimensional, two-dimensional, and multi-dimensional), and how to declare, initialize, and access them. It highlights the importance of arrays in programming, their applications in various fields, and basic operations that can be performed on them. Additionally, it provides examples and pilot questions to reinforce understanding of the concepts presented.
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/ 16

CSC 204: Fundamentals of Data Structures

Study Session 3: The Array Structure

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.

Learning Outcomes for Study Session 3


On completion of this study session, you should be able to:
3.1 Define Array and enumerate its components.
3.2 Discuss the types of arrays and be able to differentiate them.
3.3 Declare, initialise and access an array.
3.5 Outline the fields and applications of arrays.

Page 1 of 16
CSC 204: Fundamentals of Data Structures

3.1 What is an Array?


An Array is a sequenced collection of elements of the same data type with a single identifier name.
As discussed in the previous outlines, there are three basic different types of data structure, one of
them is array. The others are records and sets. These will be discussed in the forthcoming study
sessions. The array is probably the most widely used data structure; in some programming
languages it is even the only one available.

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.

Pilot Question 3.1


i. What is an Array?

Page 2 of 16
CSC 204: Fundamentals of Data Structures

ii. What kind of data does array consist of?

3.2 Types of Arrays


The types of array depend on the number of dimension an array has. Using this criterion for
grouping we have:

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.

3.2.1 One-Dimensional Array


A one-dimensional array is an array with only one dimension. This is the simplest form of an
array. It is also called a linear array. Accessing its elements involves a single subscript which can
either represent a row or column index. Indices or subscripts must be integers within the range. The
smallest and the largest indices or subscripts are referred to as the lower bound and the upper
bound, respectively.

The syntax for a one-dimensional array is

type anArrayName [ x ]

int chuck[10];

Figure 3.1 One-dimensional array

A one-dimensional array has the following important components:

Page 3 of 16
CSC 204: Fundamentals of Data Structures

i. A name: This is the array name.


ii. A type: This is the type of all array elements.
iii. An extent: This is the range of the indices or subscripts of array elements. In the above
example the elements of the array are (elements 1, element 2, elements 3... element 10)

3.2.2 Two-Dimensional Array (2D)


In two-dimensional array, the arrays are indexed by two subscripts, one for the row and one for the
column. In 2D, each element of the array must by the same type either a primitive type or object
type. Two-dimensional array is the simplest form of a multi-dimensional array. A two dimensional
array is expressed in the same way as one- dimensional array except you need an extra set of
brackets to indicate the second dimension.

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];

Figure 3.2 Two dimensional Array syntax

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];

Figure 3.3 Two-dimensional Array

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:

Figure 3.4 A two-dimensional array matrix representation

3.2.3 Multi-Dimensional Array


A Multidimensional array can be described as "arrays of arrays". For example, two-dimensional
array can be imagined as a two-dimensional table made of elements, all of them of a same uniform
data type.

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 Multi-dimensional array example

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

Pilot Question 3.2

i. Write the syntax of a one-dimensional array consisting of 15 characters.


ii. Two-dimensional arrays are also known as what?
iii. Array syntax consist of ____________, ____________ and _______________ components.

3.3 Creating and Using Arrays.


Arrays are among the oldest and most important data structures, and are used by almost every
program. To use arrays, they must be declared, initialized and prone to access.

3.3.1 Declaration of an Array


An "array declaration" names the array and specifies the type of its elements. It can also define the
number of elements in the array. A variable with array type is considered a pointer to the type of the
array elements. The length of an array is established when the array is created. After creation, its
length is fixed. Each item in an array is called an element, and each element is accessed by its
numerical index.

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

type arrayName [ arraySize ]

int score[5];

Figure 3.6 Declaring an array

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:

extern type arrayName [ ]

3.3.2 Initialising and Accessing Of Arrays.


An array is initialised so that the compiler will allocate space for the specified variables in memory.
The number of elements in an array must be fixed at compile time. It is best to make the array size a
constant and then, if required, the program can be changed to handle a different size of array by
changing the value of the constant. One way to create an array is with the new operator.

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.

3.4.1 Using the Assignment Operator (=)

Figure 3.7 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

Observe the code below in the figure below;

Page 7 of 16
CSC 204: Fundamentals of Data Structures

int [ ] temps = new int [3];

temps[0] = 78; //filling one element at a time

temps[1] = 88;

temps[2] = 53;

Figure 3.8 Using “=” to initialize an array

This method works fine until you have an array that needs to contain a large amount of data.

3.4.2 Using the “for....” loop and user input:

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:

int [ ] nums = new int [8];

for (int ctr=0; ctr<8; i++)

// fill one element at a time

nums[ctr] = Console.readInt(“Please enter a number”);

Figure 3.9 Using the “for….” loop to initialize an array.

Page 8 of 16
CSC 204: Fundamentals of Data Structures

3.4.3 Initialize at time of Declaration

It is possible to initialize an array at the time of declaration. This is done by filling the list

Example 3.2:

double [ ] height = { 11.5, 18.2, 15.9, 26.4};

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:

double [ ] height = new double [4];


height[0] = 11.5;
height[1] = 18.2;
height [2] = 15.9;
height [3] = 26.4;

Pilot Question 3.3

i. Declare an array consisting of 10 elements for all these data types: integer, character, real,
strings using your name as the array name.

3.4 Operations and Applications of Arrays


Array is used for different varieties of applications. Array is used to store the data or values of same
data type. Below are the some of the operations and applications of array

3.4.1 Basic Array Operations


The following are some of the basic array operations:

 make-array(integer n): Array


Create an array of elements indexed from 0 to n-1, inclusive. The number of
elements in the array, also known as the size of the array, is n.
 get-value-at(Array a, integer index): Element
Returns the value of the element at the given index. The value of index must be in
bounds: 0 <= index <= (n - 1). This operation is also known as subscripting.

Page 9 of 16
CSC 204: Fundamentals of Data Structures

 set-value-at(Array a, integer index, Element new-value)


Sets the element of the array at the given index to be equal to new-value

Arithmetic operations such as addition, division, multiplication, negation, copy, increment,


decrement) can also be performed on arrays.

3.4.2 Application of Arrays


Arrays are used in many fields for many purposes. It is used in the field of computer science,
physics, mathematics and so on. These are some of the function or application of arrays.
1. Arrays are used to implement mathematical vectors and matrices, as well as other kinds of
rectangular tables. For example they are used in many databases (small and large). These
databases consist of (or include) one-dimensional arrays whose elements are records.
2. Programmers use array to organize collections of data efficiently and intuitively. This
prevents repetition of data or input.
3. Arrays are used to implement other data structures, such as heaps, hash tables, queues,
stacks, strings.
4. Large arrays are sometimes used to emulate in-program dynamic memory allocation
particularly memory pool allocation.
5. Some arrays are used in modeling game boards in computer games.

Pilot Question 3.4


i. Lists 4 Arithmetic operation s that can be performed on arrays
ii. List some of the areas of fields or specialization that arrays are used.

Page 10 of 16
CSC 204: Fundamentals of Data Structures

Summary of Study Session 3: The Array Structure

In this Study Session, you have learned that:

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

Pilot Answer 3.1


i. An array is a sequenced collection of elements of the same data type with a single identifier
name. Arrays are collections of homogenous data i.e of the same data type.
ii. An array deals with homogenous data i.e. elements of the same data type.

Pilot Answer 3.2


i. char anArrayName[15];
ii. Two-dimensional arrays are also known as Tables or Matrices.
iii. Array syntax consist of a name, a type and an extent components

Pilot Answer 3.3

i.
int chuck[10];

char chuck[10];

real chuck[10];

str chuck[10]

Page 12 of 16
CSC 204: Fundamentals of Data Structures

Pilot Answer 3.4


i. Arithmetic operations that can be performed on arrays include: addition, division,
multiplication, negation, copy, increment, decrement.
ii. Some of the areas of fields or specialization that arrays are used are mathematics, physics,
accounting, computer science etc

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

One-dimensional array is an array with only one dimension

Multidimensional array can be described as "arrays of arrays".

Page 14 of 16
CSC 204: Fundamentals of Data Structures

Self-Assessment Questions for Study Session 3


To assess yourself about how well you have assimilated and understood this study session,
and to determine if you have achieved its Learning Outcomes, answering the questions below.
Write your answers in your Study Diary and discuss them with your Tutor at the next Study
Support Meeting. You can check your answers with the Notes on the Self-Assessment Questions at
the end of this Module

i. Give a common example of an array you know.


ii. Highlight the types of arrays and explain briefly.
iii. Differentiate between a one-dimensional array and multi-dimensional arrays with
examples.
iv. Write 3 methods to initialise an array.
v. Highlights 3 applications of arrays.

Page 15 of 16
CSC 204: Fundamentals of Data Structures

Notes on SAQ for Study Session 3

i. An array of integers, an array of real values.


ii. Types of arrays includes:
 One-dimensional arrays
 Two-dimensional arrays
 Multi-dimensional arrays
iii. One-dimensional array is an array with only one dimension while multi-dimensional arrays
are arrays with more 2 or more dimensions. They are called arrays of arrays. Two-
dimensional arrays can be regarded as a multi-dimensional array.
Examples:
 One-dimensional array example:

int score[10];

 Multidimensional arrays example:

real distance [100][10][10][5];

iv. Three ways of initializing an arrays are:


a. Using the Assignment Operator (=). Also called the drudge method.
b. Using the “for....” loop and user input
c. Initialize at time of Declaration

v. Application of arrays includes:


 Arrays are used by programmers to organize collections of data efficiently and
intuitively. This prevents repetition of data or input.
 Arrays are used to implement mathematical vectors and matrices, as well as
other kinds of rectangular tables.
 Arrays are used to implement other data structures, such as heaps, hash tables,
queues, stacks, strings

Page 16 of 16

You might also like