# Thread: computing the sum within a loop

1. ## computing the sum within a loop

Hi, a section of my code is as follows:

insert
Code:
```for (i = 0; i < N; i++){

result[0] = distance(myPoints[i], myPoints[(i+1)%N]);

printf("first distance:%f\n", result[0]);

for (j = 0; j < N; j++){

if (i == j)
continue;

dis[i] = distance(myPoints[i], myPoints[j]);
printf("%d %d\n", i, j);
printf("%f\n", dis[i]);

if (result[0] > dis[i])

result[0] = dis[i];

}
if(result[0] != 0) {entropyxy[i] += (log(2*result[0]));
}

printf("shortest: %f\n", result[0]);

}```
my formula for entropy is : entropyxy = SUM(from i=1 to N) log(2*result(i))

However, my entropyxy doesn't seem to increment as it stands in my code. It only computes log(2*result[0]) but doesn't add this onto the previous value of the entropyxy i.e. entropyxy[i-1]. any help on how to solve this?

N.B result[0] changes for each value of i. (it is the shortest distance to a point)

thanks

2. Originally Posted by zesty
my entropyxy doesn't seem to increment as it stands in my code. It only computes log(2*result[0]) but doesn't add this onto the previous value of the entropyxy i.e. entropyxy[i-1]. any help on how to solve this?