## Friday, February 18, 2022

### Array II Part (Traverse, Insert, Delete Operation)

Arrays: Operations

Traversal, Insertion, Deletion

Pankaj Kumar Gupta

Durga Prasad Baljeet Singh (PG) College, Anoopshahr

District Bulandshahr (Uttar Pradesh)

Abstract:

This Paper describes full discussion about  operations of Array Data Structures like: Traversal, Insertion, Deletion. Discussions about Algorithms and Procedures of same above defined operations also made in this paper.

Keywords:

Array, Homogenous, Contiguous, Linear, Primary, Lower Bound, Upper Bound, SDA, Insert, Delete, Traverse.

Meaning of array:

Computer Application we define Array as: it is a Linear Data Structure. It is also termed as a collection of homogenous data elements. All the elements of an array share common name and stored on contiguous locations. It is also categorized into derived data types.

Example:

1.      A collection of roll numbers of 50 students (int).

2.      A collection of marks of 50 students in 6 subjects for a class (float).

3.      A collection of names of 50 students (char).

Categories of Arrays:

Arrays are categorized into 3 categories that are defined as follows:

1.      Single Dimensional Arrays.

2.      Two Dimensional Arrays.

3.      Multidimensional Arrays.

Operations of Array Data Structures:

Various operations are performed on Array Data Structure, that are as follows:

Here Discussion is made on Single Dimensional Array.

1.      Traversal

2.      Insertion

3.      Deletion

4.      Searching

5.      Sorting

6.      Merging

But Traverse, Insert & Delete operations are discussed in this paper.

Traversal:

Traversal is a process of visiting each and every node of a list in systematic manner. That means to go through each and every item of the list at least once.

Algorithm TRAVERSEARRAY(A,N,LB,UB)

/* Here TRAVERSEARRAY is an algorithm which is used to traverse all the elements of Array A with N elements. LB is Lower bound & UB is the Upper bound of Array. */

Step-1 Start

Step-2 set K=LB.

Step-3 Repeat steps 4 and 5 while K<=UB

Step-4 PROCESS A[K].

Step-5 set K=K+1.

Step-6 Exit.

Insertion:

Insertion is a process of adding one or more items in the given list. If an array of elements is given and an item is said to add at specific location. If array is already full then insertion operation results “Overflow”. To insert the item into array, all the elements from that location are shifted to the right side of the Array. Specific location may in:

1.      Beginning of the Array

2.      Middle anywhere

3.      Last of the Array

Here Lower bound is considered to be as 0.

Array A is given as A={1,2,3,4}

Item to be inserted is 55

Location is:

At Beginning: New array will be like:

In Middle anywhere (Loc=2 as index): New array will be like:

At Last: New array will be like:

Algorithm INSERTARRAY(A,N,ITEM,LOC)

/* Here INSERTARRAY is an algorithm which is used to insert an ITEM on the Location LOC in an Array A with N elements. Lower bound of A is considered to be 0. */

Step-1 Start

Step-2 set J=N.

Step-3 Repeat steps 4 and 5 while K>=LOC

Step-4 set A[J]=A[J-1].

Step-5 set J=J-1.

Step-6 set A[LOC]:=ITEM.

Step-7 set N:=N+1.

Step-8 Exit.

Deletion:

Deletion is a process of removing one or more items from given list. If an array of elements is given and an item is said to remove from specific location. If array is already empty then deletion operation results “Underflow”. To delete the item from array, all the elements from that location are shifted from right to the left side of the Array in this way the element that is to be deleted is overwritten by next element right to the same location. Specific location may in:

1.      Beginning of the Array

2.      Middle anywhere

3.      Last of the Array

Here Lower bound is considered to be as 0.

Array A is given as A={1,2,3,4,5}

Item to be deleted is 1 on location 0 (i.e. Beginning)

New array will be like:

Item to be deleted is 3 on location 2 (i.e. Middle anywhere)

New array will be like:

d

Item to be deleted is 5 on location 4 (i.e. from Last)

New array will be like:

Now from all situations we decrease the size of array by 1 so that last element is removed from Array.

Algorithm DELETEARRAY(A,N,ITEM,LOC)

/*Here DELETEARRAY is an algorithm which is used to delete an ITEM  from the Location LOC in an Array A with N elements. Lower bound of A is considered to be 0. */

Step-1 Start

Step-2 set J:=LOC.

Step-3 set ITEM:=A[LOC].

Step-4 Repeat steps 4 and 5 while K<N

Step-5 set A[J]:=A[J+1].

Step-6 set J:=J+1.

Step-7 set N:=N-1.

Step-8 Exit.

Conclusion:

An array can also be termed as a collection of homogeneous values. Anybody can treat an array as a single object by referring to it through a variable. Variable name is succeeded by square brackets (that is                  `[ ]` symbols) But you can also treat the components of the array as if they are themselves variables. Traverse operation is made for going through all elements in an  array. Insert operation is made for adding one or more items in the array. Delete operation is made for removing one or more items in the array.

## Wednesday, February 16, 2022

