Need help implementing a gradebook using dynamic memory
Expand Post »
This is my first time using pointers and I'm having some trouble. So far I have declared a struct named Student which is meant to hold the data for each student. There is an input file with an unknown amount of entries. Each entry has a first and last name along with 7 scores.
I am trying to read in the data from the text file at this point. Then I will need to sort the data (selection sort) in alphabetical order by the students last names. And lastly output the data to a new text file.
Below I'll provide what I've done so far. Really I'm just trying to get a better insight as to how I should be approaching this problem. I can figure out the sorting and output later.
Re: Need help implementing a gradebook using dynamic memory
Do not use getline here. The first name can be UP TO 30 characters, but will usually be less than that. Your getline stores other fields in the first name. This input file is separated by spaces and newlines, so it is tailor made for the >> operator instead of getline. I see no need to read in the data using getline.
Re: Need help implementing a gradebook using dynamic memory
Thanks for the help. That is makes things much clearer. My next question is: How does the array continue to expand in order to span the length of the input file? I just did a simple print to the output file and it only displays the first 5 names on the list. I know that is because I set size = 5. What should I have done to make sure the array will continue to allocate memory?
Thanks for the help. That is makes things much clearer. My next question is: How does the array continue to expand in order to span the length of the input file? I just did a simple print to the output file and it only displays the first 5 names on the list. I know that is because I set size = 5. What should I have done to make sure the array will continue to allocate memory?
You have a few options.
Pick an original size that you know will be large enough to handle anything you'll encounter (say 1,000,000) and size it to that initially. It's a waste of memory and bad coding practice, but resizing will be unnecessary. That's assuming that such a maximum exists in the first place.
Use a vector or something else that does the expanding and contracting for you.
Go through the file once and count how many records there are and size the array to that, then go through again to read the data.
Do an initial guess of the size, like you do with 5. Start reading the file. When you run out of room, resize the array manually by declaring a new array of the new size, deep copy the elements from the first array to the new array, then release the memory of the original array and reassign any pointers.
Use malloc, calloc, realloc, and free rather than new and delete in order to possibly avoid a deep copy - this is a C solution rather than a C++ solution.
I would imagine you are expected to implement approach 4 above. Here' a link where Narue comments.
Re: Need help implementing a gradebook using dynamic memory
Yea your correct. I need to dynamically expand the array as needed. I took a look at the link you gave me (thanks by the way) and am still having trouble implementing it correctly. So far this is what i have:
Re: Need help implementing a gradebook using dynamic memory
Define "still having trouble implementing it correctly". What is happening that shouldn't be happening or what isn't happening that should be happening?
Re: Need help implementing a gradebook using dynamic memory
I am unsure as to where I should be placing size *= 2. Its purpose is to double the size of the array each time it is necessary.
At first, I didn't know whether I should be placing the new array (temp) inside the loop which is pulling data from the input file. The link you posted was helpful. I am just having trouble applying it to my program correctly. The program as it is now will now display 10 items, which isn't the end product I am looking for.
What I would like to do is have this new array named temp to continue and allocate memory until the input file has been accounted for entirely. Am I on the right track by setting up the new temp array after the original for loop, and running a series of them on its own? Thanks again for your help.
I am unsure as to where I should be placing size *= 2. Its purpose is to double the size of the array each time it is necessary.
At first, I didn't know whether I should be placing the new array (temp) inside the loop which is pulling data from the input file. The link you posted was helpful. I am just having trouble applying it to my program correctly. The program as it is now will now display 10 items, which isn't the end product I am looking for.
What I would like to do is have this new array named temp to continue and allocate memory until the input file has been accounted for entirely. Am I on the right track by setting up the new temp array after the original for loop, and running a series of them on its own? Thanks again for your help.
Don't confuse size with capacity. Size is the number of actual elements. Capacity is the number of elements you have room to store. size must always be less than or equal to capacity. It's usually less than capacity.
So you need a capacity variable and a size variable. "Resizing" involves changing the capacity, not the size. Obviously, "resize" can lead to confusion. Feel free to call your variables arrayCapacity and numStudents , so it will be extra clear. I would in fact advise doing that to avoid confusion.
So any display must involve the "size" variable. Any "resizing" involves changing the "capacity" of the array.
No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.