| | |
Resizing of an array.
Please support our C++ advertiser: Intel Parallel Studio Home
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 beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count data database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game getline google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text text-file tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






