| | |
Dynamic Array and tacking a linked list to array?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2009
Posts: 141
Reputation:
Solved Threads: 2
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
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?
Here is the information I am given the following two structs
C++ Syntax (Toggle Plain Text)
struct employee { int ssn; string name; float hours; assignment * list; }; struct assignment { string proj; float hours; assignment *next; };
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?
-7
#2 30 Days Ago
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.
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.
•
•
Join Date: Feb 2009
Posts: 141
Reputation:
Solved Threads: 2
0
#3 30 Days Ago
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
First number is the SSN
Next is the name
then hours.
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)
1233434 Name 40 4444445 Name 23
First number is the SSN
Next is the name
then hours.
•
•
Join Date: Jan 2008
Posts: 3,828
Reputation:
Solved Threads: 501
0
#5 29 Days Ago
•
•
•
•
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)
1233434 Name 40 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.
•
•
Join Date: Feb 2009
Posts: 141
Reputation:
Solved Threads: 2
0
#6 27 Days Ago
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
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
C++ Syntax (Toggle Plain Text)
fin.open("some.file") while(fin>>ssn>>name>>hours) { n++ } and where n in the count of items in the array since the output will be formatted this way and doing something like 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
•
•
Join Date: Jan 2008
Posts: 3,828
Reputation:
Solved Threads: 501
0
#7 27 Days Ago
•
•
•
•
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.
•
•
•
•
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)
fin.open("some.file") while(fin>>ssn>>name>>hours) { n++ } and where n in the count of items in the array since the output will be formatted this way and doing something like 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:
- Start with array size 3.
- Read in first three elements.
- Before reading in 4th element, resize array to 6.
- Read in elements 4 to 6.
- Before reading in element 7, resize array to size 12.
- Read in elements 7 through 12.
- Before reading in element 13, resize the array to 24.
- 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; 27 Days Ago at 12:43 am.
•
•
Join Date: Jan 2008
Posts: 3,828
Reputation:
Solved Threads: 501
0
#9 26 Days Ago
•
•
•
•
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
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.
•
•
Join Date: Feb 2009
Posts: 141
Reputation:
Solved Threads: 2
0
#10 26 Days Ago
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
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
C++ Syntax (Toggle Plain Text)
while(infile) { if(n >= 3) emparr = new emparr[n]; }
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
![]() |
Similar Threads
- multiple stack in one dimentional array using node/ linked list (Java)
- Linked List of Array (C++)
- Removing an item from head of linked list (C)
- trouble with double linked list (C++)
- double linked list array of pointers, on the right track? (C++)
- Bubble Sort in Linked List- Help Greatly Appreciated (Java)
- Please Help With Linked List Programming (C++)
Other Threads in the C++ Forum
- Previous Thread: Need help with error...please
- Next Thread: Copy constructor- "Bus Error"
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game generator givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






