0% found this document useful (0 votes)
3 views10 pages

Array Operations: Insertion & Deletion

The document contains C programs for reading, inserting, and deleting elements in an array. It includes algorithms for inserting an element at a specific position and deleting an element from the middle of an array. Additionally, it provides example outputs for each operation to illustrate the functionality of the programs.

Uploaded by

surekha.vatturu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views10 pages

Array Operations: Insertion & Deletion

The document contains C programs for reading, inserting, and deleting elements in an array. It includes algorithms for inserting an element at a specific position and deleting an element from the middle of an array. Additionally, it provides example outputs for each operation to illustrate the functionality of the programs.

Uploaded by

surekha.vatturu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1. Write a program to read and display n numbers using an array.

#include <stdio.h>

#include <conio.h>

int main()

int i=0,n,arr [20];

clrscr();

printf("\n Enter the number of elements: ");

scanf("%d", &n);

printf("\n Enter the elements");

for (i=0;i<n;i++)

printf("\n Arr [%d] = ", i);

scanf("%d", &arr [i]);

printf("\n The array elements are \n");

for (i=0;i<n;i++)

printf("Arr [%d] = %d\t", i, arr[i]);

return 0;

}
Output

Enter the number of elements: 5

Enter the elements

Arr [0] = 1

Arr [1] = 2

Arr [2] = 3

Arr [3] = 4

Arr [4] = 5

The array elements are

Arr [0] = 1 Arr [1] = 2 Arr [2] = 3

Arr [3] = 4 Arr [4] = 5

Algorithm to insert an element in the middle of an array

The algorithm INSERT will be declared as INSERT (A, N, POS, VAL). The
arguments are

(a) A, the array in which the element has to be inserted (b) N, the number of
elements in the array

(c) POS, the position at which the element has to be inserted and

(d) VAL, the value that has to be inserted.

In the algorithm given in Figure 5.11, in Step 1, we first initialize I with the total
number of elements in the array. In Step 2, a while loop is executed which will
move all the elements that have index greater than POS one position towards right
to create space for the new element. In Step 5, we increment the total number of
elements in the array by 1 and finally in Step 6, the new value is inserted at the
desired position.

Now, let us visualize this algorithm by taking an 1001 example. Initial Data [] is
given as shown in Figure 5.12. Calling INSERT (Data, 6, 3, 100) will lead to the
following processing in the array:

11. Write a program to insert a number at a given location in an


array.

#include <stdio.h>
#include <conio.h>

int main()

int i, n, num, pos, arr [10];

clrscr();

printf("\n Enter the number of elements in the array: ");

scanf("%d", &n);

printf("\n Enter the values");

for (i=0;i<n;i++)

scanf("%d", &arr[i]);

printf("\n Enter the number to be inserted: ");

scanf("%d", &num);

to printf("\n Enter the position at which the number has to be added: ");

scanf("%d", &pos);

for (i=n-1;i>=pos; i--)

arr[i+1] = arr[i];

arr [pos] = num;

n++;
printf("\n The array after insertion of %d is: ", num);

for (i=0;i<n;i++)

printf("\t %d", arr[i]);

getch();

return 0;

Output

Enter the number of elements in the array: 5

Enter the values: 1 2 3 4 5

Enter the number to be inserted: 7

Enter the position at which the number has to be added: 3

The array after insertion of 7 is:

123745

Deleting an Element from an Array

Deleting an element from an array means removing a data element from an already
existing array. If the element has to be deleted from the end of the existing array,
then the task of deletion is quite simple. We just have to subtract 1 from
the upper_bound. Figure 5.13 shows an algorithm to delete an element from the
end of the array.
For example, if we have an array that is declared as

int marks [];

The array is declared to store marks of all the students in the class. Now suppose
there are 54 students and the student with roll number 54 leaves the course. The
marks of this student was therefore stored in marks [54]. We just have to
decrement the upper_bound. Subtracting 1 from the upper_bound will indicate that
there are 53 valid data in the array.

However, if we have to delete the element from the middle of the array, then this
task is not trivial. On an average, we might have to move as much as half of the
elements from their position in order to occupy the space of the deleted element.

For example, consider an array whose elements are arranged in ascending order.
Now, if an element has to be deleted from somewhere middle of the array. To do
this, we will first find the location from where the element has to be deleted and
then move all the elements (that have a value greater than that of the element) one
location towards the left so that location vacated by the deleted element is occupied
by rest of the elements.

Example 5.6

Data [] is an array that is declared as int Data [10]; and contains the following
values:

Data[] = {12, 23, 34, 45, 56, 67, 78, 89, 90, 100};

(a) If a data element with value 56 has to be deleted, find its position.

(b) Delete the data element and hence give the memory representation of the array.
Solution

(a) Since the elements of the array are stored in ascending als order, we will
compare the value that has to be deleted with the value of every element in the
array. As soon as VAL = Data [I], where I is the index or subscript of the array, we
will get the position from which the element has to be deleted. For example, if we
see this array, here VAL = 56. Data [0] = 12 which is not equal to 56. Like this, we
will compare and finally get the value of POS = 4.

Algorithm to delete an element from the middle of an array

The algorithm DELETE will be declared as DELETE (A, N, POS). The arguments
are as follows:

(a) A, the array from which the element has to be deleted (b) N, the number of
elements in the array

(c) POS, the position from which the element has to be deleted

Figure 5.14 shows the algorithm in which we first initialize I with the position
from which the element has to be deleted. In Step 2, a while loop is executed
which will move all the elements that have index greater than POS one location
towards left to occupy the location vacated by the deleted element. When we say
that we are deleting an element, we are actually overwriting the element with the
value of its successive element. In Step 5, we decrement the total number of
elements in the array by 1.
Now, let us visualize this algorithm by taking an example and having a look at
Figure 5.15. Initial Data [] is given as shown in Figure 5.15. Calling DELETE
(Data, 6, 2) will lead to the following processing in the array:

13. Write a program to delete a number from a given location in an


array.
#include <stdio.h>

#include <conio.h>

int main()

int i, n, pos, arr [10];

clrscr();

printf("\n Enter the size of the array: ") i

scanf("%d", &n);

printf("\n Enter the elements of the array: ");

for (i=0;i<n;i++)

scanf("%d", &arr[i]);

printf("\n Enter the position from which the number has to be deleted: ");

scanf("%d", &pos);

for (i= pos; i<n-1;i++)

arr[i] = arr [i+1];

n--;

printf("\n The array after deletion is: ");

for (i=0;i<n;i++)

printf("\n Arr [%d] = %d", i, arr[i]);

getch();
return 0;

Ouput

Enter the size of the array: 5

Enter the elements of the array:

12345

Enter the position from which the number has to be deleted: 3

The array after deletion is:

Arr [0] = 1

Arr [1] = 2

Arr [2] = 3

Arr [3] = 5

You might also like