Search code examples
arrayscloopsmultidimensional-arraymultiple-columns

How to add values to the first row and the first column of two dimensional array in C?


I am trying to add numbers from 0 to 9 in the row and 0 to 11 in column of a two dimensional array in C. And as for the rest of the empty spaces, I would like to add 0.

The matrix size is 9 x 11. And this is what the output looks like with the empty blocks filled with 0: enter image description here

And this is the code I have so far but it does not work:

int i;
int j;
int arr[i][j];

int value = 0;
for (i = 0; i < 9; i++){
    for (j = 0; j < 11; j++){
        arr[i][j] = value;
        printf("%d\n", arr[i][j]);
        value++;
    }
    printf("\n");
}

Solution

  • The screenshot that you've posted has 10 rows and 12 columns, so assuming that, here is the code:

    int i;
    int j;
    int arr[10][12];
    
    for (i = 0; i < 10; i++) {
        for (j = 0; j < 12; j++) {
            if (i == 0) {
                arr[0][j] = j;
            } else if (j == 0) {
                arr[i][0] = i;
            } else {
                arr[i][j] = 0;
            }
    
            printf("%d", arr[i][j]);
          }
          printf("\n");
    }