A3 Array Data Structure
A3 Array Data Structure
A3 Array Data Structure
.......
0 1 2 3 ........ 99
As shown above, 0…99 are memory locations for this array and they
are contiguous. The blank panels are the actual array elements. The
individual elements of an array can be accessed using the index. In the
above diagram, the first index of the array is 0 while the last index is
99 (since this is an array of 100 elements).0 1 2 3 4 5 ……. ….. 99.
Note that the starting index of an array is always 0. Thus for an array
of n elements, the starting index of the array will be 0 and the last
index will be n-1
Declare An Array
Array declaration in C++ generally looks as shown below:
NUM
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
Array Representation
Arrays can be declared in various ways in different
languages. For illustration, let's take C array declaration.
Example
Following program traverses and prints the elements of an array:
#include <stdio.h>
main()
{
int LA[] = {1,3,5,7,8};
int item = 10, k = 3, n = 5;
int i = 0, j = n;
printf("The original array elements are :\n");
for(i = 0; i<n; i++)
printf("LA[%d] = %d \n", i, LA[i]);
}
When we compile and execute the above program, it produces the
following result
Output
#include <stdio.h>
main()
{
int LA[] = {1,3,5,7,8};
int item = 10, k = 3, n = 5;
int i = 0, j = n;
printf("The original array elements are :\n");
for(i = 0; i<n; i++)
printf("LA[%d] = %d \n", i, LA[i]);
n = n + 1;
while( j >= k)
{
LA[j+1] = LA[j];
j = j - 1;
}
LA[k] = item;
printf("The array elements after insertion :\n");
for(i = 0; i<n; i++)
printf("LA[%d] = %d \n", i, LA[i]);
}
When we compile and execute the above program,
it produces the following result −
Output
The original array elements are:
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 7
LA[4] = 8
The array elements after insertion :
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 10
LA[4] = 7
LA[5] = 8
Deletion Operation
Deletion refers to removing an existing element from the array and
re-organizing all elements of an array.
Algorithm
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
Following is the implementation of the above algorithm −
#include <stdio.h>
void 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]);
j = k;
while( j < n)
{
LA[j-1] = LA[j];
j = j + 1;
}
n = n -1;
printf("The array elements after deletion :\n");
for(i = 0; i<n; i++)
printf("LA[%d] = %d \n", i, LA[i]);
}
When we compile and execute the above program, it produces the following result −
Output
Algorithm
Consider LA is a linear array with N elements and K is a positive
integer such that K<=N.
Following is the algorithm to find an element with a value of ITEM
using sequential search.
1. Start
2. Set J = 0
3. Repeat steps 4 and 5 while J < N
4. IF LA[J] is equal ITEM THEN GOTO STEP 6
5. Set J = J +1
6. PRINT J, ITEM
7. Stop
Example
Following is the implementation of the above algorithm −
#include <stdio.h>
void main()
{
int LA[] = {1,3,5,7,8};
int item = 5, n = 5;
int i = 0, j = 0;
while( j < n)
{
if( LA[j] == item )
break;
j = j + 1;
}
Algorithm
Consider LA is a linear array with N elements and K is a
positive integer such that K<=N.
Following is the algorithm to update an element available at
the Kth position of LA.
1. Start
2. Set LA[K-1] = ITEM
3. 3. Stop
Example: Following is the implementation of the above algorithm −
#include <stdio.h>
void main()
{
int LA[] = {1,3,5,7,8};
int k = 3, n = 5, item = 10;
int i, j;