Debugging question

Im sort of stuck on this part of my code. Any help would be appreciated.

1
2
3
4
5
6
float* ThreeValueMovingAverage(float* numbers, int count) {
       average = new float[count];
       for(int i = 0; i < count; i++)
       {
               average[i] = (numbers[i-l] + numbers[i] + numbers[i+1]) / 3.0f;}
}
1
2
3
4
5
6
7
float* ThreeValueMovingAverage(float* numbers, int count) {
       average = new float[count];
       for(int i = 1; i < count-1; i++) {
               average[i] = (numbers[i-l] + numbers[i] + numbers[i+1]) / 3.0f;
       }
       return average; //missed that in your code
}


The problem is on the edge cases of the loop. When i = 0, part of your statement is numbers[i-1], which will be numbers[-1]. What I posted will make it work, but probably not in the way you want. You need to add some other behavior on the edge cases (i = 0, i = [the last element]).
Last edited on
Topic archived. No new replies allowed.