# Not sure how to do this, but Im sure you know how

• 05-13-2003
Noobie
test
Ok, lets say I have ten stones. In addition I have one pocket. I could easily write a program which lets me pick up stones, put them in a pocket, and take them out. In addition such a program could keep track of how many stones were in the pocket, and how many weren't.

But lets say I have two pockets, in that case I would have to do:

Code:

```int numberofstonesinpocketone=0; int numberofstonesinpockettwo=0;```
But what if, I had x pockets. It would be easy to write code which lets me change the number of stones in my system, a simple

Code:

`cin>>numberofstones;`
would accomplish that, but what if I don't know the number of pockets? I could do:

Code:

```int numberofstonesinpocketone=0; int numberofstonesinpockettwo=0; int numberofstonesinpocketthree=0; int numberofstonesinpocketfour=0; ... int numberofstonesinpockettenbillionthreehundredthousandandfiftytwo=0; int numberofstonesinpockettenbillionthreehundredthousandandfiftythree=0;```
which although a tedious task, could be done...however, the problem with that method is that I am then left with using excessive memory, if i make my program capable of knowing the contents of 50 pockets then, if I only use 15 pockets, I am wasting memory remembering that 45 pockets aren't being used.

What can I do? I suspect that objects are what I need to use but it seems every tutorial I find doesnt really address any of my questions, always focusing on some other aspect of the concept.

It would be beneficial if a group of users worked together to write tutorials, referring those who ask questions to the tutorials, and improving the tutorials over time.
• 05-13-2003
Noobie
You can do that? neato
• 05-13-2003
Noobie
what if I want the array to have two values? For example, if I have red and blue stones, then I could maybe do

pocket[1][1]=2; meaning that pocket 1 has two blue (1) stones

or

pocket[3][0]=6; meaning that pocket 3 has six red (0) stones
• 05-13-2003
Noobie
Quote:

Originally posted by Salem

stones *pockets = new stones[numpockets];

[/code] [/B]
what does it mean?
• 05-14-2003
CornedBee
It means you're allocating memory for numpockets stones-structures and setting pockets to point to this memory.
• 05-14-2003
Noobie
Now it makes even less sense to me!
• 05-14-2003
HybridM
stones *pockets = new stones[numpockets];

means:
1. use the pointer names "pockets".

2. Allocate memory for an array (called stones) with numpockets elements.

3. make "*pockets" point to the start of the new array.

this allows you to make an array of a specified size, rather than having to know the size before the program starts. A Dynamic Array.
• 05-14-2003
CornedBee
the array is not called "stones", the array elements are of type "stones".
• 05-14-2003
the Wookie
well i think that if he doesnt get arrays yet he wont get pointers

try reading this tutorial
http://www.cprogramming.com/tutorial/lesson8.html
or

cornedbee...same one from VBF?

and salem..
Code:

```int numpockets; cin >> numpockets; int *pockets = new int[numpockets];```
if i do something like that, VC tells me that numpockets need to be a constant..i tried to do that for my example in this thread, it didnt work so i just used vectors
• 05-14-2003
Mister C
I hope he has a book and and many reference sites!!!
• 05-15-2003
the Wookie
that works..weird, perhaps i was declaring it as

int *pockets[]=new pockets[size];

i see now..thanks
• 05-15-2003
CornedBee
Quote:

Originally posted by the Wookie
cornedbee...same one from VBF?
Yeah, same as VBF and Galahtech
• 05-15-2003
the Wookie
ah cool, i dont know if you remember me, but i was nabeels786 over on vbf. its a small internet :P
• 05-15-2003
hk_mp5kpdw
Doesn't anybody ever use map containers on this site? Does anybody see that a map would be an easy way to handle this problem? Either a map<int,int> container for the "one kind of stone in any number of pockets" or a map<int,stones> container for the "different kinds of stones in any number of pockets" problem.

Code:

```map<int,int> pocket; pocket[1] = 5; // Pocket one has 5 stones pocket[2] = 2; // Pocket two has 2 stones```
Or...
Code:

```struct stones {     int red;     int blue; } ... map<int,stones> pocket; pocket[3].blue = 5; // Pocket 3 has 5 blue stones pocket[6].red = 8;  // Pocket 6 has 8 red stones```
No need to worry about pointers and dynamically allocating space for anything, just create the container and start inserting stuff into it.
• 05-16-2003
CornedBee
I would rather use a vector here, it's too simple for a map. I mean, what do you need a tree for just 2 nodes?

Wookie: Yes, I remember.
And I think CornedBee is absolutly unique on the internet.