943,931 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1319
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 6th, 2009
0

Dynamic Array and tacking a linked list to array?

Expand Post »
Ok, I am trying to figure out how to purse this current project which deals with dynamic allocation and linked list

Here is the information I am given the following two structs

C++ Syntax (Toggle Plain Text)
  1.  
  2. struct employee
  3. {
  4. int ssn;
  5. string name;
  6. float hours;
  7. assignment * list;
  8. };
  9.  
  10. struct assignment
  11. {
  12. string proj;
  13. float hours;
  14. assignment *next;
  15. };

Objectives:

read from a file into a dynamic array of employee struct, size of array will be 3 when program starts, int ssn will serve as unique identifer.

read the second file and add a new node to the employee's linked list of assigned projects. The final arrangement
will be an array (dynamically allocated) of struct1 where each employee contains a pointer to a linked
list of the projects to which they are assigned. The employees in the array will be in ascending order by
SSN. The projects assigned to each employee will be in descending order by the hours assigned.

Any tips to get me going?
Reputation Points: 30
Solved Threads: 3
Junior Poster
power_computer is offline Offline
144 posts
since Feb 2009
Nov 6th, 2009
-7
Re: Dynamic Array and tacking a linked list to array?
Yes, start out by opening the file and learning how to just read it. Just display the file contents on the screen.

After you get that going, you should be able to start reading the file contents into the structure. You didn't say how the file is layed out so I can't really help you any more.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Nov 7th, 2009
0
Re: Dynamic Array and tacking a linked list to array?
I am not a first time posted here dragon, obviously.
However, I dont expect you to remember my skill set.

I know how to open a file, read from a file, read into a struct array, class array etc. Ive never used a dynamic array which shouldnt be too hard, ive never worked with linked lists which are challenging to me right now.

The first file looks something like

C++ Syntax (Toggle Plain Text)
  1.  
  2. 1233434 Name 40
  3. 4444445 Name 23

First number is the SSN
Next is the name
then hours.
Reputation Points: 30
Solved Threads: 3
Junior Poster
power_computer is offline Offline
144 posts
since Feb 2009
Nov 7th, 2009
0
Re: Dynamic Array and tacking a linked list to array?
Bump, I know there are some experienced C++ users in this forum, all help is appreciated.
Reputation Points: 30
Solved Threads: 3
Junior Poster
power_computer is offline Offline
144 posts
since Feb 2009
Nov 8th, 2009
0
Re: Dynamic Array and tacking a linked list to array?
I am not a first time posted here dragon, obviously.
However, I dont expect you to remember my skill set.

I know how to open a file, read from a file, read into a struct array, class array etc. Ive never used a dynamic array which shouldnt be too hard, ive never worked with linked lists which are challenging to me right now.

The first file looks something like

C++ Syntax (Toggle Plain Text)
  1.  
  2. 1233434 Name 40
  3. 4444445 Name 23

First number is the SSN
Next is the name
then hours.


You won't be doing any dynamic re-allocation because the original size is 3 and you have two records.

You need to figure out what factor you want to resize the dynamic array by. A decent default is to double it each time you resize it larger and halve it every time you make it smaller. To do so, allocate the new memory for the appropriate size, do a deep copy of the old array to the new array, then delete the old array. That's if you want to use "new" and "delete". You can also use "realloc". It's more efficient due to the fact that often no deep copying needs to be done.

I'd drop the assignment for now and practice dynamically allocating and resizing a plain old integer array and populating a plain old integer linked list, since these are both new concepts to you. Learn them, then come back and make it work with the structs. Then try to integrate the two into your assignment.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Nov 10th, 2009
0
Re: Dynamic Array and tacking a linked list to array?
Well I believe the array gets resized depending on the number of items in the list each line read is one item so I am going to problem read the file for first for a count of elements something like this

C++ Syntax (Toggle Plain Text)
  1. fin.open("some.file")
  2.  
  3. while(fin>>ssn>>name>>hours)
  4. {
  5. n++
  6. }
  7.  
  8. and where n in the count of items in the array since the output will be formatted this way
  9.  
  10. and doing something like
  11.  
  12. employee emparr = new emparr[n]

