I need to declare a 2d array to represent the size of chess-board. However, I'm having a trouble understanding how would I actually calculate the width and the length of the board. I would like to know how could I calculate size of the rows and columns of my 2d array
Say, int boardSize[5][5]
?
int main()
{
int boardSize[5][5];
int boardWidth=?
int boardHeight =?
createBoard(boardWidth, boardHeight);
}
int createBoard(int width, int height)
{
// code that actually creates board //
}
Sorry, for not being specific in the begging. So, here I need to calculate boardwidth and boardheight variables? How do I do that from the declared array above. Thank you!
Here i and j are the size of the two dimensions, i.e I denote the number of rows while j denotes the number of columns. Example: int A[10][20]; Here we declare a two-dimensional array in C, named A which has 10 rows and 20 columns.
2D Arrays (Day 1) Arrays in Java can store many items of the same type. You can even store items in two-dimensional (2D) arrays which are arrays that have both rows and columns.
We use arrayname. length to determine the number of rows in a 2D array because the length of a 2D array is equal to the number of rows it has. The number of columns may vary row to row, which is why the number of rows is used as the length of the 2D array.
Java specifies arrays similar to that of a "row major" configuration, meaning that it indexes rows first. This is because a 2D array is an "array of arrays".
boardSize[0]
gives you the first row of the matrix, boardSize[0][0]
the first of its elements. So the quantities you are looking for are sizeof boardSize/ sizeof boardSize[0]
and sizeof boardSize[0]/ sizeof boardSize[0][0]
.
BTW: use size_t
as a type for sizes, not int
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With