-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeletion.cpp
More file actions
50 lines (40 loc) · 1.25 KB
/
deletion.cpp
File metadata and controls
50 lines (40 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <stdio.h>
#include <stdlib.h>
int main() {
int i, n, index, arr[10];
// Get array size
printf("Enter the size of array: ");
scanf("%d", &n);
// Check if size is valid
if (n > 10 || n <= 0) {
printf("Invalid array size! Size should be between 1 and 10.\n");
return 0;
}
// Get array elements
printf("Enter the elements of the array:\n");
for(i = 0; i < n; i++) {
printf("arr[%d] = ", i);
scanf("%d", &arr[i]);
}
// Get index of element to delete
printf("Enter the index of the element to be deleted: ");
scanf("%d", &index);
// Check if the index is valid
if(index < 0 || index >= n) {
printf("\nDeletion is not possible. Invalid index.\n");
} else {
// Shift elements to the left from the index
for(i = index; i < n-1; i++) {
arr[i] = arr[i + 1];
}
// Decrease the size of the array
n--;
// Display the array after deletion
printf("The array after deleting the element is:\n");
for(i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
return 0;
}