-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path1_LinearArray.cpp
More file actions
95 lines (88 loc) · 1.64 KB
/
1_LinearArray.cpp
File metadata and controls
95 lines (88 loc) · 1.64 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
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
#include<iostream>
using namespace std;
int a[20];
int n,val,i,j,pos,temp;
void insert()
{
cout<<("\nEnter the position for the new element:\t");
cin>>pos;
cout<<("\nEnter the element to be inserted :\t");
cin>>val;
for(i=n-1;i>=pos;i--)
{
a[i+1]=a[i];
}
a[pos]=val;
n=n+1;
}
void del()
{
cout<<("\nEnter the position of the element to be deleted:\t");
cin>>pos;
val=a[pos];
for(i=pos;i<n-1;i++)
{
a[i]=a[i+1];
}
n=n-1;
cout<<("\nThe deleted element is =%d",val);
}
void loc()
{
int e,c=0,d=0;
cin>>e;
for(int i=0;i<n;i++)
{
if(a[i]==e)
{
c++;
cout<<i<<endl;
break;
}
}
if(c==0)
cout<<"Element not present"<<endl;
}
void display()
{
int i;
cout<<("\nThe array elements are:\n");
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
}
int main()
{
cout<<("\nEnter the size of the array elements:\t");
cin>>n;
cout<<("\nEnter the elements for the array:\n");
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"Enter 'a' for insertion, 'b' for deletion, 'c' to locate, 'd' to display & 'x' to exit the menu"<<endl;
char ch;
cin>>ch;
while(ch!='x')
{
switch(ch)
{
case 'a':
insert();
break;
case 'b':
del();
break;
case 'c':
loc();
break;
case 'd':
display();
break;
}
cout<<endl<<"Enter your choice"<<endl;
cin>>ch;
}
return 0;
}