title: Is there a way to design this code as loops?
body: <p>I am writing a blurring function, by taking the average of the rgb values of up to 9 pixels (8 pixels plus [i][j]). The if statements ensure that the program does not try to add the pixel to the average if it goes outside the bounds of the 2-D array. The code looks like it's kind of repeating, but then the if statements are not really repeating? Like I could loop inside a loop to make the height go from i - 1 to i + 1, and width from j - 1 to j + 1, but depending on which pixel is being added a different if statement is required. Is there a way to design this better? Thank you. </p>
  
  <pre><code>int red = copy[i][j].rgbtRed; //input pixel itself into average. array format copy[height][width].rgbtRed
  int n = 1;
  
  if (i != 0 &amp;&amp; j != 0)   //input upper left pixel into average
  {
      red += copy[i - 1][j - 1].rgbtRed;
      n++;
  }
  if (i != 0) //pixel directly above
  {
      red += copy[i - 1][j].rgbtRed;
      n++;
  }
  if (i != 0 &amp;&amp; j != width - 1) // pixel to top right
  {
      red += copy[i - 1][j + 1].rgbtRed;
      n++;
  }
  if (j != 0) // pixel to left
  {
      red += copy[i][j - 1].rgbtRed;
      n++;
  }
  
  // and so on and so forth for all the remaining pixels surrounding [i][j]
  
  float redf = (float) red / (float) n; //take average
  
  </code></pre>