This repository has been archived by the owner on Jul 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp01_1708.c
90 lines (81 loc) · 1.35 KB
/
p01_1708.c
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Emmanuel Jojy
// S3 CSE A
// Roll no: 53
// Prg 01/1708
// Insertion and Deleltion to Array
#include <stdio.h>
void insert(int arr[], int *n);
void delete(int arr[], int *n);
void main()
{
int n, i, arr[100];
printf("N: ");
scanf("%d", &n);
printf("Enter array elements:\n");
for(i = 0; i < n; i++)
{
printf("arr[%d]: ", i);
scanf("%d", &arr[i]);
}
while(1)
{
int ch;
printf("\n1. Insert\n2. Delete\n3. Quit\nEnter choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: insert(arr, &n); break;
case 2: delete(arr, &n); break;
}
if(ch == 3)
{
break;
}
}
}
void insert(int arr[], int *n)
{
int index, e;
printf("Enter index to insert: ");
scanf("%d", &index);
if(index > *n)
{
return ;
}
printf("Enter element to insert: ");
scanf("%d", &e);
for(int i = index, temp = e; i < *n + 1; i++)
{
temp = arr[i];
arr[i] = e;
e = temp;
}
*n = *n + 1;
printf("\nElements after insertion: ", *n);
for(int i = 0; i < *n; i++)
{
printf("\t%d", arr[i]);
}
printf("\n");
}
void delete(int arr[], int *n)
{
int index;
printf("Enter index of element to delete: ");
scanf("%d", &index);
if(index > *n - 1)
{
return ;
}
for(int i = index; i < *n - 1; i++)
{
arr[i] = arr[i + 1];
}
*n = *n - 1;
printf("\nElements after deletion: ");
for(int i = 0; i < *n; i++)
{
printf("\t%d", arr[i]);
}
printf("\n");
}