Dynamic Array and tacking a linked list to array?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2009
Posts: 141
Reputation: power_computer is an unknown quantity at this point 
Solved Threads: 3
power_computer power_computer is offline Offline
Junior Poster

Dynamic Array and tacking a linked list to array?

 
0
  #1
Nov 6th, 2009
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

  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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,646
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1498
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning
 
-7
  #2
Nov 6th, 2009
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 141
Reputation: power_computer is an unknown quantity at this point 
Solved Threads: 3
power_computer power_computer is offline Offline
Junior Poster
 
0
  #3
Nov 7th, 2009
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

  1.  
  2. 1233434 Name 40
  3. 4444445 Name 23

First number is the SSN
Next is the name
then hours.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 141
Reputation: power_computer is an unknown quantity at this point 
Solved Threads: 3
power_computer power_computer is offline Offline
Junior Poster
 
0
  #4
Nov 7th, 2009
Bump, I know there are some experienced C++ users in this forum, all help is appreciated.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,842
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster
 
0
  #5
Nov 8th, 2009
Originally Posted by power_computer View Post
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

  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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 141
Reputation: power_computer is an unknown quantity at this point 
Solved Threads: 3
power_computer power_computer is offline Offline
Junior Poster
 
0
  #6
Nov 10th, 2009
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

  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
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,842
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster
 
0
  #7
Nov 10th, 2009
Originally Posted by power_computer View Post
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.

Originally Posted by power_computer View 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

  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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 141
Reputation: power_computer is an unknown quantity at this point 
Solved Threads: 3
power_computer power_computer is offline Offline
Junior Poster
 
0
  #8
Nov 10th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,842
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster
 
0
  #9
Nov 10th, 2009
Originally Posted by power_computer View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 141
Reputation: power_computer is an unknown quantity at this point 
Solved Threads: 3
power_computer power_computer is offline Offline
Junior Poster
 
0
  #10
Nov 10th, 2009
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


  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
Reply With Quote Quick reply to this message  
Reply

Message:




Views: 531 | Replies: 11
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC