C
C
1)insertion
2)deletion
3)travertion
### Algorithm
1. **Initialize** an array with a fixed size (30) and a variable to track the
number of elements.
2. **Display the menu** options for user interaction.
3. **Input Array**:
- Prompt the user for the number of elements and store them.
4. **Insert Element**:
- Request the position and value to insert.
- Shift elements and insert the new item.
5. **Delete Element**:
- Ask for the position to delete.
- Shift elements to fill the gap.
6. **Traverse**:
- Request lower and upper bounds.
- Print elements in the specified range.
7. **Exit** the program when the user selects the exit option.
```cpp
#include <iostream.h>
#include <conio.h>
class ArrayOperations {
private:
int arr[30];
int n;
public:
ArrayOperations() : n(0) {}
void inputArray() {
cout << "Enter total number of elements: ";
cin >> n;
for (int i = 0; i < n; i++) {
cout << "Enter element for arr[" << i << "]: ";
cin >> arr[i];
}
}
void printArray() {
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
}
void deletion(int k) {
if (k < 0 || k >= n) {
cout << "Invalid position!" << endl;
return;
}
for (int j = k; j < n - 1; j++)
arr[j] = arr[j + 1];
n--;
cout << "Array after deletion: ";
printArray();
}
void main() {
clrscr();
ArrayOperations ao;
int choice, item, k, lb, ub;
do {
cout << "\nMenu:\n";
cout << "1. Input Array\n";
cout << "2. Insert Element\n";
cout << "3. Delete Element\n";
cout << "4. Traverse\n";
cout << "5. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
ao.inputArray();
break;
case 2:
cout << "Enter position for insertion: ";
cin >> k;
cout << "Enter element for insertion: ";
cin >> item;
ao.insertion(k, item);
break;
case 3:
cout << "Enter position for deletion: ";
cin >> k;
ao.deletion(k);
break;
case 4:
cout << "Enter lower bound: ";
cin >> lb;
cout << "Enter upper bound: ";
cin >> ub;
ao.traversal(lb, ub);
break;
}
} while (choice != 5);
getch();
}
### Algorithm
```cpp
#include <iostream.h>
#include <conio.h>
class SearchOperations {
private:
int arr[30];
int n;
public:
SearchOperations() : n(0) {}
void inputArray() {
cout << "Enter total number of elements in the array: ";
cin >> n;
for (int i = 0; i < n; i++) {
cout << "Enter element for arr[" << i << "]: ";
cin >> arr[i];
}
}
void printArray() {
cout << "\nGIVEN ARRAY IS AS BELOW:";
for (int i = 0; i < n; i++)
cout << "\n arr[" << i << "]: " << arr[i];
}
void sortArray() {
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (arr[i] > arr[j]) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
void linearSearch() {
int item, pos = -1;
cout << "\nEnter item for search: ";
cin >> item;
for (int i = 0; i < n; i++) {
if (arr[i] == item) {
pos = i;
break; // Stop once the item is found
}
}
if (pos == -1)
cout << "\nGiven item is not found";
else
cout << "\nGiven item found at position " << pos;
}
void binarySearch() {
int item, beg, mid, end, i = 0;
cout << "\nEnter item for search: ";
cin >> item;
beg = 0;
end = n - 1;
if (arr[mid] == item) {
cout << "\n\nGiven item found at position " << mid;
return;
} else if (item > arr[mid]) {
beg = mid + 1;
} else {
end = mid - 1;
}
}
cout << "\n\nGiven item is not found";
}
};
void main() {
clrscr();
SearchOperations so;
int choice;
do {
clrscr();
cout << "\nA menu driven program of linear and binary search";
cout << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
cout << "\nSEARCH MENU";
cout << "\n~~~~~~~~~~~";
cout << "\n1. INPUT ARRAY";
cout << "\n2. LINEAR SEARCH";
cout << "\n3. BINARY SEARCH";
cout << "\n4. EXIT";
cout << "\nEnter your choice: ";
cin >> choice;
switch (choice) {
case 1:
so.inputArray();
break;
case 2:
so.printArray();
so.linearSearch();
break;
case 3:
so.sortArray();
so.printArray();
so.binarySearch();
break;
}
getch();
} while (choice != 4);
getch();
}
```
### Algorithm
```cpp
#include <iostream.h>
#include <conio.h>
class Sorting {
private:
int arr[30];
int n;
public:
Sorting() : n(0) {}
void inputArray() {
cout << "\nEnter total number of elements in the array: ";
cin >> n;
for (int i = 0; i < n; i++) {
cout << "Enter element for arr[" << i << "]: ";
cin >> arr[i];
}
}
void printArray() {
cout << "\nGIVEN ARRAY IS AS BELOW:";
for (int i = 0; i < n; i++)
cout << " " << arr[i];
cout << endl;
}
void bubbleSort() {
for (int k = 0; k < n - 1; k++) {
int ptr = 0;
while (ptr < n - k - 1) {
if (arr[ptr] > arr[ptr + 1]) {
int temp = arr[ptr];
arr[ptr] = arr[ptr + 1];
arr[ptr + 1] = temp;
}
ptr++;
}
cout << "\nStep " << k + 1 << ":";
printArray();
}
}
int min(int k) {
int loc = k;
int minimum = arr[k];
for (int i = k; i < n; i++) {
if (arr[i] < minimum) {
loc = i;
minimum = arr[i];
}
}
return loc;
}
void selectionSort() {
for (int k = 0; k <= n - 2; k++) {
int loc = min(k);
int temp = arr[k];
arr[k] = arr[loc];
arr[loc] = temp;
cout << "\nStep " << k + 1 << ":";
printArray();
}
}
void insertionSort() {
for (int k = 1; k < n; k++) {
int temp = arr[k];
int ptr = k - 1;
while (ptr >= 0 && temp < arr[ptr]) {
arr[ptr + 1] = arr[ptr];
ptr--;
}
arr[ptr + 1] = temp;
cout << "\nStep " << k << ":";
printArray();
}
}
};
void main() {
clrscr();
Sorting sorting;
int choice;
do {
clrscr();
cout << "\nA menu driven program of sorting methods";
cout << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
cout << "\nSORT MENU";
cout << "\n~~~~~~~~~";
cout << "\n1. INPUT ARRAY";
cout << "\n2. BUBBLE SORT";
cout << "\n3. INSERTION SORT";
cout << "\n4. SELECTION SORT";
cout << "\n5. EXIT";
cout << "\nEnter your choice: ";
cin >> choice;
switch (choice) {
case 1:
sorting.inputArray();
break;
case 2:
sorting.printArray();
sorting.bubbleSort();
break;
case 3:
sorting.printArray();
sorting.insertionSort();
break;
case 4:
sorting.printArray();
sorting.selectionSort();
break;
}
getch();
} while (choice != 5);
getch();
}
```
PENDING
Q6