-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquestion1.cpp
132 lines (126 loc) · 2.92 KB
/
question1.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
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
#include <iostream>
using namespace std;
const int MAX_SIZE = 100;
class ArrayOperations {
private:
int arr[MAX_SIZE];
int size;
public:
ArrayOperations(): size(0) {}
void updateElement(int k, int x){
if (k >=0 && k < size) {
arr[k] = x;
}
else {
cout<<"Invalid index"<<endl;
}
}
void insertElement (int k, int x){
if (k >=0 && k <= size && size < MAX_SIZE) {
for (int i = size; i>k ; --i){
arr[i] = arr[i-1];
}
arr[k]= x;
++size;
}
else {
cout <<"Invalid index or Array is full"<<endl;
}
}
bool searchElement (int x) {
for (int i = 0; i< size; i++){
if (arr[i]==x){
return true;
}
}
return false;
}
void removeElement (int x) {
int i =0;
while (i< size && arr[i]!=x){
++i;
}
if (i< size){
for (int j =1; j<size; j++){
arr[j]= arr[j+1];
}
--size;
}
else {
cout <<"Element not found "<< endl;
}
}
void displayElement(int k) {
if (k>=0 && k<size){
cout <<"Element at position"<<k<<":"<<arr[k]<<endl;
}
else {
cout <<"Invalid index"<<endl;
}
}
};
int main (){
ArrayOperations arrayops;
int choice;
do {
cout <<"Menu:\n";
cout<<"1. Update an element\n";
cout<<"2. Search an Element\n";
cout<<"3. Insert an Element \n";
cout<<"4. Remove an Element \n";
cout<<"5. Display an Element \n";
cout<<"0. Exit\n";
cout<<"Enter your choice";
cin>>choice;
switch(choice){
case 1: {
int k,x;
cout <<"Enter position and value to update";
cin >> k >> x;
arrayops.updateElement(k,x);
break;
}
case 2: {
int x;
cout<<"Enter the value to search";
cin>>x;
if (arrayops.searchElement(x)){
cout<<"Element found";
}
else {
cout<<"Element not found";
}
break;
}
case 3: {
int x,k;
cout <<"Enter the position and value to insert";
cin>>k>>x;
arrayops.insertElement(k,x);
break;
}
case 4:{
int x;
cout <<"Enter the value to remove";
cin>>x;
arrayops.removeElement(x);
break;
}
case 5:{
int k;
cout <<"Enter the position to display";
cin >> k;
arrayops.displayElement(k);
break;
}
case 0:{
cout<<"Exiting the program";
break;
}
default:
cout<<"Invalid choice";
}
}
while (choice != 0);
return 0;
}