Tuesday, July 14, 2009

Making an array in C++?

How do you make an array without pre-determining the amount of information entered?


My program needs to ask the user "Enter another integer? Y/N"...


But, i need to create an array for the set of numbers the user enters. I may not ask the user how many numbers they want to enter in the beginning, though.


Thanks!

Making an array in C++?
You have to dynamically allocate the array using pointers. My question to you, do you have to use an array or could you use a linked list? A linked list would be a much more efficent method at handling this issue.





Dynamically creating an array would required that you create an integer pointer and then assign the pointer to a new array.





int * myArray;


myArray = new int[count];





IF the array needs to resized (adding another integer) then you need to delete the array, hence freeing the space, then you need to create a new array to the pointer with count+1.





Delete [] myArray;


myArray = new int[count+1];





It has been FOREVER since I have done C++, so anyone feel free to correct me on this.
Reply:You can make a ADT (abstact data type) called a linked list. Funnily enough, these objects work more efficiently for insert operations and delete operations than regular arrays.





struct node


{ yourDataType data;


node *next;


};





node *start_ptr = NULL;





I'd write a nice concrete class and use it forever. Just look up linked list on the net.
Reply:Yes this is the most horrendous thing about C++ but its true ---





This feature isn't available in C++


you would have tried something like this --%26gt; int arr[ ][ ];


But it didn't work !!!! Right ????


So what u've done - predetermining the value by asking the user - is the only alternative left.
Reply:Use a linked list (such as the vector or list template classes in STL).





#include %26lt;vector%26gt;


#include %26lt;iostream%26gt;


using std::vector;


using std::cout;


using std::endl;





...





vector%26lt;int%26gt; myVec;


while( ...user answers yes...) {


int answer = ...get answer from user...


myVec.push_back(answer);


}





Now you can access myVec as if it were an array:





for(int i=0, j=myVec.size(); i%26lt;j; i++) {


cout %26lt;%26lt; "The " %26lt;%26lt; i %26lt;%26lt; "th entry is " %26lt;%26lt; myVec[i] %26lt;%26lt; endl;


}





If for some reason you MUST have an actuall array and not a list, you could create one at this point and copy the contents of myVec into it.
Reply:You need to have a temporary variable to read in the integer.


You also need to either allocate the array dynamically or


use one of the STL library list. If you use the allocate


array yourself than you need to reallocate everytime you get a new number.


Method 1) after you read in a number


++arraySize;


arrayPtr = new int [arraySize];


// now copy the old numbers into the new array





Method 2) search std::list this is a dynamically allocate list you don't have to readjust your list every time
Reply:int myarray [ ]


No comments:

Post a Comment