Printing elements of an array in C I'm trying to print the elements of an array in a fashion that resembles thism where SIZE is a constant:



     0        10        20        30        40        50        60        70        80        90
     1        11        21        31        41        51        61        71        81        91
     2        12        22        32        42        52        62        72        82        92
     3        13        23        33        43        53        63        73        83        93
     4        14        24        34        44        54        64        74        84        94
     5        15        25        35        45        55        65        75        85        95
     6        16        26        36        46        56        66        76        86        96
     7        17        27        37        47        57        67        77        87        97
     8        18        28        38        48        58        68        78        88        98
     9        19        29        39        49        59        69        79        89        99



I have succeeded using this code:

void array_print(int *ptr) {
    for (int i = 0; i &lt; SIZE; i=i+10) {
        printf("%10d", ptr[i]);
    }
    printf("\n");
    for (int i = 1; i &lt; SIZE; i=i+10) {
        printf("%10d", ptr[i]);
    }
    printf("\n");
    for (int i = 2; i &lt; SIZE; i=i+10) {
        printf("%10d", ptr[i]);
    }
    printf("\n");
    for (int i = 3; i &lt; SIZE; i=i+10) {
        printf("%10d", ptr[i]);
    }
    printf("\n");
    for (int i = 4; i &lt; SIZE; i=i+10) {
        printf("%10d", ptr[i]);
    }
    printf("\n");
    for (int i = 5; i &lt; SIZE; i=i+10) {
        printf("%10d", ptr[i]);
    }
    printf("\n");
    for (int i = 6; i &lt; SIZE; i=i+10) {
        printf("%10d", ptr[i]);
    }
    printf("\n");
    for (int i = 7; i &lt; SIZE; i=i+10) {
        printf("%10d", ptr[i]);
    }
    printf("\n");
    for (int i = 8; i &lt; SIZE; i=i+10) {
        printf("%10d", ptr[i]);
    }
    printf("\n");
    for (int i = 9; i &lt;= SIZE; i=i+10) {
        printf("%10d", ptr[i]);
    }
    printf("\n");
}



Is there a better way to code this output?
The simple solution is to add a j loop.

void array_print(const int *ptr) {
  for (int j = 0; j &lt; 10; j++) {
    for (int i = j; i &lt; SIZE; i=i+10) {
      printf("%10d", ptr[i]);
    }
    printf("\n");
  }
}




Also look to a wider than "%10d" to cope with "1234567890" running together as well as 11 character "-1234567890".  Perhaps " %11d"?

Better code would pass in int columns, int rows, int size than hard code them.