0% found this document useful (0 votes)
14 views13 pages

C Programming: Array Insertion & Deletion

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

C Programming: Array Insertion & Deletion

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

CSE101 –

Problem Solving &


Programming in C
Unit II

03-10-2025 Ref: Programming in C, Reema Thareja.


Arrays

03-10-2025 Ref: Programming in C, Reema Thareja. 2


Inserting elements in an array
int arr[10] = {3,4,5,6,7};
3 4 5 6 7 0 0 0 0 0
arr[0] arr[1] arr[2] arr[3] arr[4] arr[5] arr[6] arr[7] arr[8] arr[9]

Insert location 2 Insert value 9

3 4 9 5 6 7 0 0 0 0 After insertion

arr[0] arr[1] arr[2] arr[3] arr[4] arr[5] arr[6] arr[7] arr[8] arr[9]
03-10-2025 Ref: Programming in C, Reema Thareja. 3
Inserting Process
• Need to shift the elements by one location so that arr[2] can be
assigned with new value.

arr[0] arr[1] arr[2] arr[3] arr[4] arr[5] arr[6] arr[7] arr[8] arr[9]

3 4 5 6 7 0 0 0 0 0

3 4 5 6 7 7 0 0 0 0
03-10-2025 Ref: Programming in C, Reema Thareja. 4
Inserting Process
• Need to shift the elements by one location so that arr[2] can be
assigned with new value.

arr[0] arr[1] arr[2] arr[3] arr[4] arr[5] arr[6] arr[7] arr[8] arr[9]

3 4 5 6 7 7 0 0 0 0

3 4 5 6 6 7 0 0 0 0
03-10-2025 Ref: Programming in C, Reema Thareja. 5
Inserting Process
• Need to shift the elements by one location so that arr[2] can be
assigned with new value.

arr[0] arr[1] arr[2] arr[3] arr[4] arr[5] arr[6] arr[7] arr[8] arr[9]

3 4 5 6 6 7 0 0 0 0

3 4 5 5 6 7 0 0 0 0
03-10-2025 Ref: Programming in C, Reema Thareja. 6
Inserting Process
• Now assign arr[2] as 9

arr[0] arr[1] arr[2] arr[3] arr[4] arr[5] arr[6] arr[7] arr[8] arr[9]

3 4 9 5 6 7 0 0 0 0

03-10-2025 Ref: Programming in C, Reema Thareja. 7


#include <stdio.h>
if (n == 100||position>=n)
printf("insert not possible.\n");
int main()
else
{
{
int array[100], position, c, n, val;
for (c = n-1; c >=position ; c--)
printf("Enter number of elements in array\n");
array[c+1] = array[c];
scanf("%d", &n);
array[position] = val;
printf("Enter %d elements\n", n);
n++;
for (c = 0; c < n; c++)
printf("Resultant array:\n");
scanf("%d", &array[c]);
for (c = 0; c < n; c++)
printf("Enter the location \n");
printf("%d\n", array[c]);
scanf("%d", &position);
}
printf("\n Enter the element to insert : ");
return 0;
scanf("%d",&val);
}

03-10-2025 Ref: Programming in C, Reema Thareja. 8


Deleting elements in an array
int arr[10] = {3,4,5,6,7}; Delete from location 2

3 4 5 6 7 0 0 0 0 0

arr[0] arr[1] arr[2] arr[3] arr[4] arr[5] arr[6] arr[7] arr[8] arr[9]

3 4 6 7 7 0 0 0 0 0 After Deletion

arr[0] arr[1] arr[2] arr[3] arr[4] arr[5] arr[6] arr[7] arr[8] arr[9]

03-10-2025 Ref: Programming in C, Reema Thareja. 9


Deleting Process
• Need to shift the elements by one location so that arr[4] can be
ignored. i.e., no. of elements is reduced by one

arr[0] arr[1] arr[2] arr[3] arr[4] arr[5] arr[6] arr[7] arr[8] arr[9]

3 4 5 6 7 0 0 0 0 0

3 4 6 6 7 0 0 0 0 0

3 4 6 7 7 0 0 0 0 0

03-10-2025 Ref: Programming in C, Reema Thareja. 10


#include <stdio.h>
if (position <0|| position>=n)
printf(”deletion not possible.\n");
int main()
else
{
{
int array[100], position, c, n, val;
for (c = position; c < n - 1; c++)
printf("Enter number of elements in array\n");
array[c] = array[c+1];
scanf("%d", &n);
printf("Resultant array:\n");
printf("Enter %d elements\n", n);
n = n-1;
for (c = 0; c < n; c++)
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("%d\n", array[c]);
printf("Enter the location \n");
}
scanf("%d", &position);
return 0;
}

03-10-2025 Ref: Programming in C, Reema Thareja. 11


Removing Duplicates
• Assume that array is sorted

3 4 5 6 6 6 9 12 13 15
• After removing duplicate elements

3 4 5 6 9 12 13 15

03-10-2025 Ref: Programming in C, Reema Thareja. 12


References
• Reema Thareja, “Programming with C”, Oxford University Press, Third
Edition, 2023.
• Dromey R.G, “How to Solve it by Computers”, Pearson Education,
2012.

03-10-2025 Ref: Programming in C, Reema Thareja. 13

You might also like