function to change array size

Hello,

I am trying to write a function to check if we need to resize an array to fit the new values.

please someone explain what should I do with my function

1
2
3
4
5
6
  hangeSize should
Dynamically allocate an array of newEls ints. 
If newEls >= oldEls:
Copy the oldEls values from the old array 
(which ptr points to) to the new array (which we just allocated).
Set the rest of the elements in the new array to 0.


1
2
3
4
Otherwise
Copy newEls values from the old array to the new array.
Deallocate the old array 
Make ptr point to the new array 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void changeSize(int * & ptr, int newEls, int oldEls)
{
	int * newArray = NULL;
	try{ newArray = new int[oldEls+1]; }
	catch (const bad_alloc &)
	{die("allocation failure");}

	if (newEls >= oldEls)
	{
		unsigned i = 0;
		
		
		for (i = 0; i < oldEls; i++)
			newArray[i] = ptr[i];
		for (; i < newEls; i++)
			newArray[i] = 0;
		ptr = newArray;
		oldEls = newEls+1;
	}
	ptr[newEls] = oldEls;
}


Please someone explain what needs to be done.
any help ......!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void changeSize(int*& ptr, int oldEls, int newEls)
{
    //Dynamically allocate an array of newEls ints. 
    int* new_array = new int[newEls]; //Do not catch exception
    std::size_t i = 0;
    for( ; i < std::min(oldEls, newEls); ++i) 
        new_array[i] = ptr[i];
    for( ; i < oldEls; ++i)
        new_array[i] = 0;
    //Deallocate the old array 
    delete[] ptr;
    //Make ptr point to the new array 
    ptr = new_array;
}
//OR
#include <algorithm>
//...
void changeSize(int*& ptr, int oldEls, int newEls)
{
    int* new_array = new int[newEls]{0};
    std::copy(ptr, ptr + std::min(oldEls, newEls), new_array);
    delete[] ptr;
    ptr = new_array;
}
Last edited on
thanks, what could I say in main function to test test the changeSize function
in main function you need to keep track by int currentSize; and int count;

when count == currentSize-1; call to increase currentSize. (say currentSize *=1.5)
Sorry I didn't get what you said, I usually say changeSize(, 1, 1); but since there's a "ptr" how to include in the function call
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void print(const int* arr, int size)
{
    for(int i = 0; i < size; ++i)
        std::cout << arr[i] << ' ';
    std::cout << '\n';
}

int main()
{
    int* foo = new int[5]{1, 2, 3, 4, 5};
    print(foo, 5);
    changeSize(foo, 5, 8);
    foo[7] = 8;
    print(foo, 8);
}

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
// resize dynamic array on the go
//anup 23.11.2014
#include <iostream>
using namespace std;

void increaseSize(int *&ar, int SIZE, int newSize)
{
	cout << "increaseSize called\n";
	int *arr = new int [newSize];
	for(int i=0; i<SIZE; i++)
	{
		arr[i] = ar[i]; 
	} 
	delete[] ar;	
	ar = arr;

return;
}


int main()
{
	int size=0, count=0, sum=0, newSize;	
	int *ar = new int;
		size=1;
	
	cout << "keep entering peoples donations: (-ve to stop)\n";
	int n;
	do
	{
		cout << "enter new amount: ";
		cin >> n;
		if(n<0 || !n) // negatives or chars
			break;
		ar[count] = n;
		count++;
		sum+= n;
		if(count>=size)
		{
			newSize = 2*size;
			increaseSize(ar, size, newSize);
			size = newSize;
		}
				
	}while(true);

cout << "\n\n total collected: " << sum << ",  average donation: " << sum/double(count) << "\n\n";
return 0;
}
Topic archived. No new replies allowed.