0% found this document useful (0 votes)
3 views

3.Introdcution Array

The document provides an introduction to arrays as a data structure, defining them as finite ordered sets of homogeneous elements with fixed sizes. It covers properties of arrays, types of indexing, and different types of arrays including one-dimensional, two-dimensional, and three-dimensional arrays, along with their declaration and memory representation. Additionally, it explains how to access elements in these arrays using appropriate syntax.
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)
3 views

3.Introdcution Array

The document provides an introduction to arrays as a data structure, defining them as finite ordered sets of homogeneous elements with fixed sizes. It covers properties of arrays, types of indexing, and different types of arrays including one-dimensional, two-dimensional, and three-dimensional arrays, along with their declaration and memory representation. Additionally, it explains how to access elements in these arrays using appropriate syntax.
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/ 24

DATA STRUCTURE

Quality Content for Outcome based Learning

INTRODUCTION TO ARRAYS

Ver. No.: 1.1 Copyright © 2021, ABES Engineering College


Array

An Array is a Data Structure which can be defined as a finite ordered


set of Homogenous elements

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

Contiguous Memory allocation

6
Dr. Amrita
Copyright Jyoti
© 2021, ABES Engineering College
Properties of Array

Random Access,

The general syntax to access an element is: <name_of_array>[<index>]

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

The array is represented in various ways based on the number of dimensions

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.

Declaration of one-dimensional array


Syntax:

<Data Type> <Arrayname> [<Array_Size>]

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

Accessing the element in one-dimensional array


Accessing its elements involves a single subscript.

Syntax: Arrayname[index]

Example:

To access 2nd element in the array A, we write A[1]

To access 9th element in the array A, we write A[8]


(Here, we that assume the first index is 0)

12
Dr. Amrita
Copyright Jyoti
© 2021, ABES Engineering College
Memory Representation of One-Dimensional
Array

The memory representation of one-dimensional array is shown in below diagram.

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

Declaration the two-dimensional array:

Syntax: <Data Type> <Arrayname> [m][n]

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

Declaration of three-dimensional array:


Syntax: <Data Type> <Arrayname> [m][n][o]

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]

If the array is declared as A[3][4][5], it will have a total of 3 x 4 x 5 = 60 elements.

21
Dr. Amrita
Copyright Jyoti
© 2021, ABES Engineering College
Three-Dimensional Array

Accessing the element in three-dimensional array


Accessing its elements involves three subscripts, one for each dimension.

Syntax: Arrayname[index1][index2][index3]

22
Dr. Amrita
Copyright Jyoti
© 2021, ABES Engineering College
Memory Representation of Three-Dimensional
Array

Memory Representation in Row Major Order

Here, the first dimension is considered as row.

(Here, we assume the first index for row and column is 0)

In the similar way Column major order arrangement can be done.

23
Dr. Amrita
Copyright Jyoti
© 2021, ABES Engineering College
Thank You

24
Dr. Amrita
Copyright Jyoti
© 2021, ABES Engineering College

You might also like