[Tutorial] Beginner's Guide: Single/Two/Multi-dimensional Arrays
#1

Beginner's Guide
Single & Multi-dimensional Arrays

(I'm a little free today so I thought about making a tutorial..)

In this tutorial, we'll cover only the basic information and understandings about how arrays work.
Knowledge about "Arrays" is necessary for any scripter. So, let's get started.

Requirements:
a) Some basic ideas about scripting/pawn.
b) Patience & Understanding ability.

Contents:
0. Definition of Array
1. Single Dimensional
2. Two Dimensional (Will post soon)
3. Three Dimensional (Will post soon)

Definition
Array is a structure of data that contains same or different values as a collection, each of them has a unique ID relating to their position in the structure. Easy, right? No, not really for a beginner, just read what's written below, you'll understand better.

1. Single Dimensional
Let's start with an example:
pawn Code:
new MyArray[10];
This will create a structure of 10 elements that looks like this:


Since this is only a "Chain", you can call it "Single Dimensional" or "String".

If we were to give IDs to each of the the elements, the whole chain would be [0] and each of the boxes would be [0]..[9]



Remember, first element will always start with ID "0" not "1"! If you count from 0 to 9, you'll see that there are 10 numbers. So, 0-9 = 10 slots.


As you can see the boxes are empty, but an element cannot be empty, so actually, these are filled with "NULL" characters.
This helps the compiler to understand where the strings end or else it wouldn't be able to to check the length of the string and terminate it.
Now, this is what it looks like:



Note that '\0' is a character constant called NULL.

Now, Let's see how it looks like if we have a word in it:

pawn Code:
new MyArray[10] = "Computers";


Now you see a '\0' at the end. It tells the compiler that the string ends there.

If you would've done:
pawn Code:
new MyArray[10] = "Computerss";
You'll come across:
Code:
error 018: initialization data exceeds declared size
Because there is no space for a NULL character!

You can also do:
pawn Code:
new MyArray[10] = {'C', 'o', 'm', 'p', 'u', 't', 'e', 'r', 's'};
//It's same as new MyArray[10] = "Computers";
To utilize the array:

pawn Code:
new MyArray[10] = "Computers";

printf("%s" /* identifier %s is for strings */, MyArray);

//or:
   
    printf
    (
        "%c%c%c%c%c%c%c%c%c%c", //If you didn't know, identifier '%c' is for characters.
        MyArray[0],
        MyArray[1],
        MyArray[2],
        MyArray[3],
        MyArray[4],
        MyArray[5],
        MyArray[6],
        MyArray[7],
        MyArray[8],
        MyArray[9]
    );

//Both will output:
Computers
pawn Code:
//If you do:
printf("%i" /* %i is for integers (numbers) */, MyArray[0]);

//it will output:
67

Because 67 is the ASCII number for the letter C.
So, for a simple use:
pawn Code:
new message[] = "Hello guys! I'm reading the tutorial here!";
format(message, sizeof message, "She said: %s", message);
print(message);

//Will give you:
She said: Hello guys! I'm reading the tutorial here!
Note: If you declare a string with no size, the precompiler will automatically set the size according to what you have initialized it with.
Example:

pawn Code:
new something[] = "YAY!";
//it becomes:
new something[5] = "YAY!";

2. Two Dimensional
Now, you know a lot more about 'Strings'. A 2D Array is actually String of Strings.

When you declare a new 2D Array:

pawn Code:
new MyArray_2D[7][8];
It looks like:



A 2D Array with content inside:



Some more explanation:

pawn Code:
new MyArray_2D [] [] =
    {
        "One",
        "Two",
        "Three"
    };
   
    printf("%s", MyArray_2D);
//will give you some weird symbols because MyArray_2D is a 2D Array,
//you are supposed to index it with an ID [i].

//So, do it this way:
printf("%s", MyArray_2D[0]);
//gives: "One" because at first index 0, you have a string "One"

printf("%s", MyArray_2D[0][0]);
//Also gives: "One" because in the printf function, you specified it as a "String" using %s,
//it will print characters until it reaches a NULL index.

printf("%c", MyArray_2D[0][0]);
//Gives: "O" because at the position 0, 0 in MyArray_2D, "O" is a character.