I know thats not the exact syntax but you get my idea, thanks for the tips, ive played about a little with linked lists due to my time constraints ill report back later once ive played around with both which with my last assignment playing around with code around the web and book, helped when learning templates and overloading functions
Reputation Points: 30
Solved Threads: 3
Junior Poster
power_computer is offline Offline
144 posts
since Feb 2009
Nov 10th, 2009
0
Re: Dynamic Array and tacking a linked list to array?
read from a file into a dynamic array of employee struct, size of array will be 3 when program starts, int ssn will serve as unique identifer.
I'm focusing in on the "size of array will be 3 when program starts" part of your first post.

Well I believe the array gets resized depending on the number of items in the list each line read is one item so I am going to problem read the file for first for a count of elements something like this

C++ Syntax (Toggle Plain Text)
  1. fin.open("some.file")
  2.  
  3. while(fin>>ssn>>name>>hours)
  4. {
  5. n++
  6. }
  7.  
  8. and where n in the count of items in the array since the output will be formatted this way
  9.  
  10. and doing something like
  11.  
  12. employee emparr = new emparr[n]

This doesn't match the description as I read it. I read it like this, assuming there are 14 elements in the file:
  1. Start with array size 3.
  2. Read in first three elements.
  3. Before reading in 4th element, resize array to 6.
  4. Read in elements 4 to 6.
  5. Before reading in element 7, resize array to size 12.
  6. Read in elements 7 through 12.
  7. Before reading in element 13, resize the array to 24.
  8. Read in elements 13 and 14.

Again, that's my interpretation and I'm having to fill in some blanks. There's no guarantee that my interpretation is correct, but I see no other reason for the initial size to be 3 when the program starts. You're basically mimicking the behavior of a vector. You should ask your teacher to clarify.
Last edited by VernonDozier; Nov 10th, 2009 at 12:43 am.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Nov 10th, 2009
0
Re: Dynamic Array and tacking a linked list to array?
Well the first three elements are considered one item in the array

emparr[0].ssn
emparr[1].name
emparr[0].hours

He really didnt clarify all he said default size of 3
Reputation Points: 30
Solved Threads: 3
Junior Poster
power_computer is offline Offline
144 posts
since Feb 2009
Nov 10th, 2009
0
Re: Dynamic Array and tacking a linked list to array?
Well the first three elements are considered one item in the array

emparr[0].ssn
emparr[1].name
emparr[0].hours

He really didnt clarify all he said default size of 3
Yeah, I'm treating the ssn, name, and hours as one object, so I'm thinking a 14 line file, each line containing those three items.

You'll either need clarification or you'll need to make some assumptions. If you have a 14 line file and the default size is three, what are you going to do when you try to store something in index 3? There's no room, so you can't read it in without resizing. Again, that's how I read it, but it's open to interpretation, so if you're reading it differently, you should ask for clarification on what to do with a 14 element input file. You really can't move on till you know.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Nov 10th, 2009
0
Re: Dynamic Array and tacking a linked list to array?
Then again I guess that would be the purpose of this assignment is to learn dynamic allocation, so if I set the default to 3 and if comes to some n element greater than three I suppose this would work


C++ Syntax (Toggle Plain Text)
  1. while(infile)
  2. {
  3. if(n >= 3)
  4. emparr = new emparr[n];
  5. }

I am going go with the purpose of dynamic allocation and say the array will need resizing but I believe the code above isnt entirely correct because Ill be declaring a new emparr which i will have to delete the first array before hand and before the delete ill had to copy the array delete the current one used for reading and then declare a new array copy old array into new and delete old I assume, that seems a tad arguious because i dont believe he use and functions like memcpy or something of the sort
Reputation Points: 30
Solved Threads: 3
Junior Poster
power_computer is offline Offline
144 posts
since Feb 2009

This thread is more than three months old

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.
Message:
Previous Thread in C++ Forum Timeline: Need help with error...please
Next Thread in C++ Forum Timeline: Copy constructor- "Bus Error"





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC