I need some help counting the rows and columns of a two dimensional array. It seems like I can\'t count columns?
#include
int main() {
char res
Use the macros shown in below code to get any dimension size of 1D, 2D or 3D arrays. More macros can be written similarly to get dimensions for 4D arrays and beyond. (i know its too late for Wickerman to have a look but these're for anyone else visiting this page)
// Output of the following program
// [
/*
Demo of the advertised macros :
----------------------------------------------
sizeof(int) = 4
sizeof(Array_1D) = 12
ELEMENTS_IN_1D_ARRAY(Array_1D) = 3
sizeof(Array_2D) = 24
ELEMENTS_IN_2D_ARRAY(Array_2D) = 6
ROWS_IN_2D_ARRAY(Array_2D) = 2
COLUMNS_IN_2D_ARRAY(Array_2D) = 3
sizeof(Array_3D) = 96
ELEMENTS_IN_3D_ARRAY(Array_3D) = 24
MATRICES_IN_3D_ARRAY(Array_3D) = 4
ROWS_IN_3D_ARRAY(Array_3D) = 2
COLUMNS_IN_3D_ARRAY(Array_3D) = 3
Array_3D[][][] Printed :
----------------------------------------------
001 002 003
011 012 013
---------------
101 102 103
111 112 113
---------------
201 202 203
211 212 213
---------------
301 302 303
311 312 313
---------------
Wickerman's problem solved :
----------------------------------------------
sizeof(result) = 70
ELEMENTS_IN_2D_ARRAY(result) = 70
ROWS_IN_2D_ARRAY(result) = 10
COLUMNS_IN_2D_ARRAY(result) = 7
*/
// ]
// ====================================================================================================
// Program follows
// ====================================================================================================
// Array Size Macros
// [
#define ELEMENTS_IN_1D_ARRAY(a1D) ( sizeof( a1D ) / sizeof( a1D[0] )) // Total no. of elements in 1D array
#define ELEMENTS_IN_2D_ARRAY(a2D) ( sizeof( a2D ) / sizeof( a2D[0][0] )) // Total no. of elements in 2D array
#define ROWS_IN_2D_ARRAY(a2D) ( sizeof( a2D ) / sizeof( a2D[0] )) // No. of Rows in a 2D array
#define COLUMNS_IN_2D_ARRAY(a2D) ( sizeof( a2D[0] ) / sizeof( a2D[0][0] )) // No. of Columns in a 2D array
#define ELEMENTS_IN_3D_ARRAY(a3D) ( sizeof( a3D ) / sizeof( a3D[0][0][0] )) // Total no. of elements in 3D array
#define MATRICES_IN_3D_ARRAY(a3D) ( sizeof( a3D ) / sizeof( a3D[0] )) // No. of "Matrices" (aka "Slices"/"Pages") in a 3D array
#define ROWS_IN_3D_ARRAY(a3D) ( sizeof( a3D[0] ) / sizeof( a3D[0][0] )) // No. of Rows in each "Matrix" of a 3D array
#define COLUMNS_IN_3D_ARRAY(a3D) ( sizeof( a3D[0][0] ) / sizeof( a3D[0][0][0] )) // No. of Columns in each "Matrix" of a 3D array
// ]
#define PRINTF_d(s) (printf(#s " = %d\n", (int)(s))) // Macro to print a decimal no. along with its corresponding decimal expression string,
// while avoiding to write the decimal expression twice.
// Demo of the Array Size Macros defined above
// [
main()
{
// Sample array definitions
// [
int Array_1D[3] = {1, 2, 3}; // 1D array
int Array_2D[2][3] = // 2D array
{
{1, 2, 3},
{11, 12, 13}
};
int Array_3D[4][2][3] = // 3D Array
{
{
{1, 2, 3},
{11, 12, 13}
},
{
{101, 102, 103},
{111, 112, 113}
},
{
{201, 202, 203},
{211, 212, 213}
},
{
{301, 302, 303},
{311, 312, 313}
}
};
// ]
// Printing sizes and dimensions of arrays with the advertised Array Size Macros
printf(
"Demo of the advertised macros :\n"
"----------------------------------------------\n");
PRINTF_d(sizeof(int));
PRINTF_d(sizeof(Array_1D));
PRINTF_d(ELEMENTS_IN_1D_ARRAY(Array_1D));
PRINTF_d(sizeof(Array_2D));
PRINTF_d(ELEMENTS_IN_2D_ARRAY(Array_2D));
PRINTF_d(ROWS_IN_2D_ARRAY(Array_2D));
PRINTF_d(COLUMNS_IN_2D_ARRAY(Array_2D));
PRINTF_d(sizeof(Array_3D));
PRINTF_d(ELEMENTS_IN_3D_ARRAY(Array_3D));
PRINTF_d(MATRICES_IN_3D_ARRAY(Array_3D));
PRINTF_d(ROWS_IN_3D_ARRAY(Array_3D));
PRINTF_d(COLUMNS_IN_3D_ARRAY(Array_3D));
// Printing all elements in Array_3D using advertised macros
// [
int x, y, z;
printf(
"\nArray_3D[][][] Printed :\n"
"----------------------------------------------\n");
for(x = 0; x < MATRICES_IN_3D_ARRAY(Array_3D); x++)
{
for(y = 0; y < ROWS_IN_3D_ARRAY(Array_3D); y++)
{
for(z = 0; z < COLUMNS_IN_3D_ARRAY(Array_3D); z++)
printf("%4.3i", Array_3D[x][y][z]);
putchar('\n');
}
printf("---------------\n");
}
// ]
// Applying those macros to solve the originally stated problem by Wickerman
// [
char result[10][7] = {
{'1','X','2','X','2','1','1'},
{'X','1','1','2','2','1','1'},
{'X','1','1','2','2','1','1'},
{'1','X','2','X','2','2','2'},
{'1','X','1','X','1','X','2'},
{'1','X','2','X','2','1','1'},
{'1','X','2','2','1','X','1'},
{'1','X','2','X','2','1','X'},
{'1','1','1','X','2','2','1'},
{'1','X','2','X','2','1','1'}
};
printf(
"\nWickerman's problem solved :\n"
"----------------------------------------------\n");
PRINTF_d(sizeof(result)); // radha_SIZEOF_2D_ARRAY
PRINTF_d(ELEMENTS_IN_2D_ARRAY(result)); // radha_SIZEOF_2D_ARRAY
PRINTF_d(ROWS_IN_2D_ARRAY(result));
PRINTF_d(COLUMNS_IN_2D_ARRAY(result));
// ]
}
// ]