On Thu, 28 Apr 2011 08:02:40 -0400, bearophile <[email protected]>
wrote:
A little quiz. This is related to a recent post of mine in the main D
newsgroup, but please don't take a look at that post yet. This is the
original function:
double[][] matgen(int n) {
double[][] a;
double tmp = 1.0 / n / n;
a.length = n;
for (int i = 0; i < n; ++i) a[i].length = n;
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
a[i][j] = tmp * (i - j) * (i + j);
return a;
}
Second "improved" version:
double[][] matgen(int n) {
double tmp = 1.0 / n / n;
auto a = new double[][](n, n);
foreach (i, row; a)
foreach (j, ref x; row)
x = tmp * (i - j) * (i + j);
return a;
}
But the second nicer version has a bug, do you see it? :-)
I read the other answer, I thought it was because the indexing is
different, but that's only on initialization. So I was wrong :)
-Steve