views:

57

answers:

3

I keep getting this weird output from my code everytime I use the 'start without degugging' (ctrl-F5) as opposed to normal 'debug' (F5).

When I try to find the following value of norm_differnece in debug (pressing F5) mode, it gives me the correct answer for norm_difference

normdifference = 1.000000

but in 'start without debugging' (pressing ctrl-f5) the wrong output

normdifference = 1456816083547664100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000

The following is a segment of code which is gives the output Note: X[] = is a array of stored DOUBLE values

for(i=0;i<n;i++){
    sum_difference += (pow((X[i*n]-X[i]),2));
      }
    norm_difference = sqrt(norm_difference);
for(i=0;i<n;i++){
    sum_norm_1 += pow(X[i],2);
      }
    norm_1 = sqrt(norm_1);
  //Take square root of the sum of squares for the row
    printf("normdifference = %f \n norm_1 = %f \n",norm_difference,norm_1);
+1  A: 

You probably haven't initialised sum_difference and sum_norm_1 to zero.

Marcelo Cantos
ok, i went and initialized sum_difference to zero. double sum_difference = 0;Same problem.
Craz
What about `sum_norm_1`? Sorry, I didn't spot that one earlier.
Marcelo Cantos
+1  A: 

Possibly you are reading past the end of your array. Some compilers in debug mode will zeo memory but not in release mode so in debug the bad read gets 0 whilst in release it gets some large number

or as per @Marcelo Cantos your variables were not initialised - in debug they might start at 0

Mark
ok, i went and initialized sum_difference to zero. double sum_difference = 0;Same problem.How can I use debug to check where it is overreading, when it gives me the correct result. Kind of a dilema.
Craz
We need to see more code - including the declaration of X and value of n. Ideally code that we can compile and run
Mark
AH I see what you mean by overreading sorry, i had defined the parameters of my array wrong for the loop. Should have beenX[n+i]-X[i]Thanks Heaps
Craz
yeah, I was about to post the whole code, but its huge since it does a dolittle LU decomposition and calls on multiple functions/files. Thank god, I spent the last hour trying to figure out what I did wrong.
Craz
A: 

It looks like you have a few bugs in your code. I'm guessing it should be:

sum_difference = 0; // <<< FIX
for (i = 0; i < n; i++)
{
    sum_difference += (pow((X[i * n] - X[i]), 2));
}
norm_difference = sqrt(sum_difference); // <<< FIX
sum_norm_1 = 0; // <<< FIX
for (i = 0; i < n; i++)
{
    sum_norm_1 += pow(X[i], 2);
}
norm_1 = sqrt(sum_norm_1); // <<< FIX
//Take square root of the sum of squares for the row
printf("normdifference = %f \n norm_1 = %f \n", norm_difference, norm_1);
Paul R