-
Notifications
You must be signed in to change notification settings - Fork 0
/
Exp1.cpp
95 lines (89 loc) · 1.58 KB
/
Exp1.cpp
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
#include<bits\stdc++.h>
using namespace std;
void insert(int arr[],int n,int pos,int value)
{
n++;
for(int i=n;i>=pos;i--)
{
arr[i]=arr[i-1];
}
arr[pos-1]=value;
for(int i=0;i<n;i++)
{
cout<<arr[i]<<" ";
}
}
void del(int arr[],int n,int pos)
{
--pos;
for(int i=pos;i<n;i++)
{
arr[i]=arr[i+1];
}
for(int i=0;i<n-1;i++)
{
cout<<arr[i]<<" ";
}
}
int linear (int arr[],int n,int val)
{
for(int i=0;i<n;i++)
{
if(arr[i]==val)
{
return i;
}
}
return -1;
}
void print(int arr[],int n)
{
for(int i=0;i<n;i++)
{
cout<<arr[i]<<" ";
}
}
int main()
{
int n, arr[n],value,pos;
cout<<"Enter size of array\n";
cin>>n;
cout<<"Enter array\n";
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
cout<<"\n";
int ch;
cout<<"1. choose to insert element\n";
cout<<"2. choose to delete element\n";
cout<<"3. choose to search element\n";
cout<<"4. choose to print array\n\n";
cout<<"Enter choice\n";
cin>>ch;
switch(ch)
{
case 1:
cout<<"Which position?\n";
cin>>pos;
cout<<" Enter Value \n";
cin>>value;
insert(arr,n,pos,value);
break;
case 2:
cout<<"Which position\n";
cin>>pos;
del(arr,n,pos);
break;
case 3:
cout<<"Enter value to be searched\n";
cin>>value;
cout<<"Element present at index "<< linear(arr,n,value)<<endl;
break;
case 4:
print(arr,n);
break;
default:cout<<"Action not performed\n";
}
return 0;
}