Lecture 2: Arrays - The Institute of Finance Management (IFM)

Download Report

Transcript Lecture 2: Arrays - The Institute of Finance Management (IFM)

Data Structure and Algorithm: CIT231

Lecture 2: Arrays Bajuna Salehe [email protected]

Arrays

 Data structures can be classified into two categories:  Linear Data Structure    Non Linear Data Structure Linear data structure – its elements form a sequence.

Non linear data structure – its elements do not form a sequence.

Arrays

 Two ways of representing linear data structure in memory.

 Through linear relationship between the elements of by means of sequential memory locations. Such linear structures are called

Arrays

 - An array of 5 integers Through linear relationship between the elements represented by means of

pointers

or

links

.

Arrays

 In linked list each node contains the data and address of the next node.

  - A linked list containing 4 integers  Arrays are useful when the number of elements to be stored are is fixed.

Easy to traverse, search and sort Linked lists are used when the number of data items in the collection are likely to vary. They are hard to maintain.

So What are Arrays?

   An array is a finite collection of similar elements stored in adjacent memory locations.

By ‘

finite

’ we mean that there are specific number of elements in an array.

By ‘

similar

’ we mean that all the elements in array are of the same type.

 E.g An array may contain all integers or all characters.

What are arrays

 An array containing ‘n’ number of elements is referenced using an index that varies from

0

to

n-1.

  For example, the elements of an array

arr[n]

containing n elements are denoted by

arr[0]

,

arr[1]

,

arr[2]

,…,

arr[n-1]

.

0

is the lower bound and

n-1

of the array.

is the upper bound  The lowest index of an array is called its

lower bound

 The highest index is called its

upper bound

 The number of elements in the array is called its

range

.

So What are Arrays?

 Intuitively an array is a set of an index and a value. For each index there is a value associated with it.

 Arrays are fundamental data structures in that they have direct correspondence with memory systems on virtually all computers.

 To retrieve the contents of a word from a memory in a machine language, we provide an address

So What are Arrays?

 Thus we could think of the entire computer memory as an array, with the memory addresses corresponding to array indices.

 In C++ it is the responsibility of programmer to use indices that are non negative and smaller than the array size.

 Neglecting of that responsibility is the one of the common programming mistakes.

Arrays Categories

 There are two types of arrays.

  One dimensional arrays Multidimensional arrays  Multidimensional array can be a 2-D array, 3 D array, 4-D array etc.

 To know that an array is 1-D or 2-D is determined by array syntax used to declare that array.

Array Declaration

 The following examples show how to declare one-dimensional as well as multidimensional arrays.

- arr1[5] – A 1-D array holding 5 elements.

- arr1[2][5] – A 2-D array with 2 rows and 5 columns holding 10 elements.

- arr1[2][5][3] – A 3-D array with 2-D arrays each of which is having 5 rows and 3 columns thus holding totally 30 elemnts.

Example of the Application of Dimension Arrays

 If we want to store and process the premier league results we can use 2 D arrays:

Array Declaration

 Array declaration necessitate that any array cannot be used before you have declared it.

 Note the elements field in the brackets [] must be a constant value since arrays are blocks of non dynamic memory whose size must be determined before execution  For variable length use dynamic memory

Initialising arrays

 The elements of global and static arrays are automatic initialised with their default values.

 We have the possibility to assign initial values to each one of its elements by enclosing the values in braces { } int billy [5] = { 16, 2, 77, 40, 12071 }  This declaration would have created an array like this:

Initialising arrays

 The amount of values between braces { } must not be larger than the number of elements that we declare for the array between square brackets [ ].

 For example, in the example of array billy we have declared that it has 5 elements and in the list of initial values within braces { } we have specified 5 values, one for each element.

Initialising arrays

 When an initialization of values is provided for an array, C++ allows the possibility of leaving the square brackets empty [ ].  In this case, the compiler will assume a size for the array that matches the number of values included between braces { }: Int billy [] = { 16, 2, 77, 40, 1207};  After this declaration, array billy would be 5 ints long, since we have provided 5 initialization values.

Accessing the value of an Array

 The elements of an array can be accessed individually as if it is a normal variable. The format is as: Name[index]  Following the previous examples in which billy had 5 elements and each of those elements was of type int, the name which we can use to refer to each element is the following:

Accessing the value of an Array

 For example, to store the value 75 in the third element of billy, we could write the following statement: billy[2] = 75;  For example, to store the value 75 in the third element of billy, we could write the following statement: billy[2] = 75;

Accessing the value of an Array

To pass the value of the third element of

billy

to a variable called

a

, we could write:

a=billy[2];  In C++ it is syntactically correct to exceed the valid range of indices for an array.

 This can create problems, since accessing out-of-range elements do not cause compilation errors but can cause runtime errors.

Array Operations

 There are several operations that can be performed on arrays.

Operation

Insertion Deletion Traversal Search Sorting Merging Reversing

Description

Adding a new element to an array Removing an element from an array Processing each element in the array Finding the location of an element with a given value Organising the element in some order Combining two arrays into single array Reversing the elements of an array

Array Insertion

 There are generally two scenarios for inserting a value into an array.

  Inserting a value into a specified location of an array.

Inserting a value into a sorted array

Array Insertion

 If a value needs to be inserted into a specified location, the programmer can simply follow the following steps.

   Insert the value into the position Move all other following elements to the next position Increment the size of the array.

Array Insertion

 If a value needs to be inserted into a sorted array, the programmer must first find the location in the array where the value should be placed in order to keep the original order in intact.

 Once the target location is found, the steps outlined in the previous slide can then be executed.

Array Deletion

 Array deletion aimed at removing a certain element at particular position in that array.

 This operation can be carried out through a deletion function which deletes the element at the given position  While doing so it shifts the numbers placed after the position from where the number is to be deleted, one place to the left of their existing positions.

 The place that is vacant after deletion of an element is filled with 0.

Reversing Array

 In the reversing operation the entire array is reversed by swapping the elements.

Merging of Two Arrays

 Merging of two arrays involve two steps.

  Sorting the arrays are to be merged.

Adding the sorted elements of both the arrays to a new array in a sorted order.