Uploaded by Kwesi Legacy

Arrays

advertisement
ARRAYS
Introduction
This chapter introduces one of the most basic data structures called array. An array data
structure is useful in implementing other data structures such as queues, stack, heaps and
many more. This data structure stores a group of data items of the same type. The items are
stored in a contiguous block of cells which are accessible with unique indices as shown in
figure 1. Arrays are static in nature which implies their size remains unchanged once created.
Figure 1. An Array Structure
Definition
An array is a list of contiguous memory cells that store data of the same type. These adjoining
cells are of the same data type and array name. Array data structure is usually finite and
ordered in nature in the sense that it stores only a known number of data items as per its size
in an ordered memory cells. Figure 2 indicates an array named Ages containing 10 elements.
Ages:
Figure 2 An Array of Size 10
Basic Terminologies of Arrays






1
Element: This refers to any data or item stored in an array. In figure 2 items such as
34, 8, 32, etc are known as elements of the array ages.
Size: The size of an array refers to the total number of elements that can be stored in
an array. The size of the array ages (figure 2) is 10. The size is fixed which makes an
array a static data structure.
Name: Every array must have a name. In the case of figure 2 the array is called ages.
Type: Type refers to the kind of data stored in an array. The data type can either be an
integer, decimal or textual (string or character).
Cell: A single memory location of an array is called a cell. The total number of cells
of an array is equal to the size of the array.
Index: An array index is a positive integer that uniquely identifies a cell of an array.
Indexing of arrays usually starts from zero (0) to the size of the array minus one (1) in
N. S. AWARAYI
step of one (1). Indices are properly indicated in figure 2. Indices are very useful in
storing and retrieving data in an array.
Why Array Data Structure
The main rationale behind data structures is to organize data in a manner that is most flexible
to manipulate. Programmers employ various data structures in various situations to make data
easy to handle when writing algorithms or programs to solve a problem.
In programming, arrays can be better understood as a collection of variables of the same type
that store data for the same purpose. A program that requires data of “similar” type would
essentially require using a number of variables. This can potentially create a problem of
organisation and makes programming difficult. This situation however could be handled by
simply using an array.
Consider an example of 10 students who sat for a quiz. If you are to write a program that
prints the highest score among the 10 students, you will probably need ten variables which
are manipulated in a manner as shown in the code segment below.
This becomes more tedious when an attempt is made to print the scores from the higher to the
lowers. An easier approach is the use of an array in which you can loop through the elements,
comparing them to locate and print the largest score as shown in the code segment below.
2
N. S. AWARAYI
Arrays like other data structures are very necessary in organising data for easy manipulation
during problem solving. Array data structure is applicable in so many ways which will be
considered in later sections.
One-Dimensional Array
An array is said to be one dimensional if a single index is required to store or retrieve
elements. The physical structure of a one-dimensional array is linear as shown in figure 3. To
store an element say 12 in the array, you are required to indicate the index say 2 ( k[2] =12).
Figure 3 Storing an Element in Index 2
Creating an Array (Array Representation)
To create or declare an array in C++, three things must be provided: the name, type and size
of the array.
The declaration above creates a one-dimensional array of type double (floating points) named
scores with a size of 6. The first index of the array, scores is zero (0) and the last is 5. When
the elements of the array are known already then the array can be declared in this manner.
The above declaration automatically creates an array of fixed size 6. In this kind of
declaration the size is optional.
*[Logical addressing]
Array Operations
The basic operations that can be performed on arrays include the following:

3
Traverse: To visit each element of the array and print it.
N. S. AWARAYI





Insertion: To add a new element to the array.
Delete: To remove an element from the array.
Search: To check whether a particular element exist in the array.
Sort: To arrange elements of an array in a particular order (Ascending or descending).
Merge: To add two distinct arrays together to form a single array.
Array Traversal Operation
Traversing an array refers to visiting all the elements of the array and processing (printing)
them. Traversal starts from the first element (index 0) of the array and processing each
element in step of one up to the last item.
Traversal Algorithm
Implementation in C++
Insertion Operation
An element can be added to an array only if it is not full. An element can be added to the
beginning, end or at a given index of the array per the requirements of the array. To insert an
item at a given location (index), move all elements a step forward starting from the last
element of the array to the location to insert the new item as represented in figure 4.
Figure 4. Inserting 25 at index 2 in an array
4
N. S. AWARAYI
Insertion Algorithm
Implementation in C++
Deletion Operation
To delete an item at a given index or location, simply move all elements a step backward
starting from the index just after the index of the item to be deleted to the last element in the
array. As the elements move, they override other elements originally at various indices.
Figure 5 illustrate the delete operation.
Figure 5. Deleting an element at index 3
5
N. S. AWARAYI
Deletion Algorithm
Implementation in C++
Search Operation
This operation checks the existence of an item in an array. The operation returns or prints the
element and/or its location (index) in the array. To search for an item using a sequential
search requires visiting every element and comparing with the search item starting from the
first index. Search algorithms are discussed further in chapter #.
Search Algorithm
6
N. S. AWARAYI
Implementation in C++
Sorting Operation
This operation orders elements in ascending or descending order. Elements can be sorted by
comparing elements next to each other and swapping them to follow a required order starting
from the first element. This process must be repeated until the elements are completely
sorted. Sorting algorithms are discussed further in chapter #.
Sorting Algorithm
Implementation in C++
7
N. S. AWARAYI
Merge Operation
The merge operation combines two or more arrays to form a single array. To merge two
arrays, a new array is created with a size equal to the sum of the size of both arrays. Elements
are then retrieved sequentially from the two arrays and inserted unto the new array.
Merge Algorithm
Implementation in C++
Multi-Dimensional Array
Multi-Dimensional arrays require more than just an index to manipulate an array. When two
indices is involved then the array represents a two-dimensional array. Two-dimensional,
three-dimensional arrays, etc are examples of multi-dimensional arrays.
Two dimensional Arrays
Two-Dimensional array is a group of adjoining memory locations of the same type arranged
in rows and columns. This array essentially looks like a matrix. Two dimensional arrays
require two indices for referencing cells, the first index representing the row index and the
8
N. S. AWARAYI
second refers to the column index. Figure 6 represents a two-dimensional array with 3 rows
and 4 columns.
Figure 6. 2-D array
Two-Dimensional Array Representation
To create a two-dimensional array in C++ programming language, you will be required to
specify the name, type and size (number of rows and columns) of the array. The code
segment below creates an array with 3 rows and 4 columns.
*[Logical Addressing of 2-d]
9
N. S. AWARAYI
Download