Mini_project

 avatar
unknown
c_cpp
5 months ago
4.9 kB
7
Indexable
#include <bits/stdc++.h>
using namespace std;
using ll = long long;


using namespace std;




void generate_the_RandomArray(int a[], int n) {
    srand(time(0));
    for (int i = 0; i < n; i++) {
        a[i] = rand() % 100; /// Random numbers between 0 and 99
    }
}


void displayArray(int a[], int n) {
    for (int i = 0; i < n; i++) {
        cout << a[i] << " ";
    }
    cout << '\n';
}


void Bubble_Sort(int arr[], int n) {
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < n - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                // Manual swap
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}


void displayMenu() {
    cout << "\nMenu:\n";
    cout << "1. Insert Element\n";
    cout << "2. Delete Element\n";
    cout << "3. Search Element\n";
    cout << "4. Display Array\n";
    cout << "5. Exit\n";
    cout << "Enter your choice: ";
}

// Function to save the array to a file after each operation
void saveToFile(int arr[], int n) {
    ofstream outFile("sorted_array.txt");
    if (outFile.is_open()) {
        for (int i = 0; i < n; i++) {
            outFile << arr[i] << " ";
        }
        outFile.close();
    }
}

// Function to find the position for insertion or deletion using binary search
int binary_Search(int a[], int n, int target, bool ok = false) {
    int l = 0, h = n - 1, ans = -1;
    while (l <= h) {
        int mid = (l + h) / 2;
        if (a[mid] == target) {
            ans = mid;
            if (ok) {
                l = mid + 1; // Search to the right for last occurrence
            } else {
                h = mid - 1; // Search to the left for first occurrence
            }
        } else if (a[mid] < target) {
            l = mid + 1;
        } else {
            h = mid - 1;
        }
    }


    return ans == -1 ? l : ans; /// Returning the  position for insertion
}

/// Function to insert an element into the array
void insert_an_Element(int a[], int n, int val) {
    if (n >= 100) {
        cout << "Overflow: Sorry! We  Cannot insert more elements.\n";
        return;
    }
    int pos = binary_Search(a, n, val, true) + 1; // Insert after last occurrence
    for (int i = n; i > pos; i--) {
        a[i] = a[i - 1]; /// Shift elements to the right
    }
    a[pos] = val;
    n++; // Increase array size
    cout << " The Element inserted successfully.\n";
    saveToFile(a, n); /// Save to file after insertion
}

/// Function to delete an element from the array
void deleteElement(int a[], int n, int val) {
    int pos = binary_Search(a, n, val, true); // Find last occurrence
    if (pos >= 0 && pos < n && a[pos] == val) {
        cout << "Deleting element at position: " << pos + 1 << '\n';
        for (int i = pos; i < n - 1; i++) {
            a[i] = a[i + 1]; // Shift elements to the left
        }
        n--; // Decrease array size
        cout << "Element deleted successfully.\n";
        saveToFile(a, n); // Save to file after deletion
    } else {
        cout << "Item not found.\n";
    }
}

// Function to search for an element in the array
void searchElement(int a[], int n, int val) {
    int pos = binary_Search(a, n, val);
    if (pos < n && a[pos] == val) {
        cout << "Element found at position (first occurrence): " << pos + 1 << endl;
    } else {
        cout << "Element not found.\n";
    }
}

// Main function
int main() {
    int arr[100];
    int n = 10; // Initial size of the array
    generate_the_RandomArray(arr, n);
    cout << "Showing the Random Array : ";
    displayArray(arr, n);

    Bubble_Sort(arr, n);
    cout << "Showing The Sorted Array : ";
    displayArray(arr, n);

    saveToFile(arr, n); // Save the initial sorted array to file

    while (true) {
        displayMenu();
        int select;
        cin >> select;

        switch (select) {
        case 1: {
            cout << "Enter element to insert: ";
            int val;
            cin >> val;
            insert_an_Element(arr, n, val);
            break;
        }
        case 2: {
            cout << "Enter element to delete: ";
            int val;
            cin >> val;
            deleteElement(arr, n, val);
            break;
        }
        case 3: {
            cout << "Enter element to search: ";
            int val;
            cin >> val;
            searchElement(arr, n, val);
            break;
        }
        case 4:
            cout << "Current Array: ";
            Bubble_Sort(arr,n);
            displayArray(arr, n);
            break;
        case 5:
            cout << "Exiting program.\n";
            return 0;
        default:
            cout << "Invalid choice. Try again.\n";
        }
    }
    return 0;
}
Editor is loading...
Leave a Comment