| | |
Resizing of an array.
Thread Solved
![]() |
•
•
Join Date: Nov 2004
Posts: 4
Reputation:
Solved Threads: 0
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.
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 thought thats what vectors were for.
And how did you think vectors were implemented? You simulate a dynamically sized array through pointers and new:
And how did you think vectors were implemented? You simulate a dynamically sized array through pointers and new:
C++ Syntax (Toggle Plain Text)
// 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 here to prove you wrong.
•
•
Join Date: Nov 2004
Posts: 4
Reputation:
Solved Threads: 0
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.
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.
>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 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.
•
•
Join Date: Nov 2004
Posts: 4
Reputation:
Solved Threads: 0
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:
INCREASE FUNCTION:
Let me know what you think ...
in the header i have:
Ballot *b;
Ballot *bptr;
Constructor:
C++ Syntax (Toggle Plain Text)
Votes::Votes(int d) { ballot_size = 10; size = 0; discards = 0; district = d; Ballot *b = new Ballot[ballot_size]; bptr = b; }
INCREASE FUNCTION:
C++ Syntax (Toggle Plain Text)
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; }
C++ Syntax (Toggle Plain Text)
Votes::~Votes() { cout << "DESTRUCTOR RUNS"; delete [] bptr; }
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: automating login to a server using c++
- Next Thread: Building a class in c++
| Thread Tools | Search this Thread |
api array based binary bitmap business c++ c/c++ char class classes code coding commentinghelp compile console conversion count decide delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error faq file forms fstream function functions game givemetehcodez graph gui hash homeworkhelp homeworkhelper iamthwee ifpug ifstream infinite input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem proficiency program programming project python random read recursion reference rpg string strings temperature template templates test text text-file tree url variable vector video win32 windows winsock word wordfrequency wxwidgets






