I was given a programming assignment.. and part of it states:
"
1. Read how many students are in the file and dynamically create an array of structure to store the student information.
"
I'm not sure what is meant by dynamically create an array?
I was given a programming assignment.. and part of it states:
"
1. Read how many students are in the file and dynamically create an array of structure to store the student information.
"
I'm not sure what is meant by dynamically create an array?
You should use a std::vector, or if that is not allowed, dynamically allocate arrays with new[] and delete[] them when you are done.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
ok thanks (:
the new[] and delete[] I believe is what my professor was referring to
Dynamically allocating memory just means you're storing data without knowing (necessarily) how much data you need to store. It also gives the memory indeterminate scope.
Here's how you create a regular array: (You're probably very familiar with this)
Here's how you dynamically create an array:Code:int my_array[5]; // array of 5 integers
Note that the number in brackets (the 5 in this case) can be a variable.Code:int* my_array = new int[5];
When you're done using that memory:
The reason you'd want dynamic allocation here is because the number of students in the file is variable.Code:delete [] my_array;
Last edited by rudyman; 04-25-2008 at 12:24 PM.
Same teacher? http://cboard.cprogramming.com/showthread.php?t=102265
I think there are more efficient ways of doing this, but I am certain none of them are what your teacher was searching for (bar none, using the stl vector class is the best all-around solution).
In either event, refer the your classmate's(?) link... Though next time it wouldn't hurt to read other threads before posting. His post has never been more than a couple posts lower than yours on the forums.
so I'm trying to dynamically create this array of structures...
should it look something like:
where x is the number of studentsCode:struct* my_array = new my_struct[x];
?? or what?? .. I don't really know -_-''
more likely
Code:my_struct* my_array = new my_struct[x];
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
Click the link I posted above. This person is trying to accomplish the same thing.
^ That one will even compile tooCode:struct my_struct* my_array = new my_struct[x];
^^^^ nobe question but is that not creating a pointer to an array?
dynamic array IS a pointer to the first element
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
so in c++ you cant just create a dynamic array?
int myAry[x];
Not in that way. Standard C++ does not support variable length arrays.so in c++ you cant just create a dynamic array?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Ok another question..........
Sorry dont want to be annoying but i find it so much easier to code when i understand how things work.
What is the difference that creating a pointed to the array allows you to make a dynamic array?