Dynamic Array "Resizing"

Oct 22, 2014 at 12:57am
I have to write a program where the user will input integer numbers. How many numbers they enter is unknown, therefor you should use a repetition structure for the input. When the user is done, they will enter -1 to exit.

Create a dynamic array if the size=2( the initial size must be 2)
Repeat until user enters -1.

I have to do this without using vectors.

This is what i have, I cannot figure out what to put in main. I was thinking of a do-while?

This is my first time in c++
Any hints?

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
#include <iostream>
using namespace std;
void resize(int *[], int);
int main()
{
	int *listDyn;
	int size=2;
	listDyn=new int[size];

	
		for (int i=0; i<size; i++)
		{
			cout << "Enter as many integers as you'd like or enter -1 to exit" ;
			cin >> listDyn[i]; 
		}


void resize(int *listDyn, int size)
{
	int *listDynNew=new int[size*2];
	for (int i=0; i<size; i++)
		listDynNew[i]=listDyn[i];
	size++;
	listDyn=listDynNew;
	delete[] listDynNew;
}

	
	
}
Oct 22, 2014 at 1:32am
There're two functions to help you manipulate dynamic array.

int *listDyn = (int*)malloc(sizeof(int) * size);

This function allocates 'size' spaces for your dynamic array.

int *extended = (int*)realloc(listDyn, sizeof(int) * newsize);

This function creates a new array with 'newsize' spaces, and it copies the data in listDyn into new array.

Oct 22, 2014 at 1:44am
Is there a way to do it without those functions?

We haven't learned that yet.
Oct 22, 2014 at 2:24am
Algo:
1
2
3
4
5
6
7
create initial dynamic array with size 2
do
   get input
   get input count //the number of inputs
   if(input count == sizeof(array))
       resize array by any amount you want
while(input != -1)

Oct 22, 2014 at 2:41am
You need to keep track of both the size of the array ( the number of valid elements in it) and the capacity (the number that it is currently capable of holding. When you run out of space, you double the capacity, and add 1 to the size.

Although malloc/realloc will work for arrays of integers, they won't work right for arbitrary classes because they don't call the constructors/destructors, so you should probably do it with new[] and delete[] as you already have it.
Oct 22, 2014 at 5:29am
So i kind of understand but im still a little confused.
I now have this:
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
using namespace std;
int resize(int *, int);
int main()
{
	int *listDyn;
	int size=2;
	listDyn=new int[size];
	int input;

	cout << "Please enter a number or enter -1 to exit" << endl;
	cin >> input;
	while(input != -1)
	{
		for (int i=0;i<size;i++)
		{
			listDyn[i]=input;
		}
		if()
			
	}
	for (int i=0;i<size;i++)
		cout << listDyn[i] << endl;
	

system("pause");
}
int resize(int *listDyn, int size)
{
	int *listDynNew=new int[size*2];
	for (int i=0; i<size; i++)
	{
	listDynNew[i]=listDyn[i];
	}
	listDyn=listDynNew;
	size++;

	delete[] listDyn;

	return listDynNew;
}


Im not sure what to put in the if statement and idk where to include my resize function.
Do you see anything else wrong with my program?
Oct 23, 2014 at 3:04am
Your resize function looks correct to me but the main loop isn't right. For one thing, you only let the user input a single number. The input loop should be something like this:

1
2
3
4
5
6
for (i=0; true; ++i) {
	cout << "Please enter a number or enter -1 to exit" << endl;
	cin >> input;
	if (!cin || input == -1) break;
	listDyn[i] = input;
}


This doesn't resize listDyn yet. You'll have to add that code.

Note that when you exit the loop, i contains the number of items in listDyn.



Last edited on Oct 23, 2014 at 3:05am
Topic archived. No new replies allowed.