dimanche 17 juin 2018

resize array and add element c++

Hey i know this is simple but for some reason i'm having a tougher time than i thought. All im trying to is if the size of my dynamic array is equal to the actual amount of elements that are in it (meaning it is full) then i want to double the size of the array and add the element

    int add_element(int *array, int size , int &count)
{
    int temp;
    cout << "What number do you want to add ? " << endl;
    cin >> temp;
    if(count = size)
    {
        copy(array, size);
        count++;
        array[count] = temp;
    }
    return count;
}

void copy(int *oldArr  , int size)
{
    int temp = size * 2;
    int *newArr = new int[temp];

    for (int i = 0; i < size; i++) 
    {
        newArr[i] = oldArr[i];

    }
    //delete[] oldArr;
    oldArr = NULL;
    oldArr = newArr;
    delete[] oldArr;

the issue im having is that is not actually doubling the size of the array because when i try to look for the element it just returns the address space. Any help would be a appreciated

Aucun commentaire:

Enregistrer un commentaire