Arrays

Started by
13 comments, last by DeathLetum 17 years, 4 months ago
DeathLetum,
I would say that Array is just a container to store multiple data in it. You can access each entry using subscript operator, [].

Array has 3 attributes:
1. Name: It has a unique name to represent the array. Each element in the array can be represented with [] and zero-based index. for example, the first element in the array is myArray[0].

2. Data Type: Array stores only single data type. You cannot store different data types in an array.

3. Size: The number of elements in an array. Once the array size is specified, it cannot be changed.
Advertisement
Quote:Original post by TKE Super Dave
int array[]; //This creates a new array (or a bookshelf) called array that holds numbers but is empty.

No that is an not valid syntax. An array can never really be empty. It just contains unintialized objects. You must specify a size (for dynamic arrays, you use the pointer syntax with new [smile]).

Quote:Original post by TKE Super Dave
int array[10] // This makes an array with 11 nodes (a bookcase with 11 shelves)

No, there are 10 elements in the array (0 through 9), not 11.

Quote:Original post by TKE Super Dave
If you don't know how big you want the book case to be then you leave the brackets blank like the first code example:

int array[];

No that is invalid syntax. Besides dynamic arrays are beyond what hes asking and will not help him.
You are right Simian Man. My knowledge of C++ is rusty as I've been working in Java for a while now, so I've forgotten the syntax. As for the 0-9 thing that was me just being stupid.

I fixed the 0-9 thing and reworded the comment of the array that doesn't have a size.
TKE Super Dave
Say we are making a game and want 10 enemies on the screen. We *could* do it like this:
Enemy e0;Enemy e1;Enemy e2;...Enemy e9;// draw themdraw(e0);draw(e1);...draw(e9);


With arrays we have the following code:
const int MAX_ENEMIES = 10;Enemy e[MAX_ENEMIES];for( int i=0; i<MAX_ENEMIES; i++ )    draw( e );


Using arrays have more benefits than just brevity:
  • The enemies can now be treated as a single entity. We don't have ten disparate enemies; we have one array of enemies! This also means that all ten can be passed to a function as one parameter.

  • This scales better. Making ten enemy variables is not undoable, but consider doing 1000!

  • We can change the number of enemies easily. To increase the number of enemies to 20, we just have to change MAX_ENEMIES to 20. Imagine going though all of your code and adding the extra enemy variables!

  • They are now stored contiguously in memory. Arrays store their elements in order in memory. This does not matter too much on its own, but it means we can use all of the STL algorithms on our array. If you don't know about the STL yet, it basically means we can sort, search and do other stuff with our enemies without having to write the code ourselves!


Hope that makes some sense [smile]
Alright So to what i can understand from a game view i could use arrays to put all my monsters in one spot

So all my Monsters would be set together then i could just use the arrays to go find the one i wanna use.

This topic is closed to new replies.

Advertisement