0% found this document useful (0 votes)
4 views46 pages

Array

The document provides a comprehensive overview of arrays as a linear data structure that stores items of the same data type in contiguous memory locations. It discusses the advantages and disadvantages of arrays, their applications, and basic operations such as insertion, deletion, searching, and updating elements. Additionally, it covers two-dimensional arrays, their initialization, memory mapping techniques, and how to calculate the address of elements in both row-major and column-major order.

Uploaded by

28899user
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)
4 views46 pages

Array

The document provides a comprehensive overview of arrays as a linear data structure that stores items of the same data type in contiguous memory locations. It discusses the advantages and disadvantages of arrays, their applications, and basic operations such as insertion, deletion, searching, and updating elements. Additionally, it covers two-dimensional arrays, their initialization, memory mapping techniques, and how to calculate the address of elements in both row-major and column-major order.

Uploaded by

28899user
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/ 46

ARRAY

ARRAY
The array is a linear data structure where values or items are placed in a
linear order, which means the memory assigned to each item is contiguous.
The data type of an array is the same for all the elements present in it.
With the contiguous memory allocation, it becomes easier to find out the
memory location of any element in the array by just knowing the first
memory location and adding the offset.
Example: Arrays are the simplest data structures that store items of
the same data type. A basic application of Arrays can be storing data
in tabular format. For example, if we wish to store the contacts on
our phone, then the software will simply place
all our contacts in an array.
1. A simple question Paper is an array of
numbered questions with each of them
assigned some marks.
2. Tray of eggs.
3. Arranging books: You have a pile of books
and a rack with multiple layers. Once all the
books are arranged, you essentially created an array of elements.
NEED
•Sorting and searching a value in an array is easier.
•Arrays are best to process multiple values quickly and easily.
•Arrays are good for storing multiple values in a single variable - In
computer programming, most cases require storing a large number
of data of a similar type. To store such an amount of data, we need
to define a large number of variables. It would be very difficult to
remember the names of all the variables while writing the
programs. Instead of naming all the variables with a different name,
it is better to define an array and store all the elements into it.
Advantages of Arrays
•Arrays represent multiple data items of the same type using a single name.
•In arrays, the elements can be accessed randomly by using the index
number.
•Arrays allocate memory in contiguous memory locations for all its elements.
Hence there is no chance of extra memory being allocated in the case of
arrays. This avoids memory overflow or shortage of memory in arrays.
•Using arrays, other data structures like linked lists, stacks, queues, trees,
graphs etc can be implemented.
•Two-dimensional arrays are used to represent matrices.
Disadvantages of Arrays
•The number of elements to be stored in an array should be known in
advance.
•An array is a static structure (which means the array is of fixed size). Once
declared the size of the array cannot be modified. The memory which is
allocated to it cannot be increased or decreased.
•Insertion and deletion are quite difficult in an array as the elements are
stored in consecutive memory locations and the shifting operation is costly.
•Allocating more memory than the requirement leads to a wastage of
memory space and less allocation of memory also leads to a problem.
Applications of Arrays
1.Array stores data elements of the same data type.
2.Maintains multiple variable names using a single name. Arrays help to
maintain large data under a single variable name. This avoids the confusion of
using multiple variables.
3.Arrays can be used for sorting data elements. Different sorting techniques like
the Bubble sort, Insertion sort, Selection sort, etc use arrays to store and sort
elements easily.
4.Arrays can be used for performing matrix operations. Many databases, small
and large, consist of one-dimensional and two-dimensional arrays whose
elements are records.
5.Arrays can be used for CPU scheduling.
6.Lastly, arrays are also used to implement other data structures like Stacks,
Queues, Heaps, Hash tables, etc.
All the data elements of an array are stored at contiguous locations in the main
memory. The name of the array represents the base address or the address of
the first element in the main memory. Each element of the array is represented
by proper indexing.
We can define the indexing of an array in the below ways -
1.0 (zero-based indexing): The first element of the array will be arr[0].
2.1 (one-based indexing): The first element of the array will be arr[1].
3.n (n - based indexing): The first element of the array can reside at any random
index number.
In the above image, we have shown the memory allocation of an array arr of size
5. The array follows a 0-based indexing approach. The base address of the array
is 100 bytes. It is the address of arr[0]. Here, the size of the data type used is 4
bytes; therefore, each element will take 4 bytes in the memory.
Accessing An Element From The Array

