Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

2d array rows/columns length C

Tags:

arrays

c

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!

like image 957
Jack Morton Avatar asked Jul 04 '12 13:07

Jack Morton


People also ask

What is the size of 2D array in C?

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.

Is a 2D array row column or column row?

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.

How do you determine the size of a 2D array?

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.

Are rows or columns first in 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".


1 Answers

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.

like image 136
Jens Gustedt Avatar answered Nov 14 '22 23:11

Jens Gustedt