Resizing of an array.

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Nov 2004
Posts: 4
Reputation: deusieeee is an unknown quantity at this point 
Solved Threads: 0
deusieeee deusieeee is offline Offline
Newbie Poster

Resizing of an array.

 
0
  #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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 56
Reputation: jasweb2002 is an unknown quantity at this point 
Solved Threads: 2
jasweb2002 jasweb2002 is offline Offline
Junior Poster in Training

Re: Resizing of an array.

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

Re: Resizing of an array.

 
0
  #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 Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 715
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Resizing of an array.

 
0
  #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:
  1. // First allocation
  2. T *p = new T[N];
  3.  
  4. ...
  5.  
  6. // Resize by creating a new array
  7. T *save = new T[N * 2];
  8. // Copy the data
  9. for ( int i = 0; i < N; i++ )
  10. save[i] = p[i];
  11. // Change the size to match
  12. N *= 2;
  13. // Destroy the old array
  14. delete [] p;
  15. // Reset to the new array
  16. p = save;
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 4
Reputation: deusieeee is an unknown quantity at this point 
Solved Threads: 0
deusieeee deusieeee is offline Offline
Newbie Poster

Re: Resizing of an array.

 
0
  #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 Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 715
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Resizing of an array.

 
0
  #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 here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 4
Reputation: deusieeee is an unknown quantity at this point 
Solved Threads: 0
deusieeee deusieeee is offline Offline
Newbie Poster

Re: Resizing of an array.

 
0
  #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:
  1. Votes::Votes(int d)
  2. {
  3. ballot_size = 10;
  4. size = 0;
  5. discards = 0;
  6. district = d;
  7. Ballot *b = new Ballot[ballot_size];
  8. bptr = b;
  9. }

INCREASE FUNCTION:
  1. void Votes::Increase()
  2. {
  3. //cout << endl << "***INCREASING THE SIZE***" << endl;
  4.  
  5. //create a temp ballot list of same size
  6. //and store the current ballot list into it.
  7. Ballot *temp = new Ballot[ballot_size];
  8. for (int i = 0; i < ballot_size; i++)
  9. temp[i] = bptr[i];
  10.  
  11. //delete bptr and create a new *b with a size 10 larger
  12. delete [] bptr;
  13. Ballot *b = new Ballot[ballot_size+10];
  14.  
  15. //store the information from the temp into b
  16. for (int j = 0; j < ballot_size; j++)
  17. b[j] = temp[j];
  18.  
  19. //delete temp
  20. delete [] temp;
  21. bptr = b;
  22.  
  23. ballot_size = ballot_size + 10;
  24. }


  1. Votes::~Votes()
  2. {
  3. cout << "DESTRUCTOR RUNS";
  4. delete [] bptr;
  5. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 10
Reputation: omeralper is an unknown quantity at this point 
Solved Threads: 1
omeralper omeralper is offline Offline
Newbie Poster

Re: Resizing of an array.

 
0
  #8
Nov 22nd, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 715
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Resizing of an array.

 
0
  #9
Nov 22nd, 2008
>Narue is doing it right.
It took you four years to figure that out? Please check the date on the last post before replying.
Last edited by Narue; Nov 22nd, 2008 at 10:06 am.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 10
Reputation: omeralper is an unknown quantity at this point 
Solved Threads: 1
omeralper omeralper is offline Offline
Newbie Poster

Re: Resizing of an array.

 
0
  #10
Nov 22nd, 2008
Well but it may be helpful again if you google it.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC