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

Arrays

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Arrays

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Page |1

Arrays
Array is a container which can hold a fix number of items and these items should be of the
same type. Most of the data structures make use of arrays to implement their algorithms.
Following are the important terms to understand the concept of Array.
 Element − each item stored in an array is called an element.
 Index − each location of an element in an array has a numerical index, which is used
to identify the element.
A linear array, is a list of finite numbers of elements stored in the memory. In a linear array,
we can store only homogeneous data elements. Elements of the array form a sequence or
linear list that can have the same type of data. Each element of the array, is referred by an
index set.
Arrays can be declared in various ways in different languages. For illustration, let's take C
array declaration.

Arrays can be declared in various ways in different languages. For illustration, let's take C
array declaration.

As per the above illustration, following are the important points to be considered.
 Index starts with 0.
 Array length is 10 which means it can store 10 elements.
 Each element can be accessed via its index. For example, we can fetch an element at
index 6 as 9.
Basic Operations
Following are the basic operations supported by an array.
 Traverse − print all the array elements one by one.
 Insertion − Adds an element at the given index.
 Deletion − Deletes an element at the given index.
 Search − Searches an element using the given index or by the value.
 Update − Updates an element at the given index.
Page |2

Traverse Operation:
This operation is to traverse through the elements of an array.
Algorithm: consider a linear array LA with lower bound LB to upper bound UB
Step 1: repeat for k=LB to UB
Apply PROCESS to LA [K]
[End of Loop]
Step 2: Exit

Example:
#include<stdio.h>
#include<conio.h>
int main()
{
int A[10],K=0,UB;
printf(“Enter the Array size less than 10 elements: “);
scanf(“%d”,&UB);
printf(“Enter the elements in array: \n”);
for(K=0;K<UB;K++)
{
scanf(“%d”,&A[K]);
}
printf(“The Traverse of array is:\n”);
for(K=0;K<UB;K++)
{
printf(“%d\n”,A[K]);
}
getch();
return 0;
}
Output:
Enter the Array size less than 10 elements: 5
Enter the elements in array: 1 2 3 4 5
The Traverse of array is:
12345

Insertion Operation:
Insert operation is to insert one or more data elements into an array. Based on the
requirement, a new element can be added at the beginning, end, or any given index of array.

Algorithm:
Step 1: [Initialize counter] Set J = N
Step 2: Repeat steps 3 and 4 while J>=K
Page |3

Step 3: [Move Jth element downward] Set LA [J+1] = LA[J]


Step 4: [Decrease counter] Set J=J-1
[End of step 2 loop]
Step 5: [Insert element] set LA [K] = ITEM
Step 6: [Reset N] Set N=N+1
Step 7: Exit.

Example:
#include <stdio.h>
int main()
{
int array[50], position, c, n, value;
printf("Enter number of elements in the array\n");
scanf("%d", &n);//6
printf("Enter %d elements\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);//1 5 6 7 8 9 index- 0 1 2 3 4 5
printf("Please enter the location where you want to insert an new element\n");
scanf("%d", &position);//3
printf("Please enter the value\n");
scanf("%d", &value);//85
for (c = n - 1; c >= position - 1; c--) // c=6-1;c>=3-1//c=5;5>=2 true
array[c+1] = array[c]; array[5+1]=6//0 1 2 3 4 5 6
//array[5+1]= array[c]è array[6] = array[c]
array[position-1] = value;//3-1=2 è 85
printf("Resultant array is\n");
for (c = 0; c <= n; c++)
printf("%d\n", array[c]);// 1 5 85 6 7 8 9
return 0;
}
Output:
Enter number of elements in array:
5
Enter 5 elements:
12345
Please enter the location where you want to insert a new element:
4
Please enter the value:
10
Resultant Array is:
1 2 3 10 4 5

Deletion Operation:
Deletion refers to removing an existing element from the array and re-organizing all elements
of an array.

Algorithm:
Page |4

Consider LA is a linear array with N elements and K is a positive integer such that K<=N.
Following is the algorithm to delete an element available at the Kth position of LA.
1. Start
2. Set J = K
3. Repeat steps 4 and 5 while J < N
4. Set LA[J] = LA[J + 1]
5. Set J = J+1
6. Set N = N-1
7. Stop

Example:

#include<stdio.h>
main()
{
int LA[] = {1,3,5,7,8};
int k = 3, n = 5;
int i, j;
printf("The original array elements are :\n");
for(i = 0; i<n; i++) {
printf("LA[%d] = %d \n", i, LA[i]);//1 3 5 7 8
}
j = k;j=3
while( j < n) 3<5{
LA[j-1] = LA[j];// 3-1=2//index value
j = j + 1;//actual
}
n = n -1;//5-1=4;
printf("The array elements after deletion :\n");
for(i = 0; i<n; i++) {
printf("LA[%d] = %d \n", i, LA[i]);//1 5 7 8
}
}

Or

#include <stdio.h>
int main()
{
int array[100], position, c, n;
printf("Enter number of elements in array\n");
scanf("%d", &n);//6
printf("Enter %d elements\n", n);
for ( c = 0 ; c < n ; c++ )
scanf("%d", &array[c]);//
printf("Enter the location where you wish to delete element\n");
scanf("%d", &position);
Page |5

if (position >= n+1 )


printf("Deletion not possible.\n");
else
{
for (c = position - 1 ; c < n - 1 ; c++ )
array[c] = array[c+1];
printf(“The position of the element is %d: ”);
printf("Resultant array is\n");
for( c = 0 ; c < n - 1 ; c++ )
printf("%d\n", array[c]);
}
return 0;
}

Output:
Enter number of elements in array:
5
Enter 5 elements:
5 10 15 20 25
Enter the location where you wish to delete element:
2
Resultant array is:
5 15 20 25

Two-Dimensional Array (2D Array):


2D array can be defined as an array of arrays. The 2D array is organized as matrices which can
be represented as the collection of rows and columns.
However, 2D arrays are created to implement a relational database look alike data structure.
It provides ease of holding bulk of data at once which can be passed to any number of
functions wherever required.
Syntax: int arr[max_rows][max_columns];
Ex: int a[][]=new int [2][5];

2D Array Memory Representation:


The array is stored in the forms:
 Column by column – column Major Order
 Row by row – Row-Major order
Column Major Order and Row Major Order:

Example:
A =| A1 A2 A3|
|A4 A5 A6|
Page |6

Could be stored in two possible ways:


Row-major Column-major
Address
order order
0 A1 A1
1 A2 A2
2 A3 A3
3 A4 A4
4 A5 A5
5 A6 A6

C: row-major order (lexicographical


access order), zero-based indexing
Address Access Value
0 A[0][0] A1
1 A[0][1] A2
2 A[0][2] A3
3 A[1][0] A4
4 A[1][1] A5
5 A[1][2] A6

On the other hand, in arrays are stored in column-major order, while the array indexes are
still written row-first (colexicographic access order):
Fortran: column-major order
(colexicographic access order), one-
based indexing
Address Access Value
1 A(1,1) A1
2 A(2,1) A2
3 A(1,2) A3
4 A(2,2) A4
5 A(1,3) A5
6 A(2,3) A6
Advantages & Disadvantages of Arrays:
Advantages:
 It is better and convenient way of storing the data of same datatype with same size.
Page |7

 It allows us to store known number of elements in it.


 It allocates memory in contiguous memory locations for its elements. It does not
allocate any extra space/ memory for its elements. Hence there is no memory
overflow or shortage of memory in arrays.
 Iterating the arrays using their index is faster compared to any other methods like
linked list etc.
 It allows to store the elements in any dimensional array – supports multidimensional
array.

Disadvantages:
 Array size is fixed
 Array is homogeneous
 Array is Contiguous blocks of memory.
 Insertion and deletion are not easy in Array

Multidimensional Arrays :
Multidimensional arrays in simple words as array of arrays. Data in multidimensional arrays
are stored in tabular form (in row major order).
General form of declaring N-dimensional arrays:
data_type array_name[size1][size2]....[ sizeN];
data_type: Type of data to be stored in the array.
Here data_type is valid C/C++ data type
array_name: Name of the array
size1, size2,... ,sizeN: Sizes of the dimensions

Examples:
Two dimensional array:
int two_d[10][20];
Three dimensional array:
int three_d[10][20][30];

Size of multidimensional arrays


Total number of elements that can be stored in a multidimensional array can be calculated by
multiplying the size of all the dimensions.
For example:
The array int x[10][20] can store total (10*20) = 200 elements.
Similarly array int x[5][10][20] can store total (5*10*20) = 1000 elements.

Matrices:
Matrix is a two dimensional array. A rectangular array of m × n numbers (real or complex) in
the form of m horizontal lines (called rows) and n vertical lines (called columns), is called a
Page |8

matrix of order m by n, written as m × n matrix. Such an array is enclosed by [ ] or ( ). In this


article, we will learn the meaning of matrices, types of matrices, important formulas, etc.
|A10 A12….. A1n|
A= |A20 A22….. A2n|
|A30 A32….. A2n|

Sparse Matrices:
Sparse matrices are matrices with a relatively high proportion of zero entries.
The natural method of representing matrices as 2d array is not suitable for sparse matrices. It
is possible to save by storing only non-zero entries.
|0 0 1 0 0|
|0 2 0 0 0|
|0 0 0 0 0|
|0 0 0 3 0|
|4 0 0 0 0|

You might also like