forked from codevscolor/codevscolor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove_one_element_array.c
More file actions
60 lines (60 loc) · 1.49 KB
/
remove_one_element_array.c
File metadata and controls
60 lines (60 loc) · 1.49 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
51
52
53
54
55
56
57
58
59
60
#include <stdio.h>
#include <stdlib.h>
int main()
{
//1
int *array, size, elementToDelete, i, position;
//2
printf("Enter total number of elements to add : ");
scanf("%d", &size);
array = (int *)malloc(size * sizeof(int));
//3
for (i = 0; i < size; i++)
{
printf("Enter element for position %d : ", i);
scanf("%d", &array[i]);
}
//4
printf("You have entered : ");
for (i = 0; i < size; i++)
{
printf("%d ", array[i]);
}
printf("\n");
//5
printf("Enter the number you want to delete : ");
scanf("%d", &elementToDelete);
//6
position = -1;
//7
for (i = 0; i < size; i++)
{
if (array[i] == elementToDelete)
{
position = i;
break;
}
}
//8
if (position != -1)
{
//9
for (i = position; i < size - 1; i++)
{
array[i] = array[i + 1];
}
array = (int *)realloc(array, (size - 1) * sizeof(int));
//10
printf("Final array :");
for (i = 0; i < size - 1; i++)
{
printf("%d ", array[i]);
}
printf("\n");
}
else
{
//11
printf("Entered number is not found in the array.");
}
}