User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 426,017 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,677 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 12485 | Replies: 6
Reply
Join Date: Nov 2004
Posts: 4
Reputation: deusieeee is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
deusieeee deusieeee is offline Offline
Newbie Poster

Resizing of an array.

  #1  
Nov 8th, 2004
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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2004
Posts: 56
Reputation: jasweb2002 is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 1
jasweb2002 jasweb2002 is offline Offline
Junior Poster in Training

Re: Resizing of an array.

  #2  
Nov 8th, 2004
I didn' know you could resize an array. I thought thats what vectors were for.
Reply With Quote  
Join Date: Nov 2004
Posts: 4
Reputation: deusieeee is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
deusieeee deusieeee is offline Offline
Newbie Poster

Re: Resizing of an array.

  #3  
Nov 8th, 2004
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.
Reply With Quote  
Join Date: Sep 2004
Posts: 6,324
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 28
Solved Threads: 458
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: Resizing of an array.

  #4  
Nov 8th, 2004
>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;
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Join Date: Nov 2004
Posts: 4
Reputation: deusieeee is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
deusieeee deusieeee is offline Offline
Newbie Poster

Re: Resizing of an array.

  #5  
Nov 8th, 2004
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[size].RecordVotes(c, m, a);
then crashes.


but it works when i just use Ballot b[10]; in the Votes header file.
Reply With Quote  
Join Date: Sep 2004
Posts: 6,324
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 28
Solved Threads: 458
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: Resizing of an array.

  #6  
Nov 9th, 2004
>b[size].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.
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Join Date: Nov 2004
Posts: 4
Reputation: deusieeee is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
deusieeee deusieeee is offline Offline
Newbie Poster

Re: Resizing of an array.

  #7  
Nov 16th, 2004
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;
}
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 1:21 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC