-2
$\begingroup$

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

$\endgroup$
1
  • 1
    $\begingroup$ There is an index-out-of-bounds error in X[N+2] and Y[M+2], which may explain it. Try compiling with an address sanitizer. $\endgroup$
    – Kirill
    Commented Jul 11, 2016 at 21:32

2 Answers 2

0
$\begingroup$

I ran your code and it is giving correct output, I think. You should check the code in your system.Also there was a syntax error in your code.
check the output I'm getting in the stdout section: https://ideone.com/9siAoO

$\endgroup$
1
  • $\begingroup$ You're right, even when I add my more complicated code it prints the proper values. I appreciate this suggestion. You wouldn't happen to know what could be the cause of this? I can't be coding with a compiler that does stuff like this, my research is in scientific computing and this is a huge waste of time and resources. $\endgroup$
    – user20973
    Commented Jul 11, 2016 at 12:51
1
$\begingroup$

Alright I did some fixing and debugging. Quite a few mistakes I would say, I think you're a beginner in C/Programming ? Anyway find the code below...

#include <math.h>
#include <stdio.h>

#define N 3
#define M 3
#define A 0.0
#define B 1.0
#define C 0.0
#define D 1.0


int main(int argc, char *argv[]){

double h_x = (double) 1/(N+1);
double 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+1] = D;
Y[0] = C; Y[M+1] = D;
xhalf[0] = h_x/2;
yhalf[0] = h_y/2;

// before
printf("\nhx= %f", h_x); printf("\th_y= %f\n", h_y);
printf("\nX[0]= %f", X[0]); printf("\tX[N+1]= %f", X[N+1]);
printf("\nY[0]= %f", Y[0]); printf("\tY[M+1]= %f\n\n", Y[M+1]);

for(i=1; i<=N; i++){
    X[i]= X[i-1]+h_x;
    xhalf[i]=xhalf[i-1]+h_x;
    printf("X[%d] = %e\n", i, X[i]);
}

for(i=1; i<=M; i++){
    Y[i]=Y[i-1]+h_y;
    yhalf[i]=yhalf[i-1]+h_y;
    printf("Y[%d] = %e\n", i, Y[i]);
}

// after
printf("\nhx= %f", h_x); printf("\th_y= %f\n", h_y);
printf("\nX[0]= %f", X[0]); printf("\tX[N+1]= %f", X[N+1]);
printf("\nY[0]= %f", Y[0]); printf("\tY[M+1]= %f\n\n", Y[M+1]);

return 0;
}

First, when you insert a code for us to help you out, please copy-paste it fully working (compilable) -- include libraries.

Second, please paste a working version that actually works and so we dont have to work our way from scratch. In this case why was the opening braces for the main function missing ??

Third, a good practice is not to leave the main function empty like you did, check my code...

Fourth, you were displaying the indexing of your arrays starting from 1, i.e. you skipped the first value and that is why you did not see a zero.

Fifth, you initialize your arrays as (N+2) and (M+2) for X and Y - respectively. However in C you do not access the (N+2)th and (M+2)th index. This is why after compiling and during execution, we got a "stack smashing detected" error. In other words, for example, you initialize:

double array[5];

However, you can only use array[0] up until array[4].

Anything besides that, keep up the good work ! We've all been there :)

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.