2

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.

3
  • 5
    That's a 2D array, not 10D Commented Mar 13, 2018 at 6:01
  • Where's the code to populate the matrix? Commented Mar 13, 2018 at 6:04
  • @JJcopl why do you expect a different output than you are seeing? What are you expecting to see? You might consider defining some constants instead of using sizeof (which doesn't work the way you are expecting). Your current code will break if you change one of the array dimensions to be a different number than the other. Try this: const int dim1 = 10; const int dim2 = 10; int matrix[dim1][dim2]; for (i = 0; i < dim1; i++) { for (j = 0; j < dim2; j++) {...} } Commented Mar 13, 2018 at 6:17

1 Answer 1

4

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).

Sign up to request clarification or add additional context in comments.

1 Comment

Aaaah, thank you for the correction. I didn't test that before and misunderstood how the compiler treated the array vs. a pointer. Thank you! I've learned something too. will edit

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.