0% found this document useful (0 votes)
9 views17 pages

Module 4 - Arrays and Indexing Types

The document provides an overview of linear arrays, including their definitions, properties, and types such as one-dimensional and two-dimensional arrays. It explains memory representation, indexing, and the differences between static and dynamic arrays, along with algorithms for traversing, inserting, and deleting elements in an array. Additionally, it covers the concepts of length and size of arrays, as well as common operations and pitfalls associated with array manipulation.

Uploaded by

egujosh321
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views17 pages

Module 4 - Arrays and Indexing Types

The document provides an overview of linear arrays, including their definitions, properties, and types such as one-dimensional and two-dimensional arrays. It explains memory representation, indexing, and the differences between static and dynamic arrays, along with algorithms for traversing, inserting, and deleting elements in an array. Additionally, it covers the concepts of length and size of arrays, as well as common operations and pitfalls associated with array manipulation.

Uploaded by

egujosh321
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Linear Arrays

Definition: An array is a collection of homogeneous (same-type) data


items stored in contiguous memory locations. E.g., an array of type int can
only store integer elements and cannot allow elements of other types. A
Linear array is a one-dimensional array
Prominent properties of Arrays:
It can only store homogenous (same-type) data
Array elements are stored in contiguous memory locations
Common terminologies
Element: An item stored in an array
Index: A numerical index attached to each memory for identifying an
element in the array
Indexing Types and Memory Representation (1)
One-Dimensional Array: The array below is one-dimensional (linear)

 The base address is the address of the first element in the array.
 Address of any other element is calculated as follows:

ADDRESS = memory location of Kth element of the array (To be calculated)


ARRAY – name of the ARRAY
WORDLENGTH – number of bytes required to store one element
LOWERBOUND – index of the first element of the array
Indexing Types and Memory Representation (2)
To calculate memory address of a given element say 6th element of the array in
the snapshot shown below:
Indexing Types and Memory Representation (3)
K=6
WORDLENGTH=2 (integer data)
LOWERBOUND =1
BASEADDRESS = 1001
ADDRESS(ARRAY[6])= 1001+ 2*( 6-1) =1011
This is the address of memory location where 6th element (34) is stored as
visible in the figure above

Two-dimensional Array
a two dimensional array is a tabular representation of data where elements
are stored in rows and columns
A two dimensional array is actually a collection of M X N elements which has
M rows and N columns
Indexing Types and Memory Representation (4)
In the example shown the first index values row=2 column=3 is used to
access element 56
Indexing Types and Memory Representation (5)
Row Major Representation
 In the row major representation the storage of array elements takes place
row wise
 All elements of first row of the array are first stored in sequence followed
by second row and then third, fourth and so on
The 2-dimensional array given in previous examples is stored in row-major
representation in the figure below
Row Number 1 Row Number 2 Row Number 3
22 33 44 55 12 34 56 78 10 5 20 11
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012

To find the Address of any element located at row and column is


calculated by using the formula
Indexing Types and Memory Representation (6)

Where
N = number of columns of the array
BASEADDRESS = Address of first element of the 2-D array
In this example to calculate memory address of a given element (44), say in 1st
row and 3rd column you will put the values in the formula
I=1, J=3, N=4, WORDLENGTH=1 (assuming only one byte is required to store
these small ints ), BASEADDRESS = 1001
ADDRESS(ARRAY[1,3])= 1001+ 1*( 4*(1-1) + (3-1)) =1003
This is the address of memory location where 44 is stored as visible in the
previous figure
Indexing Types and Memory Representation (7)
Column Major Representation
 In the column major representation the storage of array elements takes
place column wise
 All elements of first column of the array are first stored in sequence
followed by second column and then third, fourth and so on
The 2-dimensional array given in previous examples is stored in column-
major representation in the figure below
Column Number 1 Column Number 2 Column Number 3 Column Number 4
22 12 10 33 34 5 44 56 20 55 78 11
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012

To find the Address of any element located at row and column is


calculated by using the formula
Indexing Types and Memory Representation (8)

Where
M = number of rows of the array
BASEADDRESS = Address of first element of the 2-D array
In this example to calculate memory address of a given element (44), say in 1st
row and 3rd column you will put the values in the formula
I=1, J=3, M=3, WORDLENGTH=1 (assuming only one byte is required to store
these small ints ), BASEADDRESS = 1001
ADDRESS(ARRAY[3,1])= 1001+ 1*( 3*(3-1) + (1-1)) =1007
This is the address of memory location where 44 is stored as visible in the
previous figure
Length vs Size of an array
Length: The Length of an array is the number of elements in the array.
Consider the array declaration in Java, int[] num = new int[10];
 Specifies that num is an array containing 10 integer values
 Starting, unless otherwise stated, from num[0] to num[9]

