Page 1 of 1

Multidimensional Array Tutorial

PostPosted: Mon Jun 06, 2011 9:42 pm
by Hblade
Ever want to understand Arrays better? well now you can! I've decided to make a multidimensional array tutorial out of boredom.

Q1: What are arrays and multidimensional arrays?
    An array is data structure (type of memory layout) that stores a collection of individual values that are of the same... Read More

Alright so lets get started shall we? First, a normal array would look something like this: int array[25];. To put things simply, I guess you could say that the one variable named "array" that we just seen can have 25 different values in it. For example: array[0]=1; array[1]=345; notice how Array 0 and Array 1 both contain different = values. It's kind of hard for me to explain exactly how this works so I'll leave the rest of the basic array up to you to read up on using wikipedia
Code: Select all
http://en.wikipedia.org/wiki/Array


Now lets get started on the multidimensional arrays. A multidimensional array can be used just like a normal array, only you can have more dimensions. For example: int array[255][255]; is a 2D array containing a max ammount of different values of 255 for each dimension. In other words you can use it like this
Code: Select all
array[0][0]=255;
array[0][1]=188;
array[1][0]=248;


You get the point? XD Using multidimensional arrays can have it's advantages. You can add as many dimensions as you like. Example: int array[255][255][255][255][255] This is a 5D array. After you get the hang of it, it's pretty simple to use. Let's try writting something that the player can use. lets make a simple 2D Array (2 dimensions)
Code: Select all
//the first digit represents the actor, the second one represents x,y, etc.. anything else :3
int array[255][255];


now in the draw actor of your player use something like this
Code: Select all
array[0]0]=xmouse;
array[0][1]=ymouse;
xscreen=array[0][0];
yscreen=array[0][1];

This will have the player follow the mouse, now I know most of you would probably just use follow mouse :P But since this is an example on how to get started using multidimensional arrays, we'll just stick with this.

Hope this helped :) Try using arrays for many different things, to store HP, Enemy Count, anything you want to :)

Re: Multidimensional Array Tutorial

PostPosted: Tue Jun 07, 2011 2:57 am
by Clokio
I am not a expert, but I use array, (table in lua language) for creating list.

Ex: I make a list of fruits - vegetables. you store their name, if they are fruit or vegetable. Their color. And from there you can make a game for kids to learn a tomato is a vegetable. Is it a vegetable or a fruit :?:

Maybe we should bring that to the supreme court to decide... lol http://en.wikipedia.org/wiki/Nix_v._Hedden

But I still ask myselve what to do if I need to store more than 256 elements.

Would be nice to know a .ged that have a array.

Re: Multidimensional Array Tutorial

PostPosted: Tue Jun 07, 2011 10:04 pm
by Hblade
Hehehehe :D I'm pretty sure that it's a fruit, judging by the way it's seeds are. A Vegetable has different seed styles, appearently O.o Science is weird, the way I see it lol, it's something that will fill your tummy up when your hungry, I could care not if it's a vegetable or fruit rofl :D

Arrays are so nice to use. And hey, theres an idea for you. Why don't you make a childrens game and market it?

Re: Multidimensional Array Tutorial

PostPosted: Tue Jun 07, 2011 10:16 pm
by Clokio
:roll: :mrgreen:
I'ts in the todo list.

Re: Multidimensional Array Tutorial

PostPosted: Tue Jun 07, 2011 11:42 pm
by Hblade
hahahaha :D Image

Re: Multidimensional Array Tutorial

PostPosted: Wed Jun 08, 2011 12:40 pm
by Jagmaster
Fruit= Any part of the plant that contains seeds.
Fleshy fruits:
Pomes= Apples, pears (contains core)
Drupes= Olives, peaches, plums, (contains stone or pit)
Berries= tomatoes, grapes, (no pit or core) bananas, cucumbers, oranges (Modified berries, with thick skin)
Dry fruits:
Pods= Beans, peanuts (single camber)
Capsules= Poppy seeds (Multiple chambers)
Samara= Maple seeds (Wings)
Nuts= walnuts, acorns (woody shell)
Grain= corn, (seed is connected to ovary wall)
Achene= sunflower seeds (seed is not connected to ovary wall)
Vegetable= Any part of the plant that does not contain seeds.

Off topic. Biological terms have nothing to do with arrays. :lol:

Re: Multidimensional Array Tutorial

PostPosted: Wed Jun 08, 2011 3:19 pm
by Jagmaster
Relevant question: Can you save these arrays with Savevars?

Re: Multidimensional Array Tutorial

PostPosted: Wed Jun 08, 2011 8:29 pm
by Game A Gogo
Nope!

But don't worry, it's still possible to save those variable!
That means using fopen(); and creating files manually, thus using more functions. Though I'm not gonna explain it here! Maybe have a look at my game P2040 in the game demos, but if you don't like script without comments, maybe I wouldn't suggest as much.

Re: Multidimensional Array Tutorial

PostPosted: Thu Jun 09, 2011 6:59 pm
by savvy
@game a gogo: YOU COULD USE YOUR UGLY ARRAY! XD as an example that is...

so, yes, as gag said.. to save these you must make your own save/load functions in void.. gag showed me how to do this, now ill show you as best i can...

Code: Select all
void saveg(char*dir, int lv)
{
FILE*fl = fopen(dir, "w+b");
int i;
for(i=0;i<100;i++)
{
fputc(array[lv][i], fl);
}
fclose(fl);
}
[


this saves 100 parts of the array into a file.

Code: Select all
void loadg(char*dir, int lv)
{
FILE*fl = fopen(dir, "r");
int i;
for(i=0;i<100;i++)
{
array[lv][i]=fgetc(fl);
}
fclose(fl);
}
[


this gets the values of the 100 values of array...

this way it saves and loads whichever [i][] you want and all the [][i] values within. in order to save everything you must add another for loop, perhapse named i, then it would be 'array[j][i] = fgetc(fl);'. causes more lag though.

savvy

Re: Multidimensional Array Tutorial

PostPosted: Thu Jun 09, 2011 7:34 pm
by Hblade
nice

Re: Multidimensional Array Tutorial

PostPosted: Wed Jul 18, 2012 11:46 pm
by AliceXIII
Code: Select all
void saveg(char*dir)
{
FILE*fl = fopen(dir, "w+b");

fwrite(array, sizeof(array), 1, fl); // better to use fwrite();

fclose(fl);
}


Code: Select all
void loadg(char*dir)
{
FILE*fl = fopen(dir, "r");

fread(array, sizeof(array), 1, fl); //same here use fread instead

fclose(fl);
}


fwrite and fread are easier and the obvious bonuses writes and reads an entire array in one line of code and no for loop needed, unless you want to use arrays to store x,y, and animpos of cloned actors for loops are needed then to re-create the cloned actors but for other uses more superior :)

usage:
fwrite(array, sizeof(array), 1, fl);

fwrite and fread both ask for array to write/read, size of array, how many times to write/read array, and the file stream associated with it.

also sizeof() for those of you that don't know it is a c operator that can get the size of any data structures especially useful for saving and loading arrays, or using dynamic memory models ie: using malloc(), realloc(), calloc() these all expect you to know the size of memory you want to allocate so sizeof makes this task easier :)

hope this was helpful!!