Raised This Month: $ Target: $400
 0% 

Solved Why 33+32?


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 01-03-2021 , 05:22   Re: Why 33+32?
Reply With Quote #4

Quote:
Originally Posted by nG_getwreck View Post
new adminnames[33][32]
this matrix is created with 33 rows and 32 columns to store names. Since max players are 32, the column 32 represents each player’s name. Why need 33 rows and why need 2D array?
Bugsy already explained why we use 33 instead of 32 so I'm going to explain why you need a 2D array.

For a moment, let's assume you are building an XP system and you want to store how many points each player has. For this you would use an array like this:
PHP Code:
new playerPoints[33
Each cell in this array is able to store a 4 bytes integer, meaning you can do things like:
PHP Code:
//give 3200 points to the player with index "id".
playerPoints[id] = 3200 
Now, imagine you also want to store rank(where each rank is a string like "Rank1", "Rank2"). What happens if you try the following:
PHP Code:
new playerRank[33]
playerRank[id] = "Rank1" 
Remember I said each cell can hold 4 bytes of data. How many bytes do you need in order to store "Rank1"? In pawn, a string is an array of characters. "Rank1" contains 5 characters, so 5 characters * 4 bytes = 20 bytes.
Oops, your playerRank array can only hold 4 bytes in each cell, but your strings have 20 bytes, you need more space!

The solution is to use a 2D array or a matrix.
PHP Code:
new playerRank[33][5
Intuitively, what happens here is now each slot in the array(0, 1, ..., 32) is made up of 5 cells instead of only one. Therefore, each slot can now hold 5 cells * 4 bytes each cell = 20 bytes(just perfect for your rank strings).

To visualize this:
When you do playerRank[33] you end up with something like this:
Code:
playerRank[0] -> [3200]
playerRank[1] -> [1500]
...
playerRank[32] -> []
([] means a space where you can put a value)
When you do playerRank[33][5] you have something like:
Code:
playerRank[0] -> [][][][][]
playerRank[1] -> [R][a][n][k][2]
...
playerRank[32] -> [][][][][]
Someone who knows C may notice I'm dancing around the notion of "array of pointers to arrays". But if you do know C, I believe this is the best way to understand this.
View the 2D array as an array where each slot is a pointer(or link) to an array of size 5.

Another way is to view the matrix in one line(called a linearised matrix):
Code:
playerRank[0][0] playerRank[0][1] ... playerRank[0][4] | playerRank[1][0] playerRank[1][1] ... playerRank[1][4] | ... | playerRank[32][0] playerRank[32][1] ... playerRank[32][4]
first row                                                            second row                                                            last row
(each row has 5 slots and can store a string of 5 characters or less).

If you did:
PHP Code:
copy(playerRank[0], "Rank1")
copy(playerRank[1], "Rank2")

//which is the same as:
playerRank[0][0] = 'R'
playerRank[0][1] = 'a'
playerRank[0][2] = 'n'
playerRank[0][3] = 'k'
playerRank[0][4] = '1'

playerRank[1][0] = 'R'
playerRank[1][1] = 'a'
playerRank[1][2] = 'n'
playerRank[1][3] = 'k'
playerRank[1][4] = '2' 
The memory would look like:
Code:
'R' 'a' 'n' 'k' '1' | 'R' 'a' 'n' 'k' '2' | ... | playerRank[32][0] playerRank[32][1] ... playerRank[32][4]
first row               second row                                                            last row
For your original example, replace 5 with 32.
__________________

Last edited by HamletEagle; 01-03-2021 at 05:31.
HamletEagle is online now
 



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 14:12.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode