In article <news:1b**************************@posting.google. com>
mma <mm****@foobox.net> writes:
I have been using the lubksb routine in Visual C++ 6.0 and noticed
what looks like an error to me. The last section of the method looks
like this:
for(i=n;i>=1;i--)
{
sum=b[i];
for(j=i+1;j<=n;j++)
sum -= a[i][j]*b[j];
b[i]=sum/a[i][i];
}
Where a is an n X n matrix (C array of arrays) which go from 1..n
(through the use of Numerical Recipes offset index method).
Be aware that this method is not strictly portable, although it
works in practice on most if not all systems. For more detail,
see the FAQ.
It appears to me that the inner loop will cause a segmentation fault
because j=i+1 = n+1 which is greater than n--the max index for both a
and b. Yet it seems to work--am I missing something?
When i==n, the inner loop does this:
for (j = i + 1;
- This sets j to i + 1, which is also n + 1, as you note.
j <= n;
- Now compare j to n. Since j = n + 1, j > n, and j <= n is false
(produces the integer value 0 in an expression, and is treated
as false in a loop-controlling-expression). The inner loop
therefore does not run at all.
After the inner loop (immediately) terminates, the outer loop sets
b[i] to sum / a[i][i]. The variable sum is unmodified from the
original b[i] assigned to it, so this divides b[i] by a[i][i].
The outer loop then decrements i and resumes at its test expression
(which must have already been true exactly once).
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it
http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.