0% found this document useful (0 votes)
31 views15 pages

‏لقطة شاشة 2024-05-10 في 7.02.41 م

The document provides an overview of arrays, a fundamental linear data structure in computer science, detailing their definition, memory allocation, and features. It explains how to access elements in one-dimensional and multi-dimensional arrays using various indexing methods and formulas. Additionally, it covers the concept of triangular matrices and basic operations related to arrays, including memory address calculations for different arrangements.
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)
31 views15 pages

‏لقطة شاشة 2024-05-10 في 7.02.41 م

The document provides an overview of arrays, a fundamental linear data structure in computer science, detailing their definition, memory allocation, and features. It explains how to access elements in one-dimensional and multi-dimensional arrays using various indexing methods and formulas. Additionally, it covers the concept of triangular matrices and basic operations related to arrays, including memory address calculations for different arrangements.
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/ 15

Data Structures Academic Year 2023-2024

Dr. Raidah Salim Khudeyer


What is an Array?

In computer science, an array is type of linear data structure consisting of a


collection of elements of same memory size, same type stored at contiguous
memory locations.
 It’s one of the most popular and simple data structures and is often used to
implement other data structures.
 It is stored such that the position of each element can be computed from its
index tuple by a mathematical formula.
 They exist in both single dimension and multiple dimensions.

Why are arrays required?


Arrays are useful because -
 Sorting and searching a value in an array is easier.
 Arrays are best to process multiple values quickly and easily.
 Arrays are good for storing multiple values in a single variable- In computer
programming; most cases require storing a large number of data of a similar
type. To store such an amount of data, we need to define a large number of
variables. It would be very difficult to remember the names of all the
variables while writing the programs. Instead of naming all the variables with
a different name, it is better to define an array and store all the elements into
it.

Memory allocation of an array


All the data elements of an array are stored at contiguous locations in the main
memory. The name of the array represents the base address or the address of the
first element in the main memory. Each element of the array is represented by
proper indexing.

In the below ways to define the indexing of an array:


1. 0 (zero-based indexing): The first element of the array will be arr[0].
2. 1 (one-based indexing): The first element of the array will be arr[1].
3. n (n - based indexing): The first element of the array can reside at any
random index number.

1
Data Structures Academic Year 2023-2024
Dr. Raidah Salim Khudeyer

In the above image, we have shown:

 The memory allocation of an array arr of size 5.


 The array follows a 0-based indexing approach.
 The base address of the array is 100 bytes. It is the address of arr[0].
 The size of the data type used is 4 bytes; therefore, each element will take 4
bytes in the memory.

Features of Arrays
An array is:
 Linear structure: means organize in memory as linear order.
 Homogeneous structure: all components in the structure are of the same
data type.
 Finite structure: indicates that there is a last element.
 Fixed size structure: mean that the size of the array must be known at
compile time.
 Contiguously structure: means that there is a first element, a second
element, and so on.
 The component selection mechanism of an array is direct access (random
access), which means we can access any element directly (by using specific
equation), without first accessing the preceding elements. This makes
accessing elements by position faster.

2
Data Structures Academic Year 2023-2024
Dr. Raidah Salim Khudeyer

How to access an element from the array?


To access any random element from the array we required the information given
below:
 Base Address of the array.
 Size of an element in bytes.
 Type of indexing, array follows.

The formula to calculate the address to access an array element:

in one-dimensional array
 It can be visualized as a list.

 We can find out the location of any element in array by using following
formulas:

Address of element A[i] = base address(BA) + size * ( i - first index)

Here, size represents the memory (size of element) taken by the primitive data
types.
Example 1: Suppose an array, A[5] having BA = 1000 and size of an element = 2
bytes, find the location of A[3].
L(A[3]) = 1000+ 2 x [3-0]
= 1000 + 6
= 1006

3
Data Structures Academic Year 2023-2024
Dr. Raidah Salim Khudeyer
Example 2: Suppose an array, A[-10 ... 2 ] having BA = 999 and size of an element = 2
bytes, find the location of A[-1].
L(A[-1]) = 999 + 2 x [(-1) - (-10)]
= 999 + 18
= 1017

In two-dimensional array
 It can be visualized as a table consisting of rows and columns. The element in
a two-dimensional array is accessed by specifying the row and column
indexes of the item in the array.

 Two methods for arranging two or multidimensional:


1. Row-major order
2. Column-major order

 In row-major order:
o Consecutive ( ‫ )ﻣﺘﺘﺎ‬elements of the rows of the array are contiguous in
memory.
o Used in C, C++, PL/I, Pascal, Python and Java.
 in column-major order:
o Consecutive elements of the columns are contiguous.
o Used in FORTRAN, OpenGL and Matlab.

4
Data Structures Academic Year 2023-2024
Dr. Raidah Salim Khudeyer

 We can find out the location of any element in array (NXM) by using
following formulas:

1. In case of Row Major Order:


Address of element A[i,j] = base address(BA) +( [i-1]*M+ [ j-1 ]) * size

Example: finding the location (address) of element in 2D


Suppose A 3 x 4 (N=3 and M=4) integer array A is show as below and base
address= 1000 and number of bytes=2. Find the location of A [3, 2]:

Address Elements

1000 10
20 Address of element A[i,j] = base address(BA) +( [i-1]*M+ [ j-1 ]) * size
1002
1004 50
60 Address of element A[3,2] = 1000 + 2 [4 (3-1) + (2-1)]
1006
90 = 1000 + 2 [4 (2) + 1]
1008
= 1000 + 2 [8 + 1]
1010 40
= 1000 + 2 [9]
1012 30
= 1000 + 18
1014 80
= 1018
1016 75
1018 55
1020 65
1022 79

5
Data Structures Academic Year 2023-2024
Dr. Raidah Salim Khudeyer

2. In case of Column Major Order:


Address of element A[i,j] = base address(BA) +( [j-1]*N+ [ i-1 ]) * size

Example: finding the location (address) of element in 2D


Suppose A 3 x 4 (N=3 and M=4) integer array A and base address =1000 and
number of bytes=2. Find the location of A [3, 2]:
Address Elements

1000 10 Address of element A[i,j] = base address(BA) +( [j-1]*N+ [ i-1 ]) * size


1002 90
1004 75 Address of element A[3,2] = 1000 + 2 [3 (2-1) + (3-1)]
1006 20 = 1000 + 2 [3 (1) + 2]
1008 40 = 1000 + 2 [3 + 2]
1010 = 1000 + 2 [5]
55
1012 = 1000 + 10
50
1014 = 1010
30
1016 65
1018 60
1020 80
1022
79

Note: if the value of size not determine, it suppose equal to 1.

H.W.
1. You have the matrix A [3, 4] and the base address is 1500. By using rows major order
find the address of the element A [2, 3].

2. You have the matrix B [5, 6] and the base address is 500. By using two method of arrange
matrix in memory find the address of the element B [2, 3].

In case three Dimensional Arrays, memory-address of the element A[i,j,K] with dimension
(NXMXR) is given by:
In case of Row Major Order:
Address of element A[i,j,k] = base address(BA) + ([k-1]*N*M+ [i-1]*M+ (j-1))*size

In case of Column Major Order:


Address of element A[i,j,k] = base address(BA) + ([k-1]*N*M+ [j-1]*N+ (i-1))*size

Where:
R: number of levels
N: number of rows
M: number of column

6
Data Structures Academic Year 2023-2024
Dr. Raidah Salim Khudeyer

Example: Suppose A3 x 3 X 3(N=3, M=3 and R=3) integer array A and base address(BA) =1000
and number of bytes (size) =2. Find the location of A [3, 2, 2] by using two method of arrange
matrix in memory:
1. Row order
Address of element A[3,2,2] = 1000+2(3*3*(2-1)+3*(3-1)+2-1)
= 1000 + 2(9+6+1)
= 1032
Physical structure Logical structure

2. Physical structure
Column Logical structure
order

