DSA Lecture-3 Arrays
DSA Lecture-3 Arrays
Chapter 3:
Arrays
Contents
• Introduction
• Memory Representation of Array
• Types of Array
– 1D/ Single Dimensional Array
– Multi-Dimensional Array
• Operations of arrays
Introduction
• An array is a collection of variables of the same
type that are referenced by a common name.
OR
• A list of finite number n of similar(homogeneous) data
elements.
• A[1],A[2],A[3]…..A[N]
• The number K in A[K] is called a subscript and A[K] is
called subscripted variable.
• Arrays are a way to group a number of items into larger
unit.
Introduction
• The elements of the array are referenced respectively
by an index set.
• The elements of the array are stored respectively in
successive memory locations.
• The number of elements is called the length or size of
the array.
• LENGTH = UB-LB+1
• UB: Upper bound (largest index)
• LB: Lower
10 20 bound
30 5(smallest
2 0 index)
4 100 LB=0
0 1 2 3 4 5 6 7 UB=7
Representation of Array in Memory
• Memory of computer is simply a sequence of addressed
locations.
1001 1002 1003 1004 1005
… … … … …
… … … … 1015
• data_type array_name[size-1][size-2]………[size-n];
Multidimensional arrays:
For example :
• int A[5][2][3];
• float B[2][5][3];
A[1][2]
Implementation of Two-dimensional array in
Memory
• While storing the elements of a 2-D array in
memory, these are allocated contiguous memory
locations.
• A two-dimensional array can be implemented in a
programming language in two ways :
• 1. Row-major implementation
• 2. Column-major implementation
Row-major implementation :
• Row-major implementation is a linearization
technique in which elements of array are read
from the keyboard row-wise.
• i.e. the complete first row is stored, then the
complete second row is stored and so on.
• For example, an array A [3] [3]is stored in the
memory as shown in Fig.(1) below :
Row-major implementation :
• The storage can be clearly understood by arranging
array as matrix as shown below :
Column-major implementation :
• Column-major implementation is a linearization
technique in which elements of array are read from
the keyboard column-wise.
• i.e. the complete first column is stored, then the
complete second column is stored and so on.
• For example, an array a[3][3] is stored in the
memory as shown in Fig.(1) below :
Column-major implementation :
• The storage can be clearly understood by arranging
array as matrix as shown below :
Two Dimensional Array declartion :
• data_type array_name[rowSize][colSize];
• For e.g.
• int A[4][3];
• Where int is data type, A is array variable_name, 4
is row size and 3 is column size.
Two Dimensional Array initialization :
• data_type array_name[rowSize][colSize]={
{1st row elements},
{2nd row elements},
………
};
• For e.g.
• int A[4][3]={{1,2,3}, {4,5,6,}, {7,8,9}, {10,11,12}};