title: CS50(2020) PSET 4 Filter: Blur Function not working properly
body: <p>I have re-written this code a couple of times trying to get this to work.  When I read through this code, it looks like it should logically work, but I am still getting errors when I run it through Check50, and all but the corners come back with an error.  I am unsure which part of this is causing the issue, I know that one of these if statements is not adding everything correctly, but they all look logically correct to me.  This one part of the code is really giving me issues.  Thanks in advance for help.</p>
  
  <p><a href="https://i.stack.imgur.com/IKDyA.png" rel="nofollow noreferrer">The 4x4 photo error Check50 gives me</a></p>
  
  <pre><code>void blur(int height, int width, RGBTRIPLE image[height][width])
  {
      //Store copy of original
      RGBTRIPLE Original[height][width];
      for (int i = 0; i &lt; height; i++) //loop through rows
      {
          for (int n = 0; n &lt; width; n++) //loop through columns
          {
              Original[i][n] = image[i][n];
          }
      }
  int counter = 0;
  float newred = 0;
  float newgreen = 0;
  float newblue = 0;
      for (int i = 0; i &lt; height; i++) //loop through rows
      {
          for (int n = 0; n &lt; width; n++) //loop through columns
          {
              if (i &gt;= 0 &amp;&amp; n &gt;= 0)
              {
                  newred = newred + Original[i][n].rgbtRed;
                  newgreen = newgreen + Original[i][n].rgbtGreen;
                  newblue = newblue + Original[i][n].rgbtBlue;
                  counter++;
              }
              if (i &gt;= 0 &amp;&amp; n - 1 &gt;= 0)
              {
                  newred = newred + Original[i][n-1].rgbtRed;
                  newgreen = newgreen + Original[i][n-1].rgbtGreen;
                  newblue = newblue + Original[i][n-1].rgbtBlue;
                  counter++;
              }
              if (i - 1 &gt;= 0 &amp;&amp; n &gt;= 0)
              {
                  newred = newred + Original[i-1][n].rgbtRed;
                  newgreen = newgreen + Original[i-1][n].rgbtGreen;
                  newblue = newblue + Original[i-1][n].rgbtBlue;
                  counter++;
              }
              if (i &gt;= 0  &amp;&amp; n + 1 &lt; width)
              {
                  newred = newred + Original[i][n+1].rgbtRed;
                  newgreen = newgreen + Original[i][n+1].rgbtGreen;
                  newblue = newblue + Original[i][n+1].rgbtBlue;
                  counter++;
              }
              if (i + 1 &lt; height &amp;&amp; n &gt;= 0)
              {
                  newred = newred + Original[i+1][n].rgbtRed;
                  newgreen = newgreen + Original[i+1][n].rgbtGreen;
                  newblue = newblue + Original[i+1][n].rgbtBlue;
                  counter++;
              }
              if (i + 1 &lt; height &amp;&amp; n - 1 &gt;= 0)
              {
                  newred = newred + Original[i+1][n-1].rgbtRed;
                  newgreen = newgreen + Original[i+1][n-1].rgbtGreen;
                  newblue = newblue + Original[i+1][n-1].rgbtBlue;
                  counter++;
              }
              if (i - 1 &gt;= 0 &amp;&amp; n + 1 &lt; width)
              {
                  newred = newred + Original[i-1][n+1].rgbtRed;
                  newgreen = newgreen + Original[i-1][n+1].rgbtGreen;
                  newblue = newblue + Original[i-1][n+1].rgbtBlue;
                  counter++;
              }
              if (i - 1 &gt;= 0 &amp;&amp; n - 1 &gt;= 0)
              {
                  newred = newred + Original[i-1][n-1].rgbtRed;
                  newgreen = newgreen + Original[i-1][n-1].rgbtGreen;
                  newblue = newblue + Original[i-1][n-1].rgbtBlue;
                  counter++;
              }
              if (i + 1 &lt; height &amp;&amp; n + 1 &lt; width)
              {
                  newred = newred + Original[i+1][n+1].rgbtRed;
                  newgreen = newgreen + Original[i+1][n+1].rgbtGreen;
                  newblue = newblue + Original[i+1][n+1].rgbtBlue;
                  counter++;
              }
      image[i][n].rgbtRed = round(newred / (counter * 1.0));
      image[i][n].rgbtGreen = round(newgreen / (counter * 1.0));
      image[i][n].rgbtBlue = round(newblue / (counter * 1.0));
          }
      }
  
      return;
  }
  </code></pre>