DATA STRUCTURE
(KCS-301)
Arrays
(UNIT-1)
Course Instructor:
Mr Deepak Vishwakarma
(Assistant Professor, IT Department, KIET, Ghaziabad)
Syllabus (Unit-1)
DATA STRUCTURE (KCS-301)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
Basics of Array
An array is a type of data structure that stores a fixed-size of a homogeneous
collection of data. In short, we can say that array is a collection of variables of
the same type.
For example, if you want to store 100 integers, you can create a 1D array for it.
int data[100];
Declaration of a 1-dimentional array
dataType arrayName[arraySize];
Example: float mark[5];
Here, we declared an array, mark, of floating-point type. And its size is 5.
Meaning, it can hold 5 floating-point values.
It's important to note that the size and type of an array cannot be changed once it
is declared.
DATA STRUCTURE (KCS-301)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
Example of Array
DATA STRUCTURE (KCS-301)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
Dimensions of an Array
A one-dimensional array is like a list.
float marks[5];
A two dimensional array is like a table.
int x[3][3];
The C language places no limits on the
number of dimensions in an array,
though specific implementations may.
int a[2][3][4];
DATA STRUCTURE (KCS-301)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
Initializing Arrays
Arrays may be initialized when they are declared, just as any other
variables.
Place the initialization data in curly {} braces following the equals sign.
int i_Array[6] = { 1, 2, 3, 4, 5, 6 };
An array may be partially initialized, by providing fewer data items than
the size of the array. The remaining array elements will be automatically
initialized to zero.
float f_Array[ 10 ] = { 1.0, 5.0, 20.0 };
If an array is to be completely initialized, the dimension of the array is not
required. The compiler will automatically size the array to fit the
initialized data.
int data[ ] = { 34,34,78,23,45,89};
DATA STRUCTURE (KCS-301)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
Using Arrays
Elements of an array are accessed by specifying the index (offset )
of the desired element within square [ ] brackets after the array
name. Array subscripts must be of integer type.
Array name always reflects the base address of an array.
Array indices start at zero in C, and go to one less than the size of
the array. For example, a five element array will have indices zero
through four.
The index in C is actually an offset from the beginning of the array.
(The first element is at the beginning of the array, and hence has
zero offset.)
DATA STRUCTURE (KCS-301)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
CALCULATING THE ADDRESS OF 1D ARRAY ELEMENTS
Remember that the array name is a symbolic reference to the address of
the first byte of the array.
The subscript or the index represents the offset from the beginning of the
array to the element being referenced. That is, with just the array name and
the index, C can calculate the address of any element in the array.
The address of other data elements can simply be calculated using the base
address of the array.
The formula to perform this calculation is, Address of data element,
Address of Element A[i] = Base Add. of Array A + w (i – lower_bound)
Here, A is the array, i is the index of the element of which we have to
calculate the address and w is the size of one element in memory, for
example, size of int is 2.
DATA STRUCTURE (KCS-301)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
CALCULATING THE ADDRESS OF 1D ARRAY ELEMENTS
DATA STRUCTURE (KCS-301)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
OPERATIONS ON ARRAYS
There are a number of operations that can be preformed on arrays. These
operations include:
Traversing an array
Inserting an element in an array
Searching an element in an array
Deleting an element from an array
Merging two arrays
Sorting an array in ascending or descending order
DATA STRUCTURE (KCS-301)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
TWO-DIMENSIONAL ARRAYS
One-dimensional arrays are organized linearly in only one direction. But at times, we need
to store data in the form of grids or tables.
A two-dimensional array is specified using two subscripts where the first subscript denotes
the row and the second denotes the column.
The C compiler treats a two-dimensional array as an array of one-dimensional arrays.
Consider the following 2D array declaration:
int marks[3][5];
The pictorial form of a two-dimensional array is
DATA STRUCTURE (KCS-301)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
2D Arrays
Initialization of 2D Array in C
In the 1D array, we don't need to specify the size of the array if the declaration
and initialization are being done simultaneously.
However, this will not work with 2D arrays. We will have to define at least the
second dimension of the array.
The two-dimensional array can be declared and defined in the following way.
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
Or
int arr[ ][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
But not int arr[4][ ]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
Or int arr[ ][ ]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
DATA STRUCTURE (KCS-301)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
2D Array Example
DATA STRUCTURE (KCS-301)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
2D Array Example
DATA STRUCTURE (KCS-301)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
3D Array Example
DATA STRUCTURE (KCS-301)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
Passing Array to Functions
In a C program, you can pass
an entire array to functions.
an entire array to functions as a pointer.
individual elements of an array to functions.
address of individual array elements to functions.
DATA STRUCTURE (KCS-301)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
Example : Passing arrays to functions
DATA STRUCTURE (KCS-301)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
Example : Passing array to a function as a pointer
DATA STRUCTURE (KCS-301)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
Example : Passing individual array elements
DATA STRUCTURE (KCS-301)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
Example : Passing the address of individual array elements
DATA STRUCTURE (KCS-301)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
STORING A TWO-DIMENSIONAL ARRAY
Although we have shown a rectangular picture of a two-
dimensional array, in the memory, these elements actually will
be stored sequentially.
There are two ways of storing a two dimensional array in the
memory.
Row major order
Column major order
Row Major Order
The elements of the array are stored row by row where n
elements of the first row will occupy the first n locations.
For example, Elements of a 3 × 4 2D array in row major order
DATA STRUCTURE (KCS-301)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
STORING A TWO-DIMENSIONAL ARRAY
Column Major Order
When we store the elements in a column major order, the elements of the first column are
stored before the elements of the second and third column.
That is, the elements of the array are stored column by column where m elements of the
first column will occupy the first m locations.
For example, Elements of a 4 × 3 2D array in column major order
DATA STRUCTURE (KCS-301)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
ADDRESS CALCULATION IN 2D ARRAY
N-Dimensional Arrays: The N-Dimensional array is basically
an array of arrays. As 1-D arrays are identified as a single index, 2-
D arrays are identified using two indices, similarly, N-
Dimensional arrays are identified using N indices. A multi-
dimensional array is declared as follows:
int NDA[S1][S2][S3]……..[SN];
The lower bounds are assumed to be zeroes for all the dimensions.
It is possible that the array indices do not have the lower bound as
zero. For example, consider the following array T:
T[-5…5][2……9][14…54][-9…-2]
DATA STRUCTURE (KCS-301)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
ADDRESS CALCULATION IN 2D ARRAY
T[-5…5][2……9][14…54][-9…-2]
In the above array T, the lower bounds of indices are not zeroes.
So that the sizes of the indices of the array are different now and can
be calculated by using the formula:
UpperBound – LowerBound +1
So, here the S1 = 5 – (-5) + 1 = 11. Similarly, S2 = 8, S3 = 41 and
S4 = 8.
DATA STRUCTURE (KCS-301)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
ADDRESS CALCULATION IN 2D ARRAY
The Column Major formula:
Address of NDA[I1][I2]. . . [IN] = BA + W*[((…ENSN-1+ EN-
1)SN-2 +… E3 )S2+ E2 )S1 +E1]
Where
BA represents the base address of the array.
W represents the width of the array i.e, the number of dimensions in
the array.
Also, Ei is given by Ei = li – ti, where li and ti are the calculated indexes
(indices of array element which needs to be determined) and lower
bounds respectively.
DATA STRUCTURE (KCS-301)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
ADDRESS CALCULATION IN 2D ARRAY
The Row Major formula:
Address of NDA[I1][I2]. . . .[lN] = BA + W*[((E1S2 +
E2 )S3 +E3 )S4 ….. + EN-1 )SN + EN]
Where
BA represents the base address of the array.
W represents the width of the array i.e, the number of dimensions in
the array.
Also, Ei is given by Ei = li – ti, where li and ti are the calculated indexes
(indices of array element which needs to be determined) and lower
bounds respectively.
DATA STRUCTURE (KCS-301)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
ADDRESS CALCULATION IN 2D ARRAY
The simplest way to learn the formulas:
For row-major: If width = 5, the interior sequence is E1S2 + E2S3 + E3S4 +
E4S5 + E5 and if width = 3, the interior sequence is E1S2 + E2S3 + E3. Figure out
the pattern in the order and follow four basic steps for the formula of any width:
Write the interior sequence.
Put the closing bracket after each E except the first term. So for width = 5, it
becomes
E1S2 + E2)S3 + E3)S4 + E4)S5 + E5).
Put all the Opening brackets initially.
((((E1S2 + E2)S3 + E3)S4 + E4)S5 + E5).
Incorporate the base address and width in the formula.
DATA STRUCTURE (KCS-301)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
ADDRESS CALCULATION IN 2D ARRAY
The simplest way to learn the formulas:
For coloum-major:
The approach is similar for the Column Major but the pattern of the
interior sequence is reverse of the row-major pattern.
For column-major: If width =5, the interior sequence is
E5S4 + E4S3 + E3S2+ E2S1 + E1.
DATA STRUCTURE (KCS-301)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
ADDRESS CALCULATION IN 2D ARRAY
Example: Let’s take a multi-dimensional array A[10][20][30][40] with
the base address 1200. The task is to find the address of
element A[1][3][5][6].
Here, BA = 1200 and width = 4. S1 = 10, S2 = 20, S3 = 30, S4 = 40
Since the lower bounds are not given, so lower bounds are assumed to be
zero.
E1 = 1 – 0 = 1;
E2 = 3 – 0 = 3;
E3 = 5 – 0 = 5;
E4 = 6 – 0 = 6.
By applying the formula for row-major, directly write the formula as:
DATA STRUCTURE (KCS-301)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
ADDRESS CALCULATION IN 2D ARRAY
Here, BA = 1200 and width = 4. S1 = 10, S2 = 20, S3 = 30, S4 = 40
E1 = 1, E2 = 3, E3 = 5, E4 = 6
By applying the formula for row-major, directly write the formula as:
Address of A[1][3][5][6] = BA + W * (((E1S2 + E2 ) S3 + E3 ) S4 + E4 )
A[1][3][5][6] = 1200 + 4(((1 × 20 + 3)30 +5)40 + 6)
=1200 +4((23 × 30 +5)40 +6)
=1200 + 4(695 × 40 + 6)
=1200 + (4 × 27806)
=112424
DATA STRUCTURE (KCS-301)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
ADDRESS CALCULATION IN 2D ARRAY
Here, BA = 1200 and width = 4. S1 = 10, S2 = 20, S3 = 30, S4 = 40
E1 = 1, E2 = 3, E3 = 5, E4 = 6
By applying the formula for column-major, directly write the formula as:
Address of A[1][3][5][6] = BA + W * (((E4S3 + E3 ) S2+ E2 ) S1 + E1)
A[1][3][5][6] = 1200 + 4(((6 × 30 + 5)20 +3)10 + 1)
=1200 +4((185 × 20 +3)10 +1)
=1200 + 4(3703 × 10 + 1)
=1200 + (37031)
=38231
DATA STRUCTURE (KCS-301)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
ADDRESS CALCULATION IN 2D ARRAY
Example: Consider a 20 × 5 two-dimensional array marks which has its
base address = 1000 and the size of an element = 2. Now compute the
address of the element, marks[18][4] assuming that the elements are stored
in row major order.
Here, BA = 1000 and width = 2.
S1 = 20, S2 = 5
Since the lower bounds are not given, so lower bounds are assumed to be
zero.
E1 = 18 – 0 = 18;
E2 = 4 – 0 = 4;
By applying the formula for row-major, directly write the formula as:
DATA STRUCTURE (KCS-301)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
ADDRESS CALCULATION IN 2D ARRAY
Here, BA = 1000 and width = 2.
S1 = 20, S2 = 5, E1 = 18 – 0 = 18, E2 = 4 – 0 = 4;
By applying the formula for row-major, directly write the formula as:
Address of marks[18][4] = BA + W * (E1S2 + E2 )
marks[18][4] = 1000 + 2(18 × 5 + 4)
=1188
By applying the formula for coloum-major, directly write the formula as:
Address of marks[18][4] = BA + W * (E2S1 + E1 )
marks[18][4] = 1000 + 2(4 × 20 + 18)
=1196
DATA STRUCTURE (KCS-301)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
ADDRESS CALCULATION IN 3D ARRAY
Solution: From the question it implies that the valid locations of given array is
A[2:91][2:31][2:41]
Here, BA = 1000 and width = 2.
S1 = 90, S2 = 30, S3 = 40
E1 = 10-2 = 8, E2 = 20-2 = 18, E3 = 30-2 = 28
Address of A [10][20][30] in row-major implementation
= BA + W * ((E1S2+E2)S3+E3) = 1000+2((8×30+18)40+28) = 21696
Address of A [10][20][30] in coloum-major implementation
= BA + W * ((E3S2+E2)S1+E1) = 1000+2((28×30+18)90+8) = 155456
DATA STRUCTURE (KCS-301)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
SPARSE MATRICES
Sparse matrix is a matrix that has large number
of elements with a zero value.
In order to efficiently utilize the memory,
specialized algorithms and data structures that
take advantage of the sparse structure should be
used.
If we apply the operations using standard matrix
structures and algorithms to sparse matrices,
then the execution will slow down and the
matrix will consume large amount of memory. Example of Sparse Matrix
Sparse data can be easily compressed, which in
turn can significantly reduce memory usage.
DATA STRUCTURE (KCS-301)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
Types of Sparse Matrices
There are two types of sparse matrices.
In the first type of sparse matrix, all elements
above the main diagonal have a zero value. This
type of sparse matrix is also called a (lower)
triagonal matrix.
To store a lower-triangular matrix efficiently in
the memory, we can use a one-dimensional
array which stores only non-zero elements.
(a) Row-wise mapping—Here the contents of array A[] will be {1, 5, 3, 2, 7, –
1, 3, 1, 4, 2, –9, 2, –8, 1, 7}
(b) Column-wise mapping—Here the contents of array A[] will be {1, 5, 2, 3,
–9, 3, 7, 1, 2, –1, 4, –8, 2, 1, 7}
DATA STRUCTURE (KCS-301)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
Types of Sparse Matrices
In an upper-triangular matrix, Ai,j=0 where
i>j. An n×n upper-triangular matrix A has n
non-zero elements in the first row, n–1 non-
zero elements in the second row and likewise
one non-zero element in the nth row.
There is another variant of a sparse matrix, in
which elements with a non-zero value can
appear only on the diagonal or immediately
above or below the diagonal. This type of
matrix is also called a tri-diagonal matrix.
DATA STRUCTURE (KCS-301)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)