In this bit of code, the X and Y arrays should be identical but for some reason, that I CANNOT figure out for the life of me, X[0] is always 1 rather than 0. I have tried initializing the whole array to 0, putting X[0]=0 in directly, using a constant, and no matter what it is always one and the problem doesn't occur for the Y array. Any insight would be great. Here is the code:
#define N 3
#define M 3
#define A 0.0
#define B 1.0
#define C 0.0
#define D 1.0
int main()
double h_x;
h_x= (double) 1/(N+1);
double h_y;
h_y= (double) 1/(M+1);
double X[N+2];
double Y[M+2];
double xhalf[N+1];
double yhalf[M+1];
int i,j;
double o,k,l;
X[0]=C; X[N+2]=D; Y[0]=C; Y[M+2]=D; xhalf[0]=h_x/2; yhalf[0]=h_y/2;
for(i=1; i<=N; i++)
{
X[i]= X[i-1]+h_x;
xhalf[i]=xhalf[i-1]+h_x;
printf("%e\n", X[i]);
}
for(i=1; i<=M; i++)
{
Y[i]=Y[i-1]+h_y;
yhalf[i]=yhalf[i-1]+h_y;
printf("%e\n", Y[i]);
}
return 0;
}
with output:
1.25 1.50 1.75 2.5e-1 5.0e-1 7.5e-1
X[N+2]
andY[M+2]
, which may explain it. Try compiling with an address sanitizer. $\endgroup$