In the first example:
i
will get the values 0, 1, 2, ..., M-1
j
will get the values 0, 1, 2, ..., N-1
So sum
is calculated as
sum = a[0][0] + a[0][1] + a[0][2] + ... + a[0][N-1] +
a[1][0] + a[1][1] + a[1][2] + ... + a[1][N-1] +
a[2][0] + a[2][1] + a[2][2] + ... + a[2][N-1] +
...
...
a[M-1][0] + a[M-1][1] + a[M-1][2] + ... + a[M-1][N-1]
In the second example this has been switched so that
i
will get the values 0, 1, 2, ..., N-1
j
will get the values 0, 1, 2, ..., M-1
so now
sum = a[0][0] + a[0][1] + a[0][2] + ... + a[0][M-1] +
a[1][0] + a[1][1] + a[1][2] + ... + a[1][M-1] +
a[2][0] + a[2][1] + a[2][2] + ... + a[2][M-1] +
...
...
a[N-1][0] + a[N-1][1] + a[N-1][2] + ... + a[N-1][M-1]
Notice that the second version is wrong because the argument is int a[M][N]
, i.e. legal first index is 0..M-1
and legal second index is 0..N-1
In other words, if N and M differs the second version access the array out of bounds.
To make the second example correct. This line sum+=a[i][j];
should be sum+=a[j][i];
so that sum
is now:
sum = a[0][0] + a[1][0] + a[2][0] + ... + a[M-1][0] +
a[0][1] + a[1][1] + a[2][1] + ... + a[M-1][1] +
a[0][2] + a[1][2] + a[2][2] + ... + a[M-1][2] +
...
...
a[0][N-1] + a[1][N-1] + a[2][N-1] + ... + a[M-1][N-1]
With that change the two version are functionally identical, i.e. produce the same result. They only differ in the order that the elements are added.
Due to the memory layout of a 2D arrays and the way cache system works, the first version may perform better than the second. On the other hand, the compiler may optimize the two versions to perform equally.