array_deletion.c
arrays Implementation
46 lines887 bytesC
1// Array Operations - Deletion
2#include <stdio.h>
3#define MAX 100
4
5void deleteAtPosition(int arr[], int *n, int pos) {
6 if (*n <= 0) {
7 printf("Array is empty!\n");
8 return;
9 }
10 if (pos < 0 || pos >= *n) {
11 printf("Invalid position!\n");
12 return;
13 }
14
15 for (int i = pos; i < *n - 1; i++) {
16 arr[i] = arr[i + 1];
17 }
18 (*n)--;
19}
20
21void display(int arr[], int n) {
22 printf("Array: ");
23 for (int i = 0; i < n; i++) {
24 printf("%d ", arr[i]);
25 }
26 printf("\n");
27}
28
29int main() {
30 int arr[MAX] = {10, 20, 30, 40, 50};
31 int n = 5;
32
33 printf("Original ");
34 display(arr, n);
35
36 deleteAtPosition(arr, &n, 2);
37 printf("After deleting at position 2: ");
38 display(arr, n);
39
40 deleteAtPosition(arr, &n, 0);
41 printf("After deleting at position 0: ");
42 display(arr, n);
43
44 return 0;
45}
46