-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ1.cpp
More file actions
149 lines (120 loc) · 3.51 KB
/
Copy pathQ1.cpp
File metadata and controls
149 lines (120 loc) · 3.51 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
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
144
145
146
147
148
149
//Develop a Menu driven program to demostrate the following operations of Arrays
//1.CREATE
//2.DISPLAY
//3.INSERT
//4.DELETE
//5.LINEAR SEARCH
//6.EXIT
#include <iostream>
using namespace std;
class ArrayOperations {
int arr[100]; // fixed size array
int n; // current size
public:
ArrayOperations() { n = 0; }
// 1. CREATE
void create() {
cout << "\nEnter number of elements: ";
cin >> n;
if (n > 100) {
cout << "Maximum allowed size is 100. Try again.\n";
n = 0;
return;
}
cout << "Enter " << n << " elements: ";
for (int i = 0; i < n; i++)
cin >> arr[i];
cout << "Array created successfully.\n";
}
// 2. DISPLAY
void display() {
if (n == 0) {
cout << "\nArray is empty!\n";
return;
}
cout << "\nCurrent Array: ";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
}
// 3. INSERT
void insert() {
if (n == 100) {
cout << "\nArray is full! Cannot insert.\n";
return;
}
int pos, value;
cout << "\nEnter position to insert (1 to " << n+1 << "): ";
cin >> pos;
if (pos < 1 || pos > n+1) {
cout << "Invalid position!\n";
return;
}
cout << "Enter value to insert: ";
cin >> value;
for (int i = n; i >= pos; i--)
arr[i] = arr[i - 1];
arr[pos - 1] = value;
n++;
cout << "Element inserted successfully.\n";
}
// 4. DELETE
void remove() {
if (n == 0) {
cout << "\nArray is empty! Nothing to delete.\n";
return;
}
int pos;
cout << "\nEnter position to delete (1 to " << n << "): ";
cin >> pos;
if (pos < 1 || pos > n) {
cout << "Invalid position!\n";
return;
}
cout << "Deleted element: " << arr[pos - 1] << endl;
for (int i = pos - 1; i < n - 1; i++)
arr[i] = arr[i + 1];
n--;
}
// 5. LINEAR SEARCH
void linearSearch() {
if (n == 0) {
cout << "\nArray is empty! Cannot search.\n";
return;
}
int key;
cout << "\nEnter element to search: ";
cin >> key;
for (int i = 0; i < n; i++) {
if (arr[i] == key) {
cout << "Element found at position " << i + 1 << ".\n";
return;
}
}
cout << "Element not found in array.\n";
}
};
int main() {
ArrayOperations obj;
int choice;
while (true) {
cout << "\n====== ARRAY OPERATIONS MENU ======\n";
cout << "1. CREATE\n";
cout << "2. DISPLAY\n";
cout << "3. INSERT\n";
cout << "4. DELETE\n";
cout << "5. LINEAR SEARCH\n";
cout << "6. EXIT\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1: obj.create(); break;
case 2: obj.display(); break;
case 3: obj.insert(); break;
case 4: obj.remove(); break;
case 5: obj.linearSearch(); break;
case 6: cout << "Exiting program. Goodbye!\n"; return 0;
default: cout << "Invalid choice! Please try again.\n";
}
}
}