Untitled

 avatar
unknown
c_cpp
a year ago
5.7 kB
6
Indexable
// DYNAMIC ARRAYS
#include <iostream>
#include <limits>
using namespace std;

// Function Prototype
void insert(int *&arr, int &size, int &allocated_size);
void remove(int *&arr, int &size, int &allocated_size);
void elements(int *&arr, int &size, int &allocated_size);

// Driver Code
int main()
{
    // initialization of variables
    int size = 0;
    int allocated_size = 0;
    int choice = 0;

    // welcome
    cout << "\n>-----DYNAMIC ARRAY-----<" << endl
         << endl;
    while (size <= 0)
    {
        cout << "Enter the size of the array : ";
        while (!(cin >> size))
        {
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            cout << "Please enter a valid integer input : ";
        }
        if (size <= 0)
        {
            cout << "Array cannot be of length " << size << endl;
        }
    }

    // Dynamic Memory Allocation
    int *arr = new int[size];
    cout << "An array of size " << size << " has been created successfully." << endl;

    while (true)
    {
        cout << "\nPlease select from the below options:" << endl;
        cout << "1. Insert an element" << endl;
        cout << "2. Delete an element" << endl;
        cout << "3. Display available elements" << endl;
        cout << "4. Exit Portal" << endl;
        cout << "\nEnter your choice: ";
        while (!(cin >> choice))
        {
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            cout << "Please enter a valid integer input : ";
        }
        if (choice == 1)
        {
            insert(arr, size, allocated_size);
        }

        else if (choice == 2)
        {
            remove(arr, size, allocated_size);
        }
        else if (choice == 3)
        {
            elements(arr, size, allocated_size);
        }
        else if (choice == 4)
        {
            return 0;
        }
        else
        {
            cout << "Please enter a valid choice out of the given options" << endl;
        }
    }

    return 0;
}

void elements(int *&arr, int &size, int &allocated_size)
{
    if (allocated_size <= 0)
    {
        cout << "No Elements to display." << endl;
    }
    else
    {
        for (size_t i = 0; i < allocated_size; i++)
        {
            cout << *(arr + i) << " is present at index " << i << endl;
        }
    }
}

void remove(int *&arr, int &size, int &allocated_size)
{
    int index;
    while (true)
    {
        cout << "Enter the index : ";
        while (!(cin >> index))
        {
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            cout << "Please enter a valid integer input : ";
        }
        if (index < 0)
        {
            cout << "Index value can't be less than zero" << endl;
        }
        else
        {
            break;
        }
    }

    if (index >= allocated_size) // Out Of Bound
    {
        cout << "Sorry! The index value is larger than the array size" << endl;
    }
    else if (index == (allocated_size - 1)) // if the index is the last element
    {
        allocated_size--;
        size--;
        int *newarr = new int[size];
        for (size_t i = 0; i < size; i++)
        {
            *(newarr + i) = *(arr + i);
        }

        delete[] arr;

        arr = newarr;
        cout << "Successfully deleted element at index " << index << endl;
    }

    else
    {
        allocated_size--;
        size--;
        int *newarr = new int[size];

        for (size_t i = 0; i < index; i++)
        {
            *(newarr + i) = *(arr + i);
        }

        for (size_t i = index, y = (index + 1); i < size; i++, y++)
        {
            *(newarr + i) = *(arr + y);
        }

        delete[] arr;
        arr = newarr;
    }
}

// Insert Function : This function takes the integer from the user and pushes in into the array and if the space is full then it expands the array and then inserts the value
void insert(int *&arr, int &size, int &allocated_size)
{
    // User Input
    int value;
    cout << "Enter the value to be inserted in the array : ";
    while (!(cin >> value))
    {
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cout << "Please enter a valid int input : ";
    }

    // If the allocated size is less then the actual size of the array then push the element into the array and increment the allocated_size value by one
    if (allocated_size < size)
    {
        *(arr + allocated_size) = value;
        cout << "Successfully inserted " << value << " at index " << allocated_size << endl;
        allocated_size++;
    }

    // If the allocated size is equal to or more than then the actual size of the array then expand the array and push the element into the array
    else
    {
        int newsize = size + 1;
        int *newarr = new int[newsize];

        // copying the elements of old array to newly created array
        for (size_t i = 0; i < size; i++)
        {
            *(newarr + i) = *(arr + i);
        }

        size = newsize;

        // pushing the element
        *(newarr + allocated_size) = value;
        cout << "Successfully expanded the array and inserted " << value << " at index " << allocated_size << endl;
        allocated_size++;

        // deallocating the space of new array
        delete[] arr;

        // pointing the old array pointer to the new array
        arr = newarr;
    }
}
Editor is loading...
Leave a Comment