简体   繁体   中英

initializing a 2D array (Matrix) in C++

I am trying to create a 2D array by a for loop but I get some garbage random numbers code:

unsigned int i, j;
int matrix[10][10];//={{},{}};

for (i = 0; i < sizeof(matrix[i])/sizeof(int); i++) {
    for (j = 0; j < sizeof(matrix[j])/sizeof(int); j++) {
        cout <<matrix[i][j] << " " << flush;
    }
    cout << endl;
}

and when I change cout part to the following

cout <<matrix[i][j] = {{i},{j}} << " " << flush;

I get an error.

how can I make it? Thank you in advance.

The dimension of an array is how many indices are used to access it, which is different than the size of a dimension (how many elements are in a row of the dimension), which is different than the size of an array or the number of elements in the complete array.

With what you have written, you are writing a 2-dimensional array of 10 rows, each containing 10 elements for a total size of 100 elements.

If you want to do an initializer list in that syntax, you must specify each row and each element in nested braces. You have one top level set of braces for the total array, then a set of braces for each sub-array. You can do this with curly braces enclosing each row, specifying each element as a comma separated list:

unsigned int i, j;
int matrix[10][10] = {{1,2,3,4,5,6,7,8,9,10},
                     {1,2,3,4,5,6,7,8,9,10},
                     {1,2,3,4,5,6,7,8,9,10},
                     {1,2,3,4,5,6,7,8,9,10},
                     {1,2,3,4,5,6,7,8,9,10},
                     {1,2,3,4,5,6,7,8,9,10},
                     {1,2,3,4,5,6,7,8,9,10},
                     {1,2,3,4,5,6,7,8,9,10},
                     {1,2,3,4,5,6,7,8,9,10},
                     {1,2,3,4,5,6,7,8,9,10}};

EDITED: The expression sizeof(matrix[i]) gives the size of the array as 40 because the compiler is able to figure that out at compile time, but it can be risky to rely on this since if you change it to a pointer to an array or the array is passed as a parameter to a function, it will give the size of the pointer instead of the array (as explained here: How do I determine the size of my array in C? ). It would be better to display the array like this with the known length:

for (i = 0; i < 10; i++) {
    for (j = 0; j < 10; j++) {
        cout <<matrix[i][j] << " " << flush;
    }
    cout << endl;
}

Usually, for safety and convenience in changing, you can define the size using a macro like this:

#define ROWS 10
#define COLS 10
int matrix[ROWS][COLS] = ...;

then

for (i = 0; i < ROWS; i++) {
    for (j = 0; j < COLS; j++) {
        cout <<matrix[i][j] << " " << flush;
    }
    cout << endl;
}

This way, if you decide to change the size of the matrix in the future, you only change the number in one place, the macro definition, instead of hunting through the code looking for each individual constant (especially with duplicate constants of 10, you might miss one or change an extra one that meant something else, which would cause problems somewhere else).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM