forked from Pratiyush27/Arrays_Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6_remove_element.c
More file actions
53 lines (53 loc) · 1.15 KB
/
Copy path6_remove_element.c
File metadata and controls
53 lines (53 loc) · 1.15 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
//To Remove the given Element from the array
#include <stdio.h>
#include<stdlib.h>
// Function to print Array Elements
void print_array(int *A, int n) {
int i;
printf("Enter the Elements of Array\n");
for (i=0; i<n; i++) {
scanf("%d", &*(A+i));
}
for (i=0; i<n; i++) {
printf("%d\t", *(A+i));
}
}
// To remove the Element from the array
int remove_element(int *A, int n, int k) {
int i;
printf("After Removing the given Element\n");
if(k==0) // if we have to remove first one
{
for (i=1; i<n; i++) {
printf("%d\t", *(A+i));
}
}
else if(k==(n-1)) // if we have to remove last one
{
for (i=0; i<n-1; i++) {
printf("%d\t", *(A+i));
}
}
else if(k>0 && k<(n-1)) // if we have to remove any middle one
{
for (i=0; i<k; i++) {
printf("%d\t", *(A+i));
}
for (i=k+1; i<n; i++) {
printf("%d\t", *(A+i));
}
}
printf("\n");
}
int main()
{
int i,n,k;
printf("Enter the number of Elements to be Entered\n");
scanf("%d", &n);
int *A = (int*)malloc(n*sizeof(int));
print_array(A,n);
printf("\nEnter the index of element\n");
scanf("%d", &k);
remove_element(A,n,k);
return 0;
}