-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path001.c
143 lines (136 loc) · 3.1 KB
/
001.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//Author:Kavya Dhar
// Creation Date : 25-05-21
// Purpose ; //
// 1) Inserting an Element in an Array
// 2) Deleting an Element in an Array
// 3) Searching an Element in an Array
#include<stdio.h> // Pre-Processive to include standard input and output funtion header files
void insert(); // Funtion Prototype
void del(); // Funtion Prototype
void search (); // Funtion Prototype
int main() // Main funtion
{
int i;
char ch; // Variable Declratation
do
{
printf(" // Our Heartiest welcome goes to you // ");
printf("\nMain menu");
printf(" \n1. Insert");
printf("\n2. Delete");
printf("\n3. Search");
printf("\n enter your choice >> ");
scanf("%d",&ch);
switch(ch)
{
case 1: insert();
break;
case 2: del();
break;
case 3: search();
break;
default:
printf("Wrong choice, Please try again later");
}
printf ("\n Do you want to continue: y/n ");
scanf (" %c", &ch);
} while(ch == 'y');
}
// CASe for Inserting an element at specified position
void insert()
{
int a[20],n,l,x,i;
printf("Enter Size of array >> ");
scanf("%d",&n);
printf("Enter elements >> ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\nElements Before Inserting elements respective array is: ");
for(i=0;i<n;i++)
{
printf("%d ", a[i]);
}
printf("\n enter location where elements is to be inserted >> ");
scanf("%d",&l);
printf("\n enter element to be inserted >> ");
scanf("%d",&x);
n=n+1;
for(i=n-1;i>=l;i--)
{
a[i]=a[i-1];
}
a[i]=x;
printf("\nArray After insertion is >> ");
for(i=0;i<n;i++)
{
printf("\t%d",a[i]);
}
}
// CAse for deleting element from respective array
void del()
{
int a[20],n,l,i;
printf("Enter Size of Array >> ");
scanf("%d",&n);
printf("enter elements >> ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\nElements Before Delelting elements respective array is: ");
for(i=0;i<n;i++)
{
printf("%d ", a[i]);
}
printf("\n Enter location where element is to be deleted >> ");
scanf("%d",&l);
for(i=l-1;i<n-1;i++)
{
a[i]=a[i+1];
}
n=n-1;
printf("\n Array after deletion>> ");
for(i=0;i<n;i++)
{
printf("\t %d",a[i]);
}
}
// CASE For Searching An Element
void search()
{
int a[20],n,x,i;
printf("\n Enter Size of array >> ");
scanf("%d",&n);
printf("\n enter elements >> ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\nElements Before Searching elements respective array is: ");
for(i=0;i<n;i++)
{
printf("%d ", a[i]);
}
printf(" \n Enter elements to be searched >> ");
scanf("%d",&x);
for(i=0;i<n;i++)
{
/*
* If element is found in array then raise found
* and terminate from loop.
*/
if(a[i]==x)
{
printf("\n Element is at %d Index",i);
printf("\n Element is at %d Position",i+1);
break;
}
}
if(i==n)
/*
* If element is not found in array
*/
printf("\n element is not present");
}