Jump to content

Dynamic Arrays-C++

stettlinger

So i need to write this program and i am lost at declaring arrays... Can someone help me out with the assignment.

 

Create a class called DynamicStringArray which includes member
functions that allow it to emulate the behaviour of a vector of strings. The class must have:

A private member variable called dynamicArray which references a dynamic array of type
string
A private member variable called size which holds the number of entries in the array
A default constructor which sets the size 0 and the dynamicArray to NULL
A function to return size
A function named addEntry that takes a string as input. The function should create a new
dynamic array one element larger than dynamicArray, copy all the elements from
dynamicArray into the new array, add the new string onto the end of the new array,
increment size, delete the old dynamicArray and then set dynamicArray to the new array
A function named deleteEntry which takes a string as input. The function must search
dynamicArray for the string. If not found, it returns false. If found, it creates a new dynamic
array, one element smaller than dynamicArray. If must copy all elements except the input
string into the new array, delete dynamicArray, decrement size and return true. For
simplicity, if there are two strings with the same value, delete only the first value
A function named getEntry which takes an integer as input and returns the string at that
index in dynamicArray. It must return NULL if the index is out of the range of
dynamicArray

A destructor which frees the memory allocated to the dynamic array

 

 

 

Thanks so much
 

Link to comment
Share on other sites

Link to post
Share on other sites

You have a lot of the information that you need already there.

I suggest using a normal std::string dynamicArray[] array and set the size to something useful. 

Since you need to return NULL, then it indicates that you should use pointers to strings.  This is much more efficient with memory management.

 

At least create the basic structure and try some implementation then come back to us. we aren't here to do your work for you (hint- we have our own work to do as well)  :P

Aragorn (WS): 250D | 6800k | 840 Pro 512GB | Intel 530 480GB  | Asus X99-M WS | 64GB DDR4 | Corsair HX720i | GTX 1070 | Corsair H115i | Philips BDM4350UC 43" 3840x2160 IPS

Gimli (server):  Node 304 | G4560 | ADATA XPG SX8000 128GB | 2x 5TB WD Red | ASROCK H270M-ITX/AC  | 8GB DDR4 | Seasonic 400FL

 Omega (server):                 Fractal Arc Mini R2 | i3 4130 | 500GB Maxtor | 2TB WD Red : Raid 1 | 3TB Seagate Barracuda | 16GB RAM | Seasonic G-450w
Alpha (WS): 900D | 4770k | GTX 780  | 840 Pro 512GB  | GA-Z87X-OC | Corsair RM 850 | 24GB 2400mhz | Samsung S27B970D 2560x1440

                              ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

Link to comment
Share on other sites

Link to post
Share on other sites

(hint- we have our own work to do as well)

another hint:

we have our work to do but we're posting stuff here, so we're not doing our work either

 

 

I suggest using a normal std::string dynamicArray[] array and set the size to something useful. 

Since you need to return NULL, then it indicates that you should use pointers to strings.  This is much more efficient with memory management.

actually he's told to use dynamic arrays, not static ones

and of course the dynamic array needs its reference to be stored in a pointer, but it's not "much more efficient", it's actually less efficient than static arrays, but hey it's just how it works

Link to comment
Share on other sites

Link to post
Share on other sites

another hint:

we have our work to do but we're posting stuff here, so we're not doing our work either

I'm here taking a break from my own C++ work that has to be handed in tomorrow :P

 

actually he's told to use dynamic arrays, not static ones

and of course the dynamic array needs its reference to be stored in a pointer, but it's not "much more efficient", it's actually less efficient than static arrays, but hey it's just how it works

I meant storing the strings as pointers so that they don't have to be moved about and the pointers just need to be moved into a new array - that's the part that's more efficient than moving the whole number of string objects into a new memory location.

I'm not sure what you mean by dynamic array other than something like arraylist which isn't in c++. Isn't the assignment to make a dynamic array? :P