7
Data Structures Academic Year 2023-2024
Dr. Raidah Salim Khudeyer
2. Column order:
Address of element A[3,2,2] = 1000+2(3*3*(2-1)+3*(2-1)+3-1)
= 1000 + 2(9+3+2)
= 1028
H. W.
You have the matrix A [5, 3, 2] and base address =100, by using two method of arrange
matrix in memory find the address of the element A [2, 2,3].

In general, we can find out the location of any element in array (NXMXRXl) by using
following formulas:
In case of Row Major Order:
Address of element A[i,j,k,l] = BA + size* (NMR(l-1)+NM(k-1)+M(i-1)+j-LB)
In case of Column Major Order:
Address of element A[i,j,k,l] = BA + size* (NMR(l-1)+NM(k-1)+N(j-1)+i-LB)
Where LB is lower bound

Triangular Matrix
A triangular matrix is a special kind of square matrix. A square matrix is
called lower triangular if all the entries above the main diagonal are zero. Similarly, a
square matrix is called upper triangular if all the entries below the main diagonal are
zero.

8
Data Structures Academic Year 2023-2024
Dr. Raidah Salim Khudeyer

We can find out the location of a[2,2]by using following formulas:


1. Upper triangular
In case of Row Major Order:
Address of element A[i,j] = BA + size*( ( (i-1)*M - (i-1)*i/2) + (j-1) )

Address Elements

1000 44 Suppose BA =1000, i=2, j=2 and size=2


150 Address of element A[2,2] = 1000 + 2*((2-1)*3-(2-1)*2/2+2-1)
1002
58 = 1000 + 2(3-1+1)
1004
34 = 1000 + 2*3
1006
24 = 1000 + 6
1008
= 1006
1010 33

In case of Column Major Order:


Address of element A[i,j] = BA + size*( (j-1) *j / 2 + ( i-1 ) )

Address Elements

1000 44 Suppose BA = 1000, i=2, j=2 and size=2