printf("%s", MyArray_2D[1]);
//Gives: "Two" and similarly,  MyArray_2D[2] will print "Three"
Some simple examples:
pawn Code:
new NewsHeadlines [4] [128] =
    {
                //[0] ............................................................ [i]
        /* [0] */ "The server was attacked by a group of rougue members this evening!",
        /* [1] */ "A competition held by an admin caused the server to crash!",
        /* [2] */ "Server crashed due to a bad filterscript this evening.",
        /* [3] */ "I have no other news in my head."
    };
   
    SendClientMessageToAll(-1, NewsHeadlines [ random(sizeof NewsHeadlines) ] );
Here, "sizeof NewsHeadlines" will give 4.
Random of 4 will give 0 or 1 or 2 or 3 (0-3 = 4 numbers)

So, NewsHeadlines[0-3] will give the message on ID 0-3.

And since it's a variable, you can changed the headlines!
pawn Code:
format(NewsHeadlines[0], 128, "The headline has been changed!");
    printf("%s", NewsHeadlines[0]);

3. Three Dimensional
Alright, so the harder part, 3D arrays.
As I've told you 2D arrays are string of strings, then 3D Arrays are string of strings of strings. Confusing, eh?

Anyway, I'll show an example:

pawn Code:
new Array_3D [4] [4] [4];
It would look like:


And If we put index numbers (slot ids):


As in scripts:

pawn Code:
new Array_3D_A [3] [5] [12] =
{
    {"Pen",     "Book",     "Paper",  "Notes",  "Pencil"},
    {"Laptop",  "Computer", "Cell",   "Pad",    "Pod"},
    {"Car",     "Bike",     "Bus",    "Train",  "Muscle"}
};

for ( new index = 0; index < sizeof Array_3D_A; index++ )
    {
        printf
        (
            "Objects: %s | %s | %s | %s | %s",
            Array_3D_A[index][0],
            Array_3D_A[index][1],
            Array_3D_A[index][2],
            Array_3D_A[index][3],
            Array_3D_A[index][4]
        );
    }
Will output:
Code:
Objects: Pen | Book | Paper | Notes | Pencil
Objects: Laptop | Computer | Cell | Pad | Pod
Objects: Car | Bike | Bus | Train |
Let's see how we get the index ids:

pawn Code:
new Array_3D_A [3] [5] [12] =
{
    //       [0]        [1]         [2]       [3]       [4]
    /*[0]*/ {"Pen",     "Book",     "Paper",  "Notes",  "Pencil"},
    /*[1]*/ {"Laptop",  "Computer", "Cell",   "Pad",    "Pod"},
    /*[2]*/ {"Car",     "Bike",     "Bus",    "Train",  "Muscle"}
};
As you move down the lines, it's in increasing order of main indexes. (Above, there are 3 columns, 0-2)
As you move Left-to-Right, it's in increasing order of the second indexes. (Above, there are 5 rows, 0-4)
And as you count the characters of each string (word), your going in increasing order of the third indexes.
(Above, there are 12 elements, 0-11 where the strings are stored)


Another example:
pawn Code:
new Array_3D_B [] [] [] =
{
    {float:102.14571,   "******",       4100},
    {float:452.45245,   "*******",      4012},
    {float:124.78951,   "Memebase",     4501}
};

for ( new index = 0; index < sizeof Array_3D_B; index++ )
    {
        printf
        (
            "Data: %f | %s | %i",
            Array_3D_B[index][0],
            Array_3D_B[index][1],
            Array_3D_B[index][2]
        );
    }
Will output:

Code:
Data: 102.145713 | ****** | 4100
Data: 452.452453 | ******* | 4012
Data: 124.789512 | Memebase | 4501
Another one:
pawn Code:
new Array_3D_C [] [] [] =
{
    { {'A', 'C', 'E', '\0'}, 0xFF11FF25, float:4.154,  36},
    { {'B', 'A', 'M', '\0'}, 0xFFFFFFFF, float:6.154,  43},
    { {'Y', 'A', 'Y', '\0'}, 0x0,        float:74.154, 46}
};

for ( new index = 0; index < sizeof Array_3D_C; index++ )
    {
        printf
        (
            "Data: %s | %x | %f | %c",
            Array_3D_C[index][0],
            Array_3D_C[index][1],
            Array_3D_C[index][2],
            Array_3D_C[index][3]
        );
    }
Will output:
Code:
Data: ACE | -EE00DB | 4.153999 | $ | 
Data: BAM | -1 | 6.153999 | + | 
Data: YAY | 0 | 74.153999 | . |
---

That's all for now!
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)