Size: This is the number of bytes that the array occupies in memory
 The size depends on the size of the data element; e.g., int = 2 bytes (in C)
 Size must be defined before storing the elements; e.g., int[10] = 20 bytes
 The size depends on the programming language; e.g., int = 4 in Java
 Based on the size, an array may be of two types: static or dynamic
Static vs Dynamic Array (1)
Static Arrays: have their sizes predefined during declaration (creation)
 They are known as fixed arrays or fixed-length arrays
 Array elements may be defined during declaration e.g., in Java
int[] num = {3,8,4,7,9,5,11,2,6,10}; OR
 The array size may be defined during declaration e.g., in java
int[] num = new int[10];
 The array cannot shrink or expand once the size is defined
 It is only the compiler that can destroy the array
 Additional element is not possible since the user is not sure of free
memory for it
Static vs Dynamic Array (2)
Dynamic Arrays: Elements can be added or removed at run time
 Dynamic arrays don’t have any fixed length or size
 Standard library or built-in functions are available in most programming
languages to create and manage dynamic arrays. The Table below shows:
Programming Class Addition of Removal of
Language element element
Java java.util.ArrayList Add remove
C++ #include <list> Insert erase
std::list
C# System.Collections.Generic.List Add Remove
Python List Append remove
Manipulating the Contents of an Array (1)
 To use the values stored in an array, each element is referenced individually
 Indexing of each element starts at zero
 Individual array elements can be used as any variable would
 For example, one can assign a number to it, use it in a calculation, like this:
num[0] = 5; num[2] = num[0]*7+num[1];
 Loops can be used to perform the same or similar operations on all the entries
of an array in turn
 A major advantage of an array over using many individual variables; e.g.,
int[] list = {1, 4, -3, 66, 19};
for (int i=0; i<5; i++) {
System.out.println(list[i]);
}
Manipulating the Contents of an Array (2)
 Be sure to start with int i = 0 to use the first array element
 Note that the loop variable i runs from 0 to 4 as the indexing starts from 0
 A common mistake is to let the loop variable increase to too high a value
 If a variable goes beyond the end of an array, the error will not be picked up
by the compiler
 It will result in an ArrayaIndexOutOfBoundException when the code is run
 To use the length of an array in a code, append .length to the array name
 For example list.length will return 5
 It is better to use list.length rather than typing 5 explicitly
 It makes the code more general, requiring less alteration if the length of the
array were altered
Hands-on On Array Operations Algorithms (1)
Algorithm Traverse (DATA, LB, UB)
[This algorithm traverses the linear array DATA. UB is upper bound of array DATA. LB is the lower
bound. Array DATA stores N elements where N=UB-LB+1].
Step1: CTR=LB [Initialise the counter CTR a local variable for this algorithm]
Step 2: WHILE CTR<=UB repeat steps 3 and 4
Step3: PROCESS the element DATA[CTR]
[PROCESS is generic task that can be used to count the elements, perform
some calculation, sum all the elements of the array, display them or assign
new values]
Step4: CTR=CTR+1 [Increment the counter]
Step 5: Exit
Hands-on On Array Operations Algorithms (2)
Algorithm InsertLA (DATA, N, ITEM, LOC)
[This algorithm inserts new element ITEM in linear array DATA with N elements. If LOC=1 it
means the element has to insert in beginning. If LOC =N+1 it means the element have to be
inserted at the end. If LOC = j it means the elements have to be inserted at jth Location]
Step 1: i=N [Initialize counter i with index of last element]
Step 2: While i>=LOC repeat steps 3 and 4
Step 3: DATA[i+1] = DATA[i] [Move the current element one position backwards]
Step 4: i=i-1 [Decrement counter i]
Step 5: DATA[LOC]=ITEM [Insert new element at the Location]
Step 6: N=N+1 [Update total number of array elements]
Exit
Hands-on On Array Operations Algorithms (3)

Algorithm DeleteLA (DATA, N, ITEM, LOC)


[This algorithm deletes an element at jth position in a linear array DATA with N elements and stores in ITEM. If LOC=1 it
means the element to be deleted is at the beginning. If LOC =N it means the element be deleted is at the end. If LOC = j it
means the elements have to be deleted is at at jth Location]

Step 1: i = j [Initialize counter i with index of element to be deleted]


Step 2: ITEM=DATA[j] [Store the element to be deleted in ITEM]
Step 3: While i < N repeat steps 4 and 5
Step 4: DATA[i] = DATA[i+1] [Move the current element one position forward]
Step 5: i = i+1 [Increment counter i]

Step 6: N = N-1 [Update total number of array elements]


Exit

You might also like