Name – Punit Manoj Bhatarkar
PRN – 202401120016
Batch – A1
Branch – DS
CODE
#include <iostream>
using namespace std;
int main() {
int n, choice, pos, value;
cout << "Enter number of elements: ";
cin >> n;
int arr[n]; // VLA - size decided at runtime
cout << "Enter elements: ";
for (int i = 0; i < n; i++)
cin >> arr[i];
do {
cout << "\n1. Insert\n2. Update\n3. Delete\n4. Display\n5. Exit\n";
cout << "Enter choice: ";
cin >> choice;
switch (choice) {
case 1: // Insert
cout << "Enter position (0 to " << n << "): ";
cin >> pos;
cout << "Enter value: ";
cin >> value;
for (int i = n; i > pos; i--)
arr[i] = arr[i - 1];
arr[pos] = value;
n++;
cout << "Value inserted.\n";
break;
case 2: // Update
cout << "Enter position (0 to " << n - 1 << "): ";
cin >> pos;
cout << "Enter new value: ";
cin >> value;
arr[pos] = value;
cout << "Value updated.\n";
break;
case 3: // Delete
cout << "Enter position (0 to " << n - 1 << "): ";
cin >> pos;
for (int i = pos; i < n - 1; i++)
arr[i] = arr[i + 1];
n--;
cout << "Value deleted.\n";
break;
case 4: // Display
cout << "Array: ";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
break;
case 5:
cout << "Exiting...\n";
break;
default:
cout << "Invalid choice! Try again.\n";
} while (choice != 5);
return 0;
}
Output
Insert case output
Update case output
Delete case output
Display case output
Exit case output