150 Address of element A[2,j2 = 1000 + 2*((2-1)*2/2+2-1)
1002
34 = 1000 + 2(1+1)
1004
58 = 1000 + 2*2
1006
24 = 1000 + 4
1008
33 = 1004
1010

2. Lower triangular
In case of Row Major Order:
Address of element A[i,j] =BA + size*((i-1) * i /2 + ([j-1))

Address Elements

1000 44 Suppose BA = 1000, i=2, j=2 and size=2


58 Address of element A[2,2] = 1000 + 2*((2-1)*2/2+2-1)
1002
34 = 1000 + 2(1+1)
1004
150 = 1000 + 2*2
1006
24 = 1000 + 4
1008
= 1004
1010 33

9
Data Structures Academic Year 2023-2024
Dr. Raidah Salim Khudeyer
In case of Column Major Order:
Loc (A [i,j]) = Base(A) + w( ( (j-1) *N - (j-1)*j/2 ) +(i-1))

Address Elements

1000 44 Suppose Base (A)=1000 , i=2, j=2 and w=2


58 LOC (A [2,2]) = 1000 + 2*((2-1)*3 - (2-1)*2/2+2-1))
1002
150 = 1000 + 2(3-1+1)
1004
34 = 1000 + 2*3
1006
24 = 1000 + 6
1008
= 1006
1010 33

Q: How determine the number of array elements?


Ans.: To determine the number of any array elements (positions) by applying the following
equation:

Where:
n is dimensions of the array
U: upper bound for dimension i
L: lower bound for dimension i

Example1: Find the number of positions required to store the array: A [5]

=5-0+1=6

Example2: Find the number of positions required to store the matrix: A [5, 6]

(5-0+1)*(6-0+1)=6*7=42

Example3: Find the number of positions required to store the matrix:


A[2..5, 6...8]

= (5-2+1)*(8-6+1) = 4*3 =12

10
Data Structures Academic Year 2023-2024
Dr. Raidah Salim Khudeyer
Basic operations
 Traversal - This operation is used to print the elements of the array.
 Insertion - It is used to add an element at a particular index.
 Deletion - It is used to delete an element from a particular index.
 Search - It is used to search an element using the given index or by the value.
 Update - It updates an element at a particular index.

Q: How to delete and insert an element from array?


Ans.: Insertion and deletion at particular position is complex, it require shifting as in
the following examples.

Algorithm for Deletion: (Deletion from a array): DELETE (list, n, k)


Here list is one dimensional array with n elements and k is the positive
integer such that k<=n. This algorithm deletes the kth element from list.
1. Set item = list[k]
2. Repeat for j = k to n – 1
3. list[j] = list [j +1]
4. End loop
5. Set n: = n-1
6. end

Algorithm for Insertion: INSERT (list, n, k, item) : Here list is a linear array with n
elements and k is a positive integer such that k<=n. The algorithm inserts an element
item into the kth position in list.
1. Set j = n-1.
2. Repeat Steps 3 and 4 while j >= k
3. list [j + 1] = list [j]
4. Set j = j-1
5. End loop

11
Data Structures Academic Year 2023-2024
Dr. Raidah Salim Khudeyer
6. Set list[k] = item
7. Set n = n+1
8. end

Algorithm for searching (linear search): search (list, n, item) : Here list is a linear
array with n elements and k is a positive integer such that k<=n. The algorithm
inserts an element item into the kth position in list.
1: Set i to 1
2: if i > n then go to step 7
3: if list[i] = item then go to step 6
4: Set i to i + 1
5: Go to Step 2
6: Print Element item Found at index i and go to step 8
7: Print element not found
8: end

Arrays in Java
Following are some important point about Java arrays.
 Since arrays are objects in Java, we can find their length using member length.
 A Java array variable can also be declared like other variables with [] the data
type.
 The variables in the array have an index beginning from 0.
 In Java, all objects are dynamically allocated on Heap, when we only declare a
variable of a class type, only a reference is created (memory is not allocated for
the object). To allocate memory to an object, we must use new(). So the object
is always allocated memory on heap.

12
Data Structures Academic Year 2023-2024
Dr. Raidah Salim Khudeyer
One-Dimensional Arrays:
The general form of a one-dimensional array declaration is:
type array_name[];
OR
type [] array_name;

Example:
// both are valid declarations
int intArray[]; or int[] intArray;
float floatArray[];
double doubleArray[];
char charArray[];

Although the above first declaration establishes the fact that intArray is an array
variable, no array actually exists. It simply tells to the compiler that this(intArray)
variable will hold an array of the integer type. To link intArray with an actual,
physical array of integers, you must allocate one using new and assign it to intArray.

The general form of new as it applies to one-dimensional arrays appears as


follows:
Array_name = new type [size];
Here, type specifies the type of data being allocated, size specifies the number of
elements in the array, and var-name is the name of array variable that is linked to
the array. That is, to use new to allocate an array, you must specify the type and
number of elements to allocate.

Example:
int intArray[]; //declaring array
intArray = new int[20]; // allocating memory to array
OR
int[] intArray = new int[20]; // combining both statements in one

Notes:
1. The elements in the array allocated by new will automatically be initialized
to zero (for numeric types), false (for boolean), or null (for reference types).
2. Obtaining an array is a two-step process. First, you must declare a variable of
the desired array type. Second, you must allocate the memory that will hold the
array, using new, and assign it to the array variable. Thus, in Java all arrays are
dynamically allocated.

13
Data Structures Academic Year 2023-2024
Dr. Raidah Salim Khudeyer
Two-Dimensional Arrays
In Java Multidimensional arrays are arrays of arrays with each element of the
array holding the reference of other array. For example, 2D array is a 1D array of
references to 1D array, each of these 1D arrays (rows) can have a different length,
this 2D array is called "Jagged array".
Examples of declare two-dimensional array:
int[][] intArray = new int[10][20]; //a 2D array or matrix
int[][][] intArray = new int[10][20][10]; //a 3D array

Example: In the following memory layout of a jagged array numArr.


int[][] numArr = { {1,2,3}, {4,5,6,7}, {8,9} };

And jagged arrays can be created with the following code:


int [][]c;
c=new int[2][];
c[0]=new int[5];
c[1]=new int[3];

Q: What happens if we try to access element outside the array size?


Ans: Compiler error, indicate that array has been accessed with an illegal index. The
index is either negative or greater than or equal to size of array.

14

You might also like