Jump to content
  • Advertisement
Sign in to follow this  
DeathLetum

Arrays

This topic is 4235 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Quote:
Original post by DeathLetum
So arrays are just a other way of storing Variables

Edit: 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 this post


Link to post
Share on other sites
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 this post


Link to post
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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!