# Arrays

## Recommended Posts

DeathLetum    127
Alright ive messed around with a programming book Called ( Beginning C++ Game Programming ) and well lets just say i got to the Arrays chapter did it all then got to the end and felt like i didint learn anything from it. So i did the chapter over again and same results. then i went on to find a Tutorial online and well that diidnt really help. So i was wondering if someone would be nice enought to try and explain Arrays to me so i can move on with programming. I was told its a very big part of learning to program so i dont wanna skip it. Any help will be very well apriciated thx

##### Share on other sites
Well I think it would be best for you to tell us what you do understand about arrays first that way we can best help explain the rest, as there are many things arrays can do.

##### Share on other sites
DeathLetum    127
well thats kinda my problem ill do the chapter and then look back at it and be like hmmm i dont know what i just finish doing i dont really understand arrays at all. thats why im having so many probs with it

##### Share on other sites
Vinniee    124
Let me explain this with an example :)

You need 5 variables to store integer values. You can do this with this code:

int variable_1 = 1;int variable_2 = 10;int variable_3 = 100;int variable_4 = 1000;int variable_5 = 10000;

But you can also use arrays to do this:

int variable[5] = { 1, 10, 100, 1000, 10000 };

##### Share on other sites
DeathLetum    127
So arrays are just a other way of storing Variables

Edit: Its just a quicker way to write code to store Variables.

##### Share on other sites
yaroslavd    150
It's also a way of referring to a bunch of variables by the same name. For example, let's say that you want to add up all those variables. You'd have to do something like num1+num2+num3+...+num1000. If you use arrays, you can simply loop over the array because all the numbers can be referred to by num[i].

##### Share on other sites
Sneftel    1788
Quote:
 Original post by DeathLetumSo arrays are just a other way of storing VariablesEdit: Its just a quicker way to write code to store Variables.

No. Vinniee's example is a bad example.

Arrays are a method of structuring data. Consider an employee name database, with ten employees. So you might have an array as follows:

std::string names[10];names[0] = "Joe";names[1] = "Barty";...names[9] = "Li'l Gutman";

Now suppose that you wanted to get an employee number, then print out the associated name:
int num;std::cout << "enter employee id: ";;std::cin >> num;std::cout << "Employee " << num << " has name " << names[num] << ".\n";

Exercise: Try writing that functionality without using arrays.

##### Share on other sites
alway616    122
I myself have and use that same book, I suggest reading through the chapter and taking notes on syntaxes and what they do, it really helped my on the vector and classes chapters.
By the time you are done you should have a better idea what the stuff does so you dont have to wade through paragraphs of cute little examples (which alot of the time don't make much sense)
for example:
cout<<text<<endl;-------------Displays value stored in 'text' then
goes to the next line

dont be afraid to write things down!

##### Share on other sites
DeathLetum    127
ok i used the code that Sneftel put there and it work. now lets say i wanted to search for person by its name how would that work.

##### Share on other sites
Alright I'll do my best then to describe an array and give you an idea of what it is supposed to do, then hopefully you'll be able to figure it out or someone, or when I can get some more time, with more time will explain it to you.

An array is a place where data or objects can be stored. Think of it like a book shelf (I'll use this as my example the whole way through). Each array can be arrays of objects or variables or characters but cannot be a combination of both. for example you can only have bookshelf full of numbers or it be can be full of objects but not both. (For simplicity sake, and lack of remembering how to intialize the other types, I'll use only variable arrays)

int array[]; //This creates a new array (or a bookshelf) called array that holds numbers but is empty but at least has one shelf.

In a array there are nodes which is where the objects/variables (These are like the individual shelves on the book case). Each node is assigned a number from 0 to whatever the size of the array is, each shelf is has a number until you reach the final shelf. This number allows you to help find the object you want on the book shelf.

array [3] //This is equivalent of looking at the thing on the fourth bookshelf, remember the first shelf count starts at 0.

Now you have a understanding of an array we can go a bit farther. You start out with an empty array, however you don't know how big this array is as we have yet to tell the computer so. Right now the array could have 100 nodes to 1000 nodes (it's like having a book case that reaches the sky and you can't count how many shelves it has). We need to give it a size. To do so when you create an array you must tell it how many nodes or shelves you want in it. To do this you simply put the number of nodes you want minus one in the brackets.

int array[10] // This makes an array with 10 nodes (a bookcase with 10 shelves)

Sometimes you already know of things you want in the bookcase and you have already made them or figured them out, same goes for the array. Say you want to put the numbers 1, 2, 3, 4, 5 in the array. When you make the array it should look something like this:

int array[]= array{1,2,3,4,5}; //This creates an array that holds the numbers 1,2,3,4,5.

The bracket represents what you want to put in the array. Each comma represents a new node. So 1 is placed in the 0 Node or the first shelf of the bookcase and 2 is placed on the 1 node or the second bookshelf, etc..

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[];

This creates what is called an empty dynamic array, which if my memory serves me correct has one node and you must tell it to add more nodes. You can always put things in their later. I won't discuss how to do that but maybe someone else will.

To be able to add things to the array that you didn't initially know you wanted you only have to tell it which node you want to add the variable on. However be carefully becuase you can only have one thing on in a node so if you have something in that node it's going to be deleted (Bookshelf explination:You can only have one peice of paper on a shelf of the bookcase with a number on it and to put another one you have to use an erase the old number).

array[4]=2; //Is a way to put 2 on node 5 (putting 2 on self 5).

Now there is a lot more to arrays but that should give you some of the basic understanding of how an array works. Some things I didn't touch on where dynamic arrays, string and object arrays, 2D arrays and 3D arrays but that should give you the basics. Hope this helps.

P.S. I began writing this before the above guys posted. But they are both right as an array is a method used to store data and can be used to make that data storage quicker.

[Edited by - TKE Super Dave on December 11, 2006 3:23:06 PM]

##### Share on other sites
songho    268
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.

##### Share on other sites
Simian Man    1022
Quote:
 Original post by TKE Super Daveint 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 Daveint 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 DaveIf 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.

##### Share on other sites
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.

##### Share on other sites
Simian Man    1022
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[i] );

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]

##### Share on other sites
DeathLetum    127
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.