Aragorn (WS): 250D | 6800k | 840 Pro 512GB | Intel 530 480GB  | Asus X99-M WS | 64GB DDR4 | Corsair HX720i | GTX 1070 | Corsair H115i | Philips BDM4350UC 43" 3840x2160 IPS

Gimli (server):  Node 304 | G4560 | ADATA XPG SX8000 128GB | 2x 5TB WD Red | ASROCK H270M-ITX/AC  | 8GB DDR4 | Seasonic 400FL

 Omega (server):                 Fractal Arc Mini R2 | i3 4130 | 500GB Maxtor | 2TB WD Red : Raid 1 | 3TB Seagate Barracuda | 16GB RAM | Seasonic G-450w
Alpha (WS): 900D | 4770k | GTX 780  | 840 Pro 512GB  | GA-Z87X-OC | Corsair RM 850 | 24GB 2400mhz | Samsung S27B970D 2560x1440

                              ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

Link to comment
Share on other sites

Link to post
Share on other sites

Here's your entry point:

std::string *strings = new std::string[integer];

Post some code, if you're afraid of lecturers/teachers googling it, post only the parts you don't get.

I meant storing the strings as pointers so that they don't have to be moved about and the pointers just need to be moved into a new array - that's the part that's more efficient than moving the whole number of string objects into a new memory location.

I'm not sure what you mean by dynamic array other than something like arraylist which isn't in c++. Isn't the assignment to make a dynamic array?  :P

The function should create a new dynamic array one element larger than dynamicArray, copy all the elements from dynamicArray into the new array, add the new string onto the end of the new array,

Specification is specification, he needs to copy elements over. He clearly doesn't need extreme performance at this point, but knowledge instead.

Link to comment
Share on other sites

Link to post
Share on other sites

I'm not sure what you mean by dynamic array other than something like arraylist which isn't in c++. Isn't the assignment to make a dynamic array? :P

 

this is dynamic:

std::string *stringArray = new std::string[integer];

 

this is static:

std::string dynamicArray[]

Link to comment
Share on other sites

Link to post
Share on other sites

Wow, your assignment is pretty straight-forward and tells you exactly how to model the program.  If you're unsure how to do this, I would think your dilemma is caused by a lack of understanding in C++ syntax. 

Link to comment
Share on other sites

Link to post
Share on other sites

this is dynamic:

 

this is static:

Ah I see where you're coming from. Doing it that way would be even easier to manage then :P

 

Thinking about it, i'd probably have switched it around anyway finding that you can't change the size of the static array anyway. 

Ah well, it should save some time next time something like this comes up, and i can remember this :P

Aragorn (WS): 250D | 6800k | 840 Pro 512GB | Intel 530 480GB  | Asus X99-M WS | 64GB DDR4 | Corsair HX720i | GTX 1070 | Corsair H115i | Philips BDM4350UC 43" 3840x2160 IPS

Gimli (server):  Node 304 | G4560 | ADATA XPG SX8000 128GB | 2x 5TB WD Red | ASROCK H270M-ITX/AC  | 8GB DDR4 | Seasonic 400FL

 Omega (server):                 Fractal Arc Mini R2 | i3 4130 | 500GB Maxtor | 2TB WD Red : Raid 1 | 3TB Seagate Barracuda | 16GB RAM | Seasonic G-450w
Alpha (WS): 900D | 4770k | GTX 780  | 840 Pro 512GB  | GA-Z87X-OC | Corsair RM 850 | 24GB 2400mhz | Samsung S27B970D 2560x1440

                              ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

Link to comment
Share on other sites

Link to post
Share on other sites

Wow, your assignment is pretty straight-forward and tells you exactly how to model the program.  If you're unsure how to do this, I would think your dilemma is caused by a lack of understanding in C++ syntax. 

My guess is that it's because it's at a very basic level. I had similar stuff at uni last year. And now ended up with an assignment in the areas of "Make a 2D game, would be nice if it had quest system and inventory"

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×