Untitled

Anonymous
Plain Text
10/30/2025 4:00 AM
19.1 KB
12
Indexable
Never
#include <iostream>
using namespace std;

#define MAX 100  // Maximum size of array

// Function to traverse and display array elements
void traverse(int arr[], int n) {
        cout << "Array elements are: ";
            for (int i = 0; i < n; i++)
                    cout << arr[i] << " ";
                        cout << endl;
}

// Function to insert element at given position
int insertElement(int arr[], int n, int pos, int value) {
        if (n >= MAX) {
                    cout << "Array is full! Cannot insert new element.\n";
                            return n;
        }
            if (pos < 0 || pos > n) {
                        cout << "Invalid position!\n";
                                return n;
            }

                // Shift elements to right
                    for (int i = n; i > pos; i--)
                            arr[i] = arr[i - 1];

                                arr[pos] = value; // Insert new value
                                    cout << "Element inserted successfully.\n";
                                        return n + 1;
}

// Function to delete element from given position
int deleteElement(int arr[], int n, int pos) {
        if (pos < 0 || pos >= n) {
                    cout << "Invalid position!\n";
                            return n;
        }

            cout << "Element deleted: " << arr[pos] << endl;

                // Shift elements to left
                    for (int i = pos; i < n - 1; i++)
                            arr[i] = arr[i + 1];

                                return n - 1;
}

// Function to search an element in the array
int searchElement(int arr[], int n, int value) {
        for (int i = 0; i < n; i++) {
                    if (arr[i] == value)
                                return i; // return index if found
        }
            return -1; // not found
}

// Function to update an element at a given position
void updateElement(int arr[], int n, int pos, int newValue) {
        if (pos < 0 || pos >= n) {
                    cout << "Invalid position!\n";
                            return;
        }
            arr[pos] = newValue;
                cout << "Element updated successfully.\n";
}

int main() {
        int arr[MAX], n, choice, pos, value;

            cout << "Enter number of elements in array: ";
                cin >> n;

                    cout << "Enter array elements:\n";
                        for (int i = 0; i < n; i++)
                                cin >> arr[i];

                                    do {
                                                cout << "\n--- ARRAY OPERATIONS MENU ---\n";
                                                        cout << "1. Traverse\n";
                                                                cout << "2. Insert\n";
                                                                        cout << "3. Delete\n";
                                                                                cout << "4. Search\n";
                                                                                        cout << "5. Update\n";
                                                                                                cout << "6. Exit\n";
                                                                                                        cout << "Enter your choice: ";
                                                                                                                cin >> choice;

                                                                                                                        switch (choice) {
                                                                                                                                        case 1:
                                                                                                                                                        traverse(arr, n);
                                                                                                                                                                        break;

                                                                                                                                                                                    case 2:
                                                                                                                                                                                                    cout << "Enter position (0-based index): ";
                                                                                                                                                                                                                    cin >> pos;
                                                                                                                                                                                                                                    cout << "Enter value: ";
                                                                                                                                                                                                                                                    cin >> value;
                                                                                                                                                                                                                                                                    n = insertElement(arr, n, pos, value);
                                                                                                                                                                                                                                                                                    break;

                                                                                                                                                                                                                                                                                                case 3:
                                                                                                                                                                                                                                                                                                                cout << "Enter position (0-based index): ";
                                                                                                                                                                                                                                                                                                                                cin >> pos;
                                                                                                                                                                                                                                                                                                                                                n = deleteElement(arr, n, pos);
                                                                                                                                                                                                                                                                                                                                                                break;

                                                                                                                                                                                                                                                                                                                                                                            case 4:
                                                                                                                                                                                                                                                                                                                                                                                            cout << "Enter value to search: ";
                                                                                                                                                                                                                                                                                                                                                                                                            cin >> value;
                                                                                                                                                                                                                                                                                                                                                                                                                            pos = searchElement(arr, n, value);
                                                                                                                                                                                                                                                                                                                                                                                                                                            if (pos != -1)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                cout << "Element found at index " << pos << endl;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                else
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    cout << "Element not found!\n";
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    break;

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                case 5:
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                cout << "Enter position (0-based index): ";
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                cin >> pos;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                cout << "Enter new value: ";
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                cin >> value;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                updateElement(arr, n, pos, value);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                break;

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            case 6:
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            cout << "Exiting program.\n";
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            break;

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        default:
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        cout << "Invalid choice! Try again.\n";
                                                                                                                        }
                                    } while (choice != 6);

                                        return 0;
}
                                                                                                                        }
                                    }
}
        }
}
        }
}
        }
}
            }
        }
}
}
Editor is loading...
Leave a Comment