program exe has stopped working

I am coming across an error I cannot figure out. originally in my compute_averages function I had this code

int i, j;
float avg, count;


for (i = 0; i < (int)n; i++)
{
count = 0.0;
avg = 0.0;

for (j = 0; j < (int)width; j++)
{
count++;
if (i + j < (int)n)
{
avg += data[i + j];
}
else
{
break;
}
}

answer[i] = avg/(width-1);
}

}

It would compile fine, but the run time was far too inefficient. This is what I have now

void compute_averages(float data[], float answer[], size_t n, size_t width)
{
int i;
double sum;

for(i = 0; i < (int)width; ++i)
{
if(i < (int)n)
{
sum += data[i];
}
else
{
sum += 0;
}
}
answer[0] = sum/(width - 1);

for(i = 1; i < (int)n; ++i)
{
sum -= data[i - 1];

sum += data[i + width -1];

answer[i] = sum/(width - 1);
}
}

It compiles fine, but once I load a sound file a box pops up saying it has stopped working. This is for a sound visualizer...the sound file will load fine if I comment it out, the visual effects obviously won't work, but it will load and play the song fine...
Any ideas???
I need more information on this . may be few more detail code .
sum += data[i + width -1]; was the problem. Even though it was within the bounds of n, for some reason it wouldn't work without a restriction if it grew over the length of n. Thanks for taking a look though
Topic archived. No new replies allowed.