Is this code good for increasing the size of an array?

I read a section on this in my book yesterday and I wanted to see if I could code one myself with out using my book as a reference.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include<iostream>

int *addElementToArray(int *arr, int currSize);

int main()
{
    int arraySize = 2;
    int *arr = new int[arraySize];
    int currentIndex = 0;
    int value;
    std::cout << "Type 0 to exit or any other number to continue\n";
    std::cin >> value;
    while(value != 0)
    {
        if(currentIndex == arraySize)
        {
            addElementToArray(arr, arraySize);
            arraySize++;
            std::cout << "Had to add another element to the array!\n";
        }
        arr[currentIndex] = value;
        currentIndex++;
        std::cout << "Type another value or 0 to exit\n";
        std::cin >> value;
    }
    for(int i = 0; i < arraySize; i++)
    {
        std::cout << arr[i] << "\n";
    }
    return 0;
}

int *addElementToArray(int *arr, int currSize)
{
    int *newArr = new int[currSize + 1];
    for(int i = 0; i < currSize; i++)
    {
        newArr[i] = arr[i];
    }
    delete arr;
    return newArr;
}
Last edited on
Line 40 of your program, you for the square brackets

delete [] arr;

and yes that's a good way to increase the size of the array, and here's another way similar to yours:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>

using namespace std;

void increment(int* const, const int);
void printarray(const int* const, const int);

int main()
{
	const int MAX_ELEMENTS = 3;
	int myarray[MAX_ELEMENTS] = {1000 , 2000 , 3000};

	cout<<*myarray<<endl;
	cout<<*(myarray + 1)<<endl;
	cout<<*(myarray + 2)<<endl;

	cout<<"Increasing scores by passing myarray as a constant pointer.\n\n";
	increment(myarray , MAX_ELEMENTS);

	cout<<"Displaying scores by passing myarray as a constant pointer to a constant.\n";
	printarray(myarray , MAX_ELEMENTS);
	
	return 0;
}
void increment(int* const parray , const int MAX)
{
	for ( int i = 0 ; i < MAX ; ++i )
	{
		parray[i] += 100;
	}
}
void printarray(const int* parray , const int MAX)
{
	for ( int i = 0 ; i < MAX ; ++i )
	{
		cout<<parray[i]<<endl;
	}
}
	


Thank you for your response and code sample!
The original code has a significant error.
At line 17:
addElementToArray(arr, arraySize);
This should actually read:
arr = addElementToArray(arr, arraySize);
Without that, the newly-allocated array is never used, instead values continue to be added to the now deleted old array.

As well as the already mentioned line 40
delete [] arr;

Also, less important, but good practice, just before the end of main(), the array arr should be deleted too. That way, each new [] will be paired with a matching delete []

A variation on the original code.

Rather than returning a pointer to the new array, function addElementToArray returns the new size of the array. Also, rather than increasing the array size by 1 each time, different strategies can be employed, such as adding 10 or 100 elements. This version increases the array size by 50% each time. For large quantities of data, this is much more efficient in terms of the number of re-allocations and copying of elements.

I tested this using a data file rather than keyboard input, it was just a list of prime numbers but any file of integers would do.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <fstream>

int addElementToArray(int * &arr, int currSize);

int main()
{
    int arraySize = 2;
    int *arr = new int[arraySize];
    int currentIndex = 0;
    int value;

    std::ifstream fin("primes.txt");

    while (fin >> value)
    {
        if  (currentIndex >= arraySize)
        {
            arraySize = addElementToArray(arr, arraySize);
            std::cout << "New array size = " << arraySize << std::endl;
        }
        arr[currentIndex++] = value;
    }

    for (int i = 0; i < currentIndex; i++)
    {
        std::cout << arr[i] << " ";
        if ( (i+1)%10==0 )
            std::cout << '\n';
    }
    std::cout << std::endl;

    delete [] arr;

    return 0;
}

int addElementToArray(int * &arr, int currSize)
{
//    int newSize = currSize + 1;
//    int newSize = currSize + 100; // add 100 elements each time
    int newSize = (currSize * 3) / 2;  // increase size by 50%

    int *newArr = new int[newSize];
    for (int i = 0; i < currSize; i++)
    {
        newArr[i] = arr[i];
    }
    delete [] arr;

    arr = newArr;

    return newSize;
}
Last edited on
Thanks I didn't notice that mistake one line 17.
Topic archived. No new replies allowed.