Vector Element spontaneously changing?!

This is a discussion on Vector Element spontaneously changing?! within the C Programming forums, part of the General Programming Boards category; Hello,
I am somewhat of a newbie when it comes to programming in C, although I am familiar with Fortran. ...

Vector Element spontaneously changing?!

Hello,

I am somewhat of a newbie when it comes to programming in C, although I am familiar with Fortran. I have been experiencing unrequested changes to certain elements in a vector while using a for-loop. My code for this section is as follows:

Code:

for (i=1;i<NZ;i++) {
a[i]=-eta2; // defining the entries in the tridag matrix
b[i]=eta1; // defining the entries in the tridag matrix
c[i]=-eta2; // defining the entries in the tridag matrix
printf("%f should be %f and not %f \n",b[1],eta1,eta2);
}

where NZ, eta1 and eta2 are read in from a parameter file upon running the compiled program. The program compiles without any errors, but once executed I am presented with the following output:

5.201031 should be 5.201031 and not -2.100515
5.201031 should be 5.201031 and not -2.100515
5.201031 should be 5.201031 and not -2.100515
5.201031 should be 5.201031 and not -2.100515
-2.100515 should be 5.201031 and not -2.100515

with the final line repeating until the counter reaches NZ (in this case, 100). As can be seen from the above output, b[1] seems to change without reason when i=4 from eta1 to -eta2. It's also worth noting that a[1] and c[1] are unchanged throughout this loop.

Does anyone have any ideas as to what might be causing this strange behaviour? A compiler bug (surely not)? I am using gcc 3.3 on Mac OS X 10.3.3.

The mini-program works properly (using the line ` int x, NZ = 6; ` ). However, even with hard-coded values in my larger program the same problems are occurring. For example, using the lines:

Code:

for( i = 1; i < NZ; i++ )
{
a[ i ] = -eta2;
printf("a[%d] is %f and should be %f\n", i, a[ i ], -eta2 );
b[ i ] = eta1;
printf("b[1] is %f and should be %f\n", b[1], eta1 );
printf("b[%d] is %f and should be %f\n", i, b[ i ], eta1 );
c[ i ] = -eta2;
printf("c[%d] is %f and should be %f\n", i, c[ i ], -eta2 );
}

gives

a[1] is -2.100515 and should be -2.100515
b[1] is 5.201031 and should be 5.201031
b[1] is 5.201031 and should be 5.201031
c[1] is -2.100515 and should be -2.100515<SNIP>
a[4] is -2.100515 and should be -2.100515
b[1] is 5.201031 and should be 5.201031
b[4] is 5.201031 and should be 5.201031
c[4] is -2.100515 and should be -2.100515
a[5] is -2.100515 and should be -2.100515
b[1] is -2.100515 and should be 5.201031
b[5] is 5.201031 and should be 5.201031
c[5] is -2.100515 and should be -2.100515

EDIT:I forgot to mention that the last four lines of output repeat, changing with i and b[1] giving the same (incorrect) value, until i reaches NZ-1 (=99)

The very fact that this can happen in a self-contained loop is quite disconcerting. Has anyone ever experienced anything similar to this? Anyway, I will re-write various parts of my code tomorrow and report back in the evening. Thanks to all for their help.

-Matt

Last edited by homgran; 05-29-2004 at 05:37 AM.
Reason: Clarification of output