Array
Array
Definition of an Array
• An Array is a collection of homogeneous (similar) types of data
element in continuous memory.
• An Array is a linear data structure because all elements of the array
are stored in linear order.
Array declaration
• All variables must be declared before they are used in the program.
Therefore, the same concept also holds with array variables.
• An array must be declared before it is used .During the declaration of
an array ,the size of the array has to be specified.
• Declaring an array involves the following specifications.
• Data Type- The data type means the different kind of values it can
store. The data type can be an integer, float, char,or any other valid
data type.
• Array name-The name refers to the name of the array which will be
used to identify the array.
• Size -The size of an array refers to the maximum number of values an
Array can hold.
• Syntax-data_typearray_name[size]
• Example –int salary[10];
Memory representation of an array
Calculating the address of Array elements
• Array stores all its data element in continuous memory location,
storing the base address(Address of the first element of the array),
• Hence, the address of the other data elements can easily be
calculated using the base address. The formula to find the address of
elements in 1D area is as follows:
• Address of data element,A[i] = Base Address(BA)+w (i-lower bound)
• i is the index of the element for which the address is to be calculated,
BA is the base address of the array A , and w is the size of each
element(ex the size of int is 2 bytes, the size of char is 1 byte etc)
Calculating the address of Array element
• Array stores all its data elements in continuous memory location,
storing the base address( address of the first element of the Array),
hence the address of the other data elements can easily be calculated
using the base address. The formula to find the address of elements
in a 1-D array is as follows:
• Address of data element, A[i]= Base address(BA) + w(i- lower bound)
• i Is the index of the element for which the address is to be calculated,
BA is the base address of the Array A and w is the size of each
element( for example the size of int is 2 bytes, the size Of char is 1
byte etc.)
• Q. An Array is giving int marks[6]={ 34,53, 87, 100, 98,65}; calculate
the address of marks[3] if the base address is 3000.
• Sol. It is given that the base address of the array is 3000 and we know
that the size of integer is 2 bytes. Hence we can easily find the
address of marks[3].
Operations on Arrays
• Traversing an array
• inserting an element in an array
• deleting and element in an array
• Searching an element in an array
• merging of two arrays
• Sorting an array
Traversing an Array
• Traversing an array means to access every element in an array exactly
once so that it can be processed.
Inserting an element in an array
• Insertion in Array can be done in three ways:
• insertion at the beginning
• insertion at the specified position
• insertion at the end
Insertion at the beginning
Algorithm for insertion in the beginning
• The maximum elements that can be stored in the array is defined by
size. we should first check if the array has an empty space available to
store any element in it or not and then we proceed with the insertion
process.
Continue
Algorithm for insertion at a specified position
Insertion at the end
Deleting an element in an array
• Deletion in an array can be done in three ways :
• deletion from the beginning
• Deletion from the specific position
• deletion from the end
Deletion from the beginning
Algorithm for Deletion from the beginning
2-D Arrays/Two-dimensional Arrays
• 2-D arrays are organized in the form of grids or tables. They are
collection of 1-D arrays. One dimensional Arrays are linearly organized
in the memory.A 2-D array consists of two subscripts:
• First subscript-Which denotes the row
• Second subscript- which denotes the column
Declaration of two dimensional Arrays
• For declaring two dimensional Array we must know the name of the
Array, the data type of each element and the size of each dimensional
(size of rows and columns).
• Syntax- data_type Array_name [row_size][ column_size];
• Two dimensional Array is also called an m X n array, as it contains mXn
elements where each element in the array can be accessed by i and j,
where i<=m and j<=n and where I,j,m,n are defined as follows:
• I,j = Subscrips of Array elements,
• m= number of rows
• n= number of columns
Example
• Let us take an array of 3×3 elements. Therefore, the array is declared
as: int marks [3][3];
Row major order
• In row major order the elements of the first row are stored before the
elements of the second, third and n rows. Here the data elements are
stored in row by row basis:
Column Major order
• In column major order the elements of the first column are stored
before the elements of the second, third and n columns. Here the
data elements are stored in a column by column basis.
• Elements in row major order
• Elements in column major order
• Q. Consider a 25×5 dimensional Array of students which has a base
address 500 and the size of each element is 2. Now calculate the
address of the element student[15][3] assuming that the elements
are stored in
• row major order
• column major order
• Ans.
• Column major order
Multidimensional Arrays/N-Dimensional
Arrays
• A multidimensional Array is also known as an n-dimensional Array. It
is an array of Arrays.An n-dimensional array is an m1 X m2 X m3…..
array as it contains m1 X m2 X….. Elements. Multi dimensional arrays
are declared and initialized in the same way as one dimensional and
two dimensional arrays.
Calculating the address of 3D Arrays
• Just like 2D arrays we can you store 3-D arrays in two ways, row major order and
column major order.
• 1. Element in row major order
• Address([i][j][k]) =Base Address (BA) +w(L3(L2(E1)+E2)+E3)
• Elements in column major order
• Address ([i][j][k])=Base Address (BA) +w((E3L2+E2)L1 +E1)
• Where L is length of index L=Upper bound – Lower bound +1,
• E is effective address ,E=i-Lower bound
• E1=i-Lower Bound
• E2=j-Lower bound
• E3=k-Lower Bound
• Q. Latest date a 3D Array A(4:12, -2:1,8:14) and calculate the address
of A(5,4,9) using row major order and column major order where the
base address is 500 and w=4.
• Ans.
• Row major order
Operations on to the Arrays
• Sum
• Difference
• Product
• Transpose
Write a program to read and display of 3×3
matrix.
• Q Write a program to find the sum of two matrices.
• Write a program to find the transpose of a 3×3 metrics.