### Array (Types of Arrays)

Arrays

Pankaj Kumar Gupta

Durga Prasad Baljeet Singh (PG) College, Anoopshahr

District Bulandshahr (Uttar Pradesh)

Abstract:

This Paper describes full discussion about arrays. All topics about array like: Definition of an array, Declaration of array, Types of arrays (SDA, DDA, MDA), Memory allocation, Subscripting, Initialization, Inputting, Outputting etc.

Keywords:

Array, Homogenous, Contiguous, Linear, Primary, Lower Bound, Upper Bound, SDA, DDA, MDA, Declaration, Allocation, Initialization, Subscripting, Indexing.

Meaning of array:

According to Cambridge Dictionary is:

A large group of things or people, especially one that is attractive or causes administration or has been position or has been positioned in a particular way.”

According to Oxford Dictionary is:

A group or collection of things or people, often one that is large or impressive.”

Here in Computer Application we define Array as: it is a Linear Data Structure. It is also termed as a collection of homogenous data elements. All the elements of an array share common name and stored on contiguous locations. It is also categorized into derived data types.

Example:

1.      A collection of roll numbers of 50 students (int).

2.      A collection of marks of 50 students in 6 subjects for a class (float).

3.      A collection of names of 50 students (char).

Categories of Arrays:

Arrays are categorized into 3 categories that are defined as follows:

1.      Single Dimensional Arrays.

2.      Two Dimensional Arrays.

3.      Multidimensional Arrays.

1.      Single Dimensional Array (SDA) :

It is also known as One Dimensional Array (1D). All the elements of this type of Array are stored in one dimension only.

For example: A collection of Roll Numbers of 50 students.

Declaration of SDA:

Variables of primary data types are declared to use the same. And Arrays are categorized into secondary data types, variables of array must also be declared like primary data types. So followings are the syntaxes of array data type.

Syntax:
data_type array_name[size];

For example:

To declare Roll Numbers of 5 Students (in integer form):

int rn;

To declare Marks of 4 Subjects (in float form):

float m;

To declare Name of 10 characters (in character form):

char n;

Memory Allocation (SDA):

All the elements of Array (SDA) are stored at contiguous locations in memory.

Indexing or Subscripting (SDA):

Since all the elements of array share common name, to recognize the elements of an array has become the problem. To overcome this problem all the elements uses the indexing or subscripting. Indexing of SDA always starts from 0 and goes up to     n-1. Where n is the size of array.

Initialization (SDA):

Boxes created in the memory (i.e. memory allocation) must be filled with some values. Same values may be filled with initialization or may be input form user. here initialization is shown in following diagram:

Inputting (SDA):

If values of SDA are required to input from user, we may input same from user by following method:

Algorithm INPUTARRAY(A,N,LB,UB)

/* Here INPUTARRAY is an algorithm which is used to input all the elements of Array A with the Capacity of N elements. LB is Lower bound & UB is the Upper bound of Array. */

Step-1 Start

Step-2 set K=LB.

Step-3 Repeat steps 4 and 5 while K<=UB

Step-4 Input value of A[K].

Step-5 set K=K+1.

Step-6 Exit.

Outputting:

If values of SDA stored in the memory are required to show to user, we may output same to user by following method:

Algorithm OUTPUTARR(A,N,LB,UB)

/* Here OUTPUTARR is an algorithm which is used to output all the elements of Array A with the Capacity of N elements. LB is Lower bound & UB is the Upper bound of Array. */

Step-1 Start

Step-2 set K=LB.

Step-3 Repeat steps 4 and 5 while K<=UB

Step-4 print the value of A[K].

Step-5 set K=K+1.

Step-6 Exit.

2.      Two Dimensional Array (2DA/DDA):

It is also known as Double Dimensional Array (DDA). All the elements of this type of Array are stored in two dimensions (i.e. rows & columns) only.

For example: A collection of marks of 5 students in 3-3 subjects (float) etc.

Declaration of DDA:

Syntax:
datatype arrayname[row][col];

For example:

To declare Marks of 5 Students in 3-3 subjects (in floating point form):

float m;

Memory Allocation (DDA):

All the elements of Array (DDA) are stored at contiguous locations in memory.

float m;

Indexing or Subscripting (DDA):

Since all the elements of array share common name, to recognize the elements of an array has become the problem. To overcome this problem all the elements uses the indexing or subscripting. Indexing always starts from  and goes up to     [r-1][c-1]. Where r is the no. of rows and c is the no. of columns.

Initialization (DDA):

Boxes created in the memory (i.e. memory allocation) must be filled with some values. Same values may be filled with initialization or may be input form user. Here initialization is shown in following diagram:

Inputting (DDA):

If values of DDA are required to input from user, we may input same from user by following method:

Algorithm INPUT2DARRAY(A,R,C,LR,UR,LC,UC)

/* Here INPUT2DARRAY is an algorithm which is used to input all the elements of Array A with the Capacity of R X C elements. LR is Lower bound of Row & UR is the Upper bound Row. LC is Lower bound of Column & UC is the Upper bound Column. R is the No. of Rows & C is the No. of Columns. */

Step-1 Start

Step-2 set K=LR.

Step-3 Repeat steps 4 to 8 while K<=UR

Step-4 set J=LC.

Step-5 Repeat steps 6 and 7 while J<=UC

Step-6 Input value of A[K][J].

Step-7 set J=J+1.

Step-8 set K=K+1.

Step-9 Exit.

Outputting (DDA):

If values of DDA stored in the memory are required to show to user, we may output same to user by following method:

Algorithm OUTPUT2DARRAY(A,R,C,LR,UR,LC, UC)

/* Here OUTPUT2DARRAY is an algorithm which is used to input all the elements of Array A with the Capacity of R X C elements. LR is Lower bound of Row & UR is the Upper bound Row. LC is Lower bound of Column & UC is the Upper bound Column. R is the No. of Rows & C is the No. of Columns. */

Step-1 Start

Step-2 set K=LR.

Step-3 Repeat steps 4 to 8 while K<=UR

Step-4 set J=LC.

Step-5 Repeat steps 6 and 7 while J<=UC

Step-6 Output the value of A[K][J].

Step-7 set J=J+1.

Step-8 set K=K+1.

Step-9 Exit.

3.      Multidimensional Array (MDA):

It may be 3-D, 4-D … and n-D Array. All the elements of this type of Array are stored in 3-Dimensions,  4-Dimensions, ……….. n-Dimensions. Here we will consider 3-D Array for an example. Consider A collection of marks of 5 students in 3-3 subjects for 2 Classes (float) etc.

Declaration of 3D Array:

Syntax:

datatype arrayname[room][row][col];

For example:

To declare Marks of 5 Students in 3-3 subjects for 2 Classes (in floating point form):
float m;

Memory Allocation (3DA):

All the elements of Array (3DA) are stored at contiguous locations in memory.

Indexing or Subscripting (3DA):

Since all the elements of array share common name, to recognize the elements of an array has become the problem. To overcome this problem all the elements uses the indexing or subscripting. Indexing always starts from  and goes up to [x-1][y-1][z-1]. Where x is the no. of rooms, y is the no. of rows and z is the no. of columns.
Initialization (3DA):

Boxes created in the memory (i.e. memory allocation) must be filled with some values. Same values may be filled with initialization or may be input form user. Here initialization is shown in following diagram:

Inputting (3DA):

If values of 3DA are required to input from user, we may input same from user by following method:

Algorithm INPUT3DARRAY(A,B,R,C,LB,UB,LR, UR,LC,UC)

/* Here INPUT3DARRAY is an algorithm which is used to input all the elements of Array A with the Capacity of B X R X C elements. LB is Lower bound of Room & UB  is the Upper bound Room. LR is Lower bound of Row & UR is the Upper bound Row. LC is Lower bound of Column & UC is the Upper bound Column. B is the No. of Rooms, R is the No. of Rows & C is the No. of Columns. */

Step-1 Start

Step-2 set I=LB.

Step-3 Repeat steps 4 to 11 while I<=UB

Step-4 set J=LR.

Step-5 Repeat steps 6 to 10 while J<=UR

Step-6 set K=LC.

Step-7 Repeat steps 8 and 9 while K<=UC

Step-8 Input value of A[I][J][K].

Step-9 set K=K+1.

Step-10 set J=J+1.

Step-11 set K=K+1.

Step-12 Exit.

Outputting (3DA):

If values of 3DA stored in the memory are required to show to user, we may output same to user by following method:

Algorithm OUTPUT3DARRAY(A,B,R,C,LB,UB, LR,UR,LC,UC)

/* Here OUTPUT3DARRAY is an algorithm which is used to output all the elements of Array A with the Capacity of B X R X C elements. LB is Lower bound of Room & UB  is the Upper bound Room. LR is Lower bound of Row & UR is the Upper bound Row. LC is Lower bound of Column & UC is the Upper bound Column. B is the No. of Rooms, R is the No. of Rows & C is the No. of Columns. */

Step-1 Start

Step-2 set I=LB.

Step-3 Repeat steps 4 to 11 while I<=UB

Step-4 set J=LR.

Step-5 Repeat steps 6 to 10 while J<=UR

Step-6 set K=LC.

Step-7 Repeat steps 8 and 9 while K<=UC

Step-8 Output the value of A[I][J][K].

Step-9 set K=K+1.

Step-10 set J=J+1.

Step-11 set K=K+1.

Step-12 Exit.

Conclusion:

An array can also be termed as a collection of homogeneous values. Anybody can treat an array as a single object by referring to it through a variable. Variable name is succeeded by square brackets (that is                  `[ ]` symbols) But you can also treat the components of the array as if they are themselves variables.

References:

### Array II Part (Traverse, Insert, Delete Operation)

Arrays: Operations Traversal, Insertion, Deletion Pankaj Kumar Gupta Head, BCA Department Durga Prasad Baljeet Singh (PG) Colleg...