3.Introdcution Array
3.Introdcution Array
INTRODUCTION TO ARRAYS
2
Dr. Amrita
Copyright Jyoti
© 2021, ABES Engineering College
Properties of Array
Finite means fixed element , Arrays have a fixed size where the size of the array is defined when the array is
declared. In the below given figure, the size of the array is fixed i.e., the size 5 is fixed and we cannot add
one more element in the array
3
Dr. Amrita
Copyright Jyoti
© 2021, ABES Engineering College
Properties of Array
Ordered Set means every number will be in Sequence and will be denoted by numbers called Index (“indices”
in plural) or ”subscript.
4
Dr. Amrita
Copyright Jyoti
© 2021, ABES Engineering College
Properties of Array
Homogenous elements means Data type of all the elements will be same
5
Dr. Amrita
Copyright Jyoti
© 2021, ABES Engineering College
Properties of Array
6
Dr. Amrita
Copyright Jyoti
© 2021, ABES Engineering College
Properties of Array
Random Access,
7
Dr. Amrita
Copyright Jyoti
© 2021, ABES Engineering College
There are 3 types of indexing provided by different languages to access the array.
0 (zero-based indexing): The first element of the array is indexed by a subscript 0. The index of nth element is
“n-1” in this case. (C, C++, etc.).
1 (one-based indexing): The first element of the array is indexed by the subscript 1. (Basic, MATLAB, R,
Mathematica
n (n-based indexing): The base index of an array can be freely chosen. Usually, programming languages
allowing n-based indexing also allow negative index values. (Fortran, Pascal, ALGOL, etc.)
8
Dr. Amrita
Copyright Jyoti
© 2021, ABES Engineering College
Types Of Array
1. One-Dimensional array
2. Two-Dimensional array
3. Three-Dimensional array
9
Dr. Amrita
Copyright Jyoti
© 2021, ABES Engineering College
One-Dimensional Array
A one-dimensional array (or single dimension array) is an array with one subscript only.
Example:
Declaration of one-dimensional array "A" having "int" data type and size 10 elements in C.
int A[10]
10
Dr. Amrita
Copyright Jyoti
© 2021, ABES Engineering College
One-Dimensional Array
11
Dr. Amrita
Copyright Jyoti
© 2021, ABES Engineering College
One-Dimensional Array
Syntax: Arrayname[index]
Example:
12
Dr. Amrita
Copyright Jyoti
© 2021, ABES Engineering College
Memory Representation of One-Dimensional
Array
In the above diagram A[0], A[1], A[2],. . . , A[9] are the array elements. The address mentioned for these
elements represent the physical location of data elements in the main memory. It is assumed that each element
requires 4 bytes for storage in this scenario.
13
Dr. Amrita
Copyright Jyoti
© 2021, ABES Engineering College
Two-Dimensional Array
A two-dimensional array (2-D array) has two subscripts. The elements are stored in the form of rows and
columns. It can also be termed as an array of one-dimensional arrays.
Matrix is an example of two-dimensional array
14
Dr. Amrita
Copyright Jyoti
© 2021, ABES Engineering College
Two-Dimensional Array
Where,
m = Number of rows
n = Number of columns
Example: Declaration of two-dimensional array “A” having “int” datatype and row size 6 and column size 5.
int A[6][5]
15
Dr. Amrita
Copyright Jyoti
© 2021, ABES Engineering College
Accessing the element in two-dimensional array
Accessing its elements involves two subscripts; one for row and second for column.
Syntax:
Arrayname[ row_index][column_index]
Example:
To access 2nd element of 1st row in the array A, we write A[0][1]
16
Dr. Amrita
Copyright Jyoti
© 2021, ABES Engineering College
Memory Representation of Two-Dimensional
Array
There are two-ways by which the 2D array elements can be represented in Memory.
a)Row-major Representation
b) Column-major Representation
17
Dr. Amrita
Copyright Jyoti
© 2021, ABES Engineering College
Row Major
Row-major Representation:
In row-major order, storage of the elements in the memory is row-wise i.e. storage of elements of first row is
followed by the storage of elements of second row and so on so forth.
18
Dr. Amrita
Copyright Jyoti
© 2021, ABES Engineering College
Column major
Column-major Representation:
In column-major order, elements are stored column wise i.e., storage of elements of the first column followed
by storage of second column elements and so on so forth.
19
Dr. Amrita
Copyright Jyoti
© 2021, ABES Engineering College
Three-Dimensional Array
When an array is represented in the form of 3 different dimensions, it is called 3-D Array. It can also be called
as an array of 2-dimensional arrays.
3-Dimensional array which has 3 dimensions named U1, U2, and U3.
20
Dr. Amrita
Copyright Jyoti
© 2021, ABES Engineering College
Three-Dimensional Array
Where,
m = 1st Dimension
n = 2nd Dimension
o = 3rd Dimension
Example: Declaration of three-dimensional array “A” having “int” datatype with first dimension size 6, second
dimension size 5, third dimension size 4.
int A[6][5][4]
21
Dr. Amrita
Copyright Jyoti
© 2021, ABES Engineering College
Three-Dimensional Array
Syntax: Arrayname[index1][index2][index3]
22
Dr. Amrita
Copyright Jyoti
© 2021, ABES Engineering College
Memory Representation of Three-Dimensional
Array
23
Dr. Amrita
Copyright Jyoti
© 2021, ABES Engineering College
Thank You
24
Dr. Amrita
Copyright Jyoti
© 2021, ABES Engineering College