We required the information given below to access any random element from
the array -
•Base Address of the array.
•Size of an element in bytes.
•Type of indexing, array follows.

Byte address of element A[i] = base address + size * ( i - first index)

Here, size represents the memory taken by the primitive data types. As an
instance, int takes 2 bytes, float takes 4 bytes of memory space in C
programming.
We can understand it with the help of an example –

Suppose an array, A[-10 ..... +2 ] having Base address (BA) = 999 and size of
an element = 2 bytes, find the location of A[-1].

L(A[-1]) = 999 + 2 x [(-1) - (-10)]

= 999 + 18

= 1017
Basic operations
Now, let's discuss the basic operations supported in the array -
•Traversal - This operation is used to print the elements of the array.
•Insertion - It is used to add an element at a particular index.
•Deletion - It is used to delete an element from a particular index.
•Search - It is used to search an element using the given index or by the value.
•Update - It updates an element at a particular index.
Insertion operation
This operation is performed to insert one or more elements into the array. As per the
requirements, an element can be added at the beginning, end, or at any index of the array.
Now, let's see the implementation of inserting an element into the array.
Deletion operation
As the name implies, this operation removes an element from the array and
then reorganizes all of the array elements.
1.#include <stdio.h>
2.#include <stdlib.h>
17.if (index >= n+1)
3. 18.{
4.int main(void) 19.printf (" \n Deletion is not
5.{ possible in the array.");
6.int i, n, index, arr[10]; 20.}
7.printf("Enter the size of the array: "); 21.else
8.scanf("%d", &n); 22.{
9.printf("Enter the elements 23.for (i = index; i < n - 1; i++)
of the array: \n");
24.arr[i] = arr[i + 1];
10.for (i = 0; i < n; i++)
11.{
25.printf("The array after deleting
12.printf("arr[%d] = ", i); the element is: ");
13.scanf("%d", &arr[i]); 26.for (i = 0; i < n - 1; i++)
14.} 27.printf("%d ", arr[i]);
15.printf("Enter the index of the element 28.return 0;
to be deleted: "); 29.}
16.scanf("%d", &index); 30.}
Search operation
This operation is performed to search an element in the array based on the
value or index.
Update operation
This operation is performed to update an existing array element located at the
given index.
2D ARRAY
The syntax of declaring two dimensional array is very much similar
to that of a one dimensional array, given as follows.
int arr[max_rows][max_columns];
Above image shows the two dimensional
array, the elements are organized in
the form of rows and columns. First
element of the first row is represented
by a[0][0] where the number shown in
the first index is the number of that
row while the number shown in the
second index is the number of the column.
How do we access data in a 2D array
Due to the fact that the elements of 2D arrays can be random
accessed. Similar to one dimensional arrays, we can access the
individual cells in a 2D array by using the indices of the cells. There
are two indices attached to a particular cell, one is its row number
while the other is its column number.
However, we can store the value stored in any particular cell of a 2D
array to some variable x by using the following syntax.
int x = a[i][j];
where i and j is the row and column number of the cell respectively.
We can assign each cell of a 2D array to 0 by using the following
code:
for ( int i=0; i<n ;i++)
{
for (int j=0; j<n; j++)
{
a[i][j] = 0;
}
}
Initializing 2D Arrays
We know that, when we declare and initialize one dimensional
array in C programming simultaneously, we don't need to specify
the size of the array. However this will not work with 2D arrays. We
will have to define at least the second dimension of the array.
The syntax to declare and initialize the 2D array is given as follows.
int arr[2][2] = {0,1,2,3};
The number of elements that can be present in a 2D array will
always be equal to (number of rows * number of columns).
#include <stdio.h>
void main ()
{ int arr[3][3],i,j;
for (i=0;i<3;i++)
{ for (j=0;j<3;j++)
{ printf("Enter a[%d][%d]: ",i,j);
scanf("%d",&arr[i][j]); }
} printf("\n printing the elements ....\n");
for(i=0;i<3;i++)
{ printf("\n");
for (j=0;j<3;j++)
{ printf("%d\t",arr[i][j]); } } }
Mapping 2D array to 1D array
When it comes to map a 2 dimensional array, most of us might think that
why this mapping is required. However, 2 D arrays exists from the user point
of view. 2D arrays are created to implement a relational database table
lookalike data structure, in computer memory, the storage technique for 2D
array is similar to that of an one dimensional array.
The size of a two dimensional array is equal to the multiplication of number
of rows and the number of columns present in the array. We do need to map
two dimensional array to the one dimensional array in order to store them in
the memory.
A 3 X 3 two dimensional array is shown in the following image. However,
this array needs to be mapped to a one dimensional array in order to store it
into the memory.
There are two main
techniques of storing 2D
array elements into memory
1. Row Major ordering
In row major ordering, all the
rows of the 2D array are stored
into the memory contiguously.
Considering the array shown
in the above image, its memory
allocation according to row major order is shown as follows.
first, the 1st row of the array is stored into the memory completely, then the
2nd row of the array is stored into the memory completely and so on till the
last row.
2. Column Major ordering
According to the column major ordering, all the columns of the 2D array are
stored into the memory contiguously. The memory allocation of the array
which is shown in in the above image is given as follows.

first, the 1st column of the array is stored into the memory completely, then
the 2nd row of the array is stored into the
memory completely and so
on till the last column of the array.
Calculating the Address of the random element of a 2D array
Due to the fact that, there are two different techniques of storing the two
dimensional array into the memory, there are two different formulas to
calculate the address of a random element of the 2D array.
By Row Major Order
If array is declared by a[m][n] where m is the number of rows while n is the
number of columns, then address of an element a[i][j] of the array stored in
row major order is calculated as,
Address of A[I][J] = B + W * ((I – LR) * N + (J – LC))
I = Row Subset of an element whose address to be found,
J = Column Subset of an element whose address to be found,
B = Base address,
W = Storage size of one element store in an array(in byte),
LR = Lower Limit of row/start row index of the matrix(If not given assume it as
zero),
LC = Lower Limit of column/start column index of the matrix(If not given assume it
as zero),
N = Number of column given in the matrix.
Given an array, arr[1………10][1………15] with base
value 100 and the size of each element is 1 Byte in memory.
Find the address of arr[8][6] with the help of row-major order.
By Column major order
If array is declared by a[m][n] where m is the number of rows while n is the
number of columns, then address of an element a[i][j] of the array stored in
row major order is calculated as,
Address of A[I][J] = B + W * ((J – LC) * M + (I – LR))
I = Row Subset of an element whose address to be found,
J = Column Subset of an element whose address to be found,
B = Base address,
W = Storage size of one element store in any array(in byte),
LR = Lower Limit of row/start row index of matrix(If not given assume it
as zero),
LC = Lower Limit of column/start column index of matrix(If not given
assume it as zero),
M = Number of rows given in the matrix.
Example: Given an array arr[1………10][1………15] with a base value
of 100 and the size of each element is 1 Byte in memory find the
address of arr[8][6] with the help of column-major order.
Transpose of a matrix
The transpose of a matrix is the one whose rows are columns of the original
matrix, i.e. if A and B are two matrices such that the rows of the matrix B are
the columns of the matrix A then Matrix B is said to be the transpose of
Matrix A.
THANK YOU

You might also like