# Why does it say that my index it -1?

I am working on blur in filter and it says my index is our if bound -1

``````        // Create count starts and end count
int sh = -1;
int eh = 1;
int sw = -1;
int ew = 1;

//Modify start for first height
if (h == 0)
{
sh = 0;
}
if (h == height - 1)
{
eh = 0;
}
if (w == 0)
{
sw = 0;
}
if (w == width - 1)
{
ew = 0;
}

// Calculate averige
// Iterate over height
for (int hi = sh; hi < eh; hi++)
{
// Iterate over width
for (int wi = sw; wi < ew; wi++)
{
// Add up the value of the 10 pixels
blue = blue + temp[h - hi][w - wi].rgbtBlue;
green = green + temp[h - hi][w - wi].rgbtGreen;
red = red + temp[h - hi][w - wi].rgbtRed;
}
}
``````

I have tested everything I can think. Is it time hat my approach will not work in general and I need to go back to the drawing board?

~/pset4/filter/ \$ ./filter -b images/tower.bmp t.bmp

helpers.c:185:39: runtime error: index -1 out of bounds for type 'RGBTRIPLE [width]'

helpers.c:186:41: runtime error: index -1 out of bounds for type 'RGBTRIPLE [width]'

helpers.c:187:37: runtime error: index -1 out of bounds for type 'RGBTRIPLE [width]'

helpers.c:185:39: runtime error: index -1 out of bounds for type 'RGBTRIPLE [height][width]'

helpers.c:186:41: runtime error: index -1 out of bounds for type 'RGBTRIPLE [height][width]'

helpers.c:187:37: runtime error: index -1 out of bounds for type 'RGBTRIPLE [height][width]'

• It's saying that sometimes, `h-hi` is -1 and sometimes `w-wi` is -1 and you can't have negative indices into your array. – curiouskiwi Jan 20 '20 at 22:56
• I understand that, what I don't understand is why the array is thing to get index to -1. I wrote if statements to stop it from going in negative when looking for the average. I added more of my code in case that helps – Christopher Holder Jan 20 '20 at 23:01
• What happens when `h` and `w` are both 0 (ie, the very top left pixel)? `ew` is 3, so your wi loop runs 3 times, and you get w-wi is 0, then -1, then -2. – curiouskiwi Jan 20 '20 at 23:26

As pointed out in the comment by curiouskiwi

It's saying that sometimes, h-hi is -1 and sometimes w-wi is -1 and you can't have negative indices into your array.

The mistake was that

``````temp[h - hi][w - wi]
``````

should be

``````temp[h + hi][w + wi]
``````