when to use arrays???

Started by
4 comments, last by Mikey21 21 years, 8 months ago
Okay, I know what arrays are and how to declare, define, and access them. What the book doesn''t do is give me an example of when to use one. Could someone tell me exactly what arrays are used for. I know they are a very important and common data structure, I just don''t know when I would use one. (I know of the use "char blah[20]" for reading word input, but that''s it)
Advertisement
Array''s are incredibly useful structures. A good example is an array of vertices for a terrain or model. And then you could have another array of index values so you know which vertices to use when. And which texture coordinates to use on what. Also most input systems I''ve seen use an array of booleans (or something like that) to represent all the keys on the keyboard. When a key is pressed, you check the array and see which keys are true (ie getting pushed down). But afaik, these kinds of arrays (well, the vertex/index example anyway) have to be allocated using malloc (or new if you use C++). That starts getting into pointers, which is also an incredibly useful device.

char *blah;
blah = (char*) malloc(sizeof(char) * 20);

And there you have effectively an array of characters with 20 elements which can be accessed exactly as you would a normal "char blah[20]". Okay, I''m done.

--Buzzy
(formerly buzzy_b)
if you had 10 numbers
you can store it like this

int x1,x2,x3,x4,x5,x6,x7,x8,x9,x10;

if you had 10000 numbers, how would you store it?? an array

int x[10000];



Its my duty, to please that booty ! - John Shaft
You use arrays to store items that relate to each other in some way. One example is a chess board. You can declare chess board array like int board[8][8]. Then you have a 2 dimensional array to access any square.

Also, here is the C++ way to allocate an array:

char *blah;
blah=new char[20];

When you are done with it, all you do is:

delete[] blah;

---
Make it work.
Make it fast.

"I’m happy to share what I can, because I’m in it for the love of programming. The Ferraris are just gravy, honest!" --John Carmack: Forward to Graphics Programming Black Book
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]
my personal way is to think of arrays as database tables. if you look into how to normalize databases it should teach you how to divide your data into different arrays.
ie.

you have an array of all the different weapons with their personal stats

your player array would then have references to which weapons they have

(visual basic code)
public type Player
currentVector as D3dvector ''current position
previousVector as d3dvector
weapons() as integer ''an array with listing references to what weapons(x) the player has
currentWeapon as integer ''currently selected players(i).weapons(x)
end type

Dim players() as player

when your normalized you can add more easily. if you hardcoded your player to have a possible 3 weapons youd have to change your array structure to support 4 weapons. this way you also keep duplicate data across arrays to a minimum.
As I see it, arrays have two major benefits over normal variables.

1. You can use one variable to store LOTS of data. This keeps the code much easier to read.

2. You can iterate through an array. Imagine if you had a space invaders game. Somewhere in the main loop, you are going to need to go through each ship and update it's positions, AI, ect. This is next to impossible once a game gets at all complex.

Using normal variables:

  //we're assuming we have a ship structure set upship Ship1;ship Ship2;ship Ship3;ship Ship4;while(GameRunning){if(Ship1.xpos < 0)//bla blaif(Ship2.xpos < 0)//bla blaif(Ship3.xpos < 0)//bla blaif(Ship4.xpos < 0)//bla bla}  


Using Arrays:

  ship Ships[5000];while(GameRunning){int i;for(i = 0, i < 5000, i += 1){if(Ships[i].xpos < 0)//bla bla}}  


Did you see that? I just managed 5000 ships in the second example with less code than I managed 4 ships in the first. Of course, managing ships isn't always this simple, in fact it rarely is, but that should give you the idea.

There is an even better way to do it, using linked lists, but that is WAY beyond the scope of this post.

[edited by - Gwahir the Windlord on August 9, 2002 3:22:38 PM]
Big plans... Small games.

This topic is closed to new replies.

Advertisement