Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,582 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,934 people online right now. Registration is fast and FREE... Join Now!




Dynamic Array

 
Reply to this topicStart new topic

Dynamic Array, How to implement a vector using dynamic arrays in C++.

trixt.er
5 Oct, 2008 - 11:52 AM
Post #1

New D.I.C Head
*

Joined: 28 Sep, 2008
Posts: 19



Thanked: 1 times
My Contributions
[font=Courier New]A couple of weeks ago I posted some questions on how to add indexes to a dynamic array. This would treat the dynamic array as if it were a modified vector. Ah the wonderfulness of dynamic memory. My concern is how to add an index to your vector like dynamic array. For example just like a vector I can declare a dynamic array with a maximum number of indexes. As the user adds values to the dynamic array it draws nearer to running out of space. So for the users sake I would like your ideas on how to add an index one at a time to a dynamic array. There is obviously more than one way to do this but what is the most efficient way?

Here is an example of a way users could enter values one at a time into a dynamic array (a.k.a. vector). Only problem is what if the user wishes to add more than 50 indexes?
CODE

setClass::setClass( )//Default constructor.
{
    size = 0;
    set = new int[50];
}

void setClass::addElement(int value)
{
       set[size] = value;
       size++;//Function works fine until size exceeds 50!
}


Please adivse.
User is offlineProfile CardPM
+Quote Post

Psionics
RE: Dynamic Array
5 Oct, 2008 - 12:28 PM
Post #2

D.I.C Head
Group Icon

Joined: 6 Sep, 2008
Posts: 122



Thanked: 2 times
Dream Kudos: 100
My Contributions
Well if you're wanted to add one more item to the top of the list at a time, a linked list would be my recommendation. However, if you're unsure on what size you'd like to make your new array, you could try something like this in a function:

cpp

// Step 1: Create really big temp array
const int MaxLen = 100;
char tempString[ MaxLen ];

// Step 2: Get user input
cout << "Enter your name:";
cin.get( tempString, MaxLen );

// Step 3: Length of string and make a smaller one
// Allocate array of exact size
int len = (int)strlen( tempString ) + 1;
char* pDynamic = new char[ len ];

// Step 4: Copy the string
strcpy_s( pDynamic, len, tempString );

//NOTE: delete the string LATER
return pDynamic;



But if you're dealing with vectors, chances are you'll want to use a linked list, and that's something you can google, or I believe we have a tutorial on them smile.gif they can be quite tricky to the un-initiated

Hope this helps smile.gif
User is offlineProfile CardPM
+Quote Post

trixt.er
RE: Dynamic Array
5 Oct, 2008 - 01:21 PM
Post #3

New D.I.C Head
*

Joined: 28 Sep, 2008
Posts: 19



Thanked: 1 times
My Contributions
The problem that I am facing is in the actual declaration of the dynamic array. Can I not create a dynamic array with only one index and then create a function called addElement that adds a single element to the dynamic array?
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Dynamic Array
5 Oct, 2008 - 03:11 PM
Post #4

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,031



Thanked: 105 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
I fail to see how this questions is fundamentally different from this one. If the question hasn't changed, the answers aren't likely to either.

User is offlineProfile CardPM
+Quote Post

trixt.er
RE: Dynamic Array
5 Oct, 2008 - 09:37 PM
Post #5

New D.I.C Head
*

Joined: 28 Sep, 2008
Posts: 19



Thanked: 1 times
My Contributions
I fail to see why my question can not get answered.

CODE

// Step 1: Create really big temp array  
const int MaxLen = 100;  
char tempString[ MaxLen ];  

This for example will only allow a user to enter in a hundred values. Not say 101 or 164.
I hope I am getting through. How do you add one element to a dynamic array. The memory obviously must be allocated from the heap. Does that mean the array needs to be copied over to a bigger dynamic array and then deleted every time the program needs to add an extra element? smile.gif

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/3/08 12:37AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month