-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEXPT 1-Array basic operations.cpp
More file actions
104 lines (104 loc) · 1.85 KB
/
EXPT 1-Array basic operations.cpp
File metadata and controls
104 lines (104 loc) · 1.85 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
94
95
96
97
98
99
100
101
102
103
104
#include<iostream>
#include<stdlib.h>
using namespace std;
class arr
{
int arr[100],i,n,pos,val,key;
public:
void create();
void ins();
void del();
void search();
void display();
};
void arr::create()
{
cout<<"Enter the size of array: ";
cin>>n;
cout<<"Enter the elements for the array: ";
for(i=0;i<n;i++)
{
cin>>arr[i];
}
}
void arr::ins()
{
cout<<"Enter the position of element to be inserted: ";
cin>>pos;
cout<<"Enter the element to be inserted: ";
cin>>val;
for(i=n-1;i>=pos;i--)
{
arr[i+1]=arr[i];
}
arr[pos]=val;
n=n+1;
}
void arr::del()
{
cout<<"Enter the position of the element to be deleted: ";
cin>>pos;
val=arr[pos];
for(i=pos;i<n-1;i++)
{
arr[i]=arr[i+1];
}
n=n-1;
cout<<"The deleted element is = "<<val<<endl;
}
void arr::search()
{
cout<<"Enter the element to be searched: ";
cin>>key;
for(i=0;i<n;i++)
{
if(arr[i]==key)
{
cout<<"The element is present at position "<<i<<endl;
break;
}
}
if(i==n)
{
cout<<"The element does not exist\n";
}
}
void arr::display()
{
cout<<"The array elements are: ";
for(i=0;i<n;i++)
{
cout<<arr[i]<<" ";
}
cout<<endl;
}
int main()
{
arr a;
a.create();
int choice;
do{
cout<<"\nEnter your choice:\n";
cout<<"1=Insert"<<endl;
cout<<"2=Delete"<<endl;
cout<<"3=Search"<<endl;
cout<<"4=Display"<<endl;
cout<<"5=Exit"<<endl;
cin>>choice;
switch(choice)
{
case 1: a.ins();
break;
case 2: a.del();
break;
case 3: a.search();
break;
case 4: a.display();
break;
case 5: exit(0);
break;
default: cout<<"Invalid choice";
break;
}
} while(choice!=5);
}