You normally study what happens as you increase the number of elements.
If that's the case here, the code you posted is O(N), not O(N2).
If the grid has 100 elements, the number of times printf
will be called will be proportional to 100 (O(N)), not proportional to 10,000 (O(N2)).
If on the other handle you were studying what happens as you increase the number of rows or columns, visiting all the elements will be at least O(R*C) since you have R*C elements.
Note that you can flatten the two loops, but it doesn't change the complexity in the least:
void ShowGrid()
{
const int H = grid.height;
const int W = grid.width;
int n;
for (n = 0; n < H*W; ++n)
{
int y = n / W;
int x = n % W;
printf("%d", arr2_get(&grid, x, y));
printf("\n") if x == W-1;
}
}