I have a assignment that we are to write two clases (Ballot, Votes). We are to use an array of Ballot type to hold information for each particular Ballot. The Votes class is used to mark the ballots, count up all to votes, delete a ballot etc. I have the program working completley except for the resizing of the array of Ballots.

The list of ballots must be implemented with an array (i.e. of Ballot objects). This array must be dynamically allocated and must have the ability to resize, according to the following rules. There may be at most 10 unused array slots at any given time. This means that if and when it is necessary to allocate more space for the array, it cannot be more than 10 spaces at a time. This also means that if votes are deleted from the list so that the number of unused slots increases to more than 10, the array should be resized down, so that space is not being wasted. You should probably think about adding in one or more extra member functions to handle array resizing, and be sure to call upon such functions when necessary.

I'm stuck on ideas of how to go about doing this. Any help // ideas will be greatly apreciated.

Thanks.

I didn' know you could resize an array. I thought thats what vectors were for.

i belive it is going to be dynamically allocating memory for the new array size, and deleting the dynamically allocated memory. but i am just not versed much on doing such things.

>I thought thats what vectors were for.
And how did you think vectors were implemented? You simulate a dynamically sized array through pointers and new:

// First allocation
T *p = new T[N];

...

// Resize by creating a new array
T *save = new T[N * 2];
// Copy the data
for ( int i = 0; i < N; i++ )
  save[i] = p[i];
// Change the size to match
N *= 2;
// Destroy the old array
delete [] p;
// Reset to the new array
p = save;

When i dynamically assign the size of the array of Ballot Objects in my 1st allocation:
Ballot *b = new Ballot[ballot_size];

all seems well until i call the Votes::AddVote(Candidate c, Mark m, int a) function.
which it calls:
b.RecordVotes(c, m, a);
then crashes.


but it works when i just use Ballot b[10]; in the Votes header file.

>b.RecordVotes(c, m, a);
I may just be paranoid, but indexing an array with a variable called size is very suspicious.

>but it works when i just use Ballot b[10]; in the Votes header file.
First, the array definition shouldn't be in a header file. Second, give us a compilable example so that we can give you a better suggestion than your computer is possessed. And make sure the example is small.

Sorry i didnt post that i solved the problem.
Let me know what you think ...


in the header i have:

Ballot *b;
Ballot *bptr;


Constructor:

Votes::Votes(int d)
{
	ballot_size = 10;
	size = 0;
	discards = 0;	
	district = d;
	Ballot *b = new Ballot[ballot_size];
	bptr = b;
}

INCREASE FUNCTION:

void Votes::Increase()
{
	//cout << endl << "***INCREASING THE SIZE***" << endl;
	
	//create a temp ballot list of same size
	//and store the current ballot list into it.
	Ballot *temp = new Ballot[ballot_size];
	for (int i = 0; i < ballot_size; i++)
		temp[i] = bptr[i];

	//delete bptr and create a new *b with a size 10 larger
	delete [] bptr;
	Ballot *b = new Ballot[ballot_size+10];
	
	//store the information from the temp into b
	for (int j = 0; j < ballot_size; j++)
		b[j] = temp[j];
	
	//delete temp
	delete [] temp;
	bptr = b;

	ballot_size = ballot_size + 10;
}
Votes::~Votes()
{
	cout << "DESTRUCTOR RUNS";
	delete [] bptr;
}

Narue is doing it right. You can resize the array by creating a new array which its size is different. And you can pass the datas, delete the old array. Thats all. As narue did it.

>Narue is doing it right.
It took you four years to figure that out? Please check the date on the last post before replying.

Well but it may be helpful again if you google it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.