Menu Driven Program
This program provides a simple interactive menu-driven interface for performing basic array manipulation operations. Users can insert elements at different positions, delete elements, and display the current state of the array.
Declaration of Variables:
arr
: An array to store elements.n
: Number of elements in the array.ch1
: Choice variable for the menu.elem
: Element to be inserted.pos
: Position for insertion.
Display Menu:
- Prints the menu options to the console.
Insertion Operations:
insertAtEnd
: Inserts an element at the end of the array.insertAtBegin
: Inserts an element at the beginning of the array.insertAtPos
: Inserts an element at a specified position in the array.insertAfterValue
: Inserts an element after a specified value in the array.
Display Operation:
- Displays the elements of the array.
Deletion Operations:
deleteEnd
: Deletes the last element of the array.deleteBegin
: Deletes the first element of the array.
#include <iostream>
#include <cstdlib>
#define MAX 100
void insertAtEnd(int arr[], int& n, int elem);
void insertAtBegin(int arr[], int& n, int elem);
void insertAtPos(int arr[], int& n, int elem, int pos);
void insertAfterValue(int arr[], int& n, int elem, int val);
void display(const int arr[], int n);
void deleteEnd(int arr[], int& n);
void deleteBegin(int arr[], int& n);
void menu();
int main() {
int arr[MAX], n = 0, ch1, elem, pos;
while (true) {
menu();
std::cout << "Enter your choice :: ";
std::cin >> ch1;
switch (ch1) {
case 1:
std::cout << "Enter element you want to insert :: ";
std::cin >> elem;
insertAtEnd(arr, n, elem);
break;
case 2:
std::cout << "Enter element you want to insert :: ";
std::cin >> elem;
insertAtBegin(arr, n, elem);
break;
case 3:
std::cout << "Enter element you want to insert :: ";
std::cin >> elem;
std::cout << "Enter Position :: ";
std::cin >> pos;
insertAtPos(arr, n, elem, pos);
break;
case 4:
std::cout << "Enter element you want to insert :: ";
std::cin >> elem;
std::cout << "Enter Value :: ";
std::cin >> pos;
insertAfterValue(arr, n, elem, pos); // pos is used as VALUE
break;
case 5:
deleteBegin(arr, n);
break;
case 6:
deleteEnd(arr, n);
break;
case 9:
display(arr, n);
break;
default:
exit(0); // exit the loop and program
}
}
return 0;
}
void menu() {
std::cout << "1. insert at end\n";
std::cout << "2. insert at Begin\n";
std::cout << "3. insert at postion \n";
std::cout << "4. insert after Value \n";
std::cout << "5. delete end \n";
std::cout << "6. delete Begin \n";
std::cout << "9. Display \n";
}
void insertAtEnd(int arr[], int& n, int elem) {
if (n == MAX) {
std::cout << "Array data is FULL ";
} else {
arr[n] = elem;
n++;
}
}
void insertAtBegin(int arr[], int& n, int elem) {
if (n == MAX) {
std::cout << "Array data is FULL ";
} else {
for (int i = n; i > 0; i--)
arr[i] = arr[i - 1];
arr[0] = elem;
n++;
}
}
void insertAtPos(int arr[], int& n, int elem, int pos) {
if (n == MAX) {
std::cout << "Array data is FULL \n";
} else if (pos > n) {
std::cout << "Invalid Position \n";
} else {
for (int i = n; i > pos - 1; i--)
arr[i] = arr[i - 1];
arr[pos - 1] = elem;
n++;
}
}
void insertAfterValue(int arr[], int& n, int elem, int value) {
int pos = -1; // -1 no value found
for (int i = 0; i < n; i++) {
if (arr[i] == value) {
pos = i;
break;
}
}
std::cout << "\n Position = " << pos << "\n";
if (n == MAX) {
std::cout << "Array data is FULL \n";
} else if (pos == -1) {
std::cout << "Value not found \n";
} else {
std::cout << "Found - " << pos << "\n";
for (int i = n; i > pos - 2; i--)
arr[i] = arr[i - 1];
arr[pos - 2] = elem;
n++;
}
}
void display(const int arr[], int n) {
for (int i = 0; i < n; i++)
std::cout << arr[i] << " ";
std::cout << "\n";
}
void deleteEnd(int arr[], int& n) {
if (n == 0) {
std::cout << "Array is Empty ";
return;
} else {
std::cout << "\nDeleting = " << arr[n - 1] << "\n";
n--;
}
}
void deleteBegin(int arr[], int& n) {
if (n == 0) {
std::cout << "Array is Empty ";
return;
} else {
for (int i = 0; i < n; i++)
arr[i] = arr[i + 1];
n--;
}
}