2.1 Introduction
An array is defined as a set of finite number of homogeneous elements or data items. Some common operations performed on arrays are:
1. Creation of an array
2. Traversing an array (accessing array elements)
3. Insertion of new elements.
4. Deletion of required element.
5. Modification of an element
6. Merging of arrays.
7. Searching an element.
8. Sorting the elements.
2.2 Limitation of arrays
1.The prior knowledge of number of elements in the array is necessary.
2. These are static structures.
3. Since the elements of these arrays are stored in these arrays are time consuming. This is because of moving down or up to create a new space of a new element or to occupy the space vacated by the deleted item.
Experiment 1
Aim – Write a program to insert, delete and traverse an array
Theory –
Inserting an element at the end of array can be easily done, provided the memory space allocated for array is large enough to accommodate the additional element. For inserting the element at required position , elements must be moved downwards or rightwards to create space for the element to be inserted.
Deleting an element at the end of array presents no difficulties, but deleting element somewhere in the middle of the array would require to shift all the elements to fill the space emptied by the deletion of the element, then element following it are moved upward or left by one location.
Traversing means to access all the elements of array, starting from first element (Lower bound) upto the last element (Upper bound ) in the array one-by-one.
Algorithm (Insertion)
Assume len to be the length of the array A, NE be the total no. of elements, pos denotes index no. or position, num to be the element to be inserted.
1. [Initialize the value of I ]
Set I = NE
2. Repeat for I=NE to pos
[Shift the elements down by one element]
Set a[I+1]=a[I]
[end of Loop]
3. [Insert the element at required position]
Set A[pos]=num
4. [Reset len]
Set NE=len+1
5. display yhe new list of arrays
6. End
Algorithm (Deletion)
Assume len to be the length of the array A, NE be the total no. of elements, pos denotes index no. or position, num to be the element to be deleted.
1. Set item=A[pos]
2. Repeat for j = pos to NE-1
[Shifting elements 1 position upwards or left]
Set A[j] = A[j+1]
[End of loop]
3. Reset NE = NE -1
4. Display the new list of arrays
5. End
Algorithm (Traversal)
Let LB be the lower bound and UB be the upper bound of linear array A.
1. [Initialize counter.]
Set I at lower bound LB
2. Repeat for I=LB to UP
[visit element]
Display A[I]
[End of the loop]
3. Exit.
2.2 Viva Questions
1. What is meant by static implementation of the array
2. What is meant by dynamic implementation of the array
3. Name some Data structures, which can be implemented with the help of arrays.
4. Does deletion operation in static implementation, actually deletes the array element or it overwrites it.
5. What are the limitations of the array.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment