title: CS50 pset4 - filter (less comfortable), blur function
body: <p>I wrote a code for the blur function of the filter pset. However, whenever I try to compile the code, I get an error. Can someone see where my mistake lies?</p>
  
  <p>Code:</p>
  
  <pre><code>
  // Blur image
  void blur(int height, int width, RGBTRIPLE image[height][width])
  {
      int count = 0;
  
      for (int i = 0; i &lt; height; i++)
      {
          for (int j = 0; j &lt; width; j++)
          {
              int red = 0;
              int green = 0;
              int blue = 0;
  
              if (j - 1 &gt;= 0 &amp;&amp; j - 1  &lt;= 255)
              {
                  red += image[i][j - 1].rgbtRed;
                  green += image[i][j - 1].rgbtGreen;
                  blue += image[i][j - 1].rgbtBlue;
                  count++;
              }
  
              if (j + 1 &gt;= 0 &amp;&amp; j + 1  &lt;= 255)
              {
                  red += image[i][j + 1].rgbtRed;
                  green += image[i][j + 1].rgbtGreen;
                  blue += image[i][j + 1].rgbtBlue;
                  count++;
              }
  
              if (j - 1 &gt;= 0 &amp;&amp; j - 1  &lt;= 255 &amp;&amp; i - 1 &gt;= 0 &amp;&amp; i - 1 &lt;= 255)
              {
                  red += image[i][j - 1].rgbtRed;
                  green += image[i][j - 1].rgbtGreen;
                  blue += image[i][j - 1].rgbtBlue;
                  count++;
              }
  
              if (i - 1 &gt;= 0 &amp;&amp; i - 1 &lt;= 255)
              {
                  red += image[i][j - 1].rgbtRed;
                  green += image[i][j - 1].rgbtGreen;
                  blue += image[i][j - 1].rgbtBlue;
                  count++;
              }
  
              if (j + 1 &gt;= 0 &amp;&amp; j + 1  &lt;= 255 &amp;&amp; i - 1 &gt;= 0 &amp;&amp; i - 1 &lt;= 255)
              {
                  red += image[i][j - 1].rgbtRed;
                  green += image[i][j - 1].rgbtGreen;
                  blue += image[i][j - 1].rgbtBlue;
                  count++;
              }
  
              if (j - 1 &gt;= 0 &amp;&amp; j - 1  &lt;= 255 &amp;&amp; i + 1 &gt;= 0 &amp;&amp; i + 1 &lt;= 255)
              {
                  red += image[i][j - 1].rgbtRed;
                  green += image[i][j - 1].rgbtGreen;
                  blue += image[i][j - 1].rgbtBlue;
                  count++;
              }
  
              if (i + 1 &gt;= 0 &amp;&amp; i + 1 &lt;= 255)
              {
                  red += image[i][j - 1].rgbtRed;
                  green += image[i][j - 1].rgbtGreen;
                  blue += image[i][j - 1].rgbtBlue;
                  count++;
              }
  
              if (j + 1 &gt;= 0 &amp;&amp; j + 1  &lt;= 255 &amp;&amp; i + 1 &gt;= 0 &amp;&amp; i + 1 &lt;= 255)
              {
                  red += image[i][j - 1].rgbtRed;
                  green += image[i][j - 1].rgbtGreen;
                  blue += image[i][j - 1].rgbtBlue;
                  count++;
              }
  
              image[i][j].rgbtRed = (red / count);
              image[i][j].rgbtGreen = (green / count);
              image[i][j].rgbtBlue = (blue / count);
  
          }
  
      }
      return;
  }
  
  </code></pre>
  
  <p>Error message:</p>
  
  <pre><code>
  helpers.c:170:24: runtime error: index -1 out of bounds for type 'RGBTRIPLE [width]'
  helpers.c:171:26: runtime error: index -1 out of bounds for type 'RGBTRIPLE [width]'
  helpers.c:172:25: runtime error: index -1 out of bounds for type 'RGBTRIPLE [width]'
  helpers.c:178:24: runtime error: index -1 out of bounds for type 'RGBTRIPLE [width]'
  helpers.c:179:26: runtime error: index -1 out of bounds for type 'RGBTRIPLE [width]'
  helpers.c:180:25: runtime error: index -1 out of bounds for type 'RGBTRIPLE [width]'
  helpers.c:146:24: runtime error: index -1 out of bounds for type 'RGBTRIPLE [width]'
  helpers.c:147:26: runtime error: index -1 out of bounds for type 'RGBTRIPLE [width]'
  helpers.c:148:25: runtime error: index -1 out of bounds for type 'RGBTRIPLE [width]'
  helpers.c:154:24: runtime error: index -1 out of bounds for type 'RGBTRIPLE [width]'
  helpers.c:155:26: runtime error: index -1 out of bounds for type 'RGBTRIPLE [width]'
  helpers.c:156:25: runtime error: index -1 out of bounds for type 'RGBTRIPLE [width]'
  
  </code></pre>
  
  <p>My input is:</p>
  
  <pre><code>./filter -b stadium.bmp outfile.bmp
  
  </code></pre>
  
  <p>If anyone needs more information on the pset, please feel free to head over to CS50's own page:</p>
  
  <p><a href="https://cs50.harvard.edu/x/2020/psets/4/filter/less/" rel="nofollow noreferrer">https://cs50.harvard.edu/x/2020/psets/4/filter/less/</a></p>
  
  <p>updated code:</p>
  
  <pre><code>void blur(int height, int width, RGBTRIPLE image[height][width])
  {
      RGBTRIPLE copy[height][width];
      int count = 0;
  
      for (int i = 0; i &lt; height; i++)
      {
          for (int j = 0; j &lt; width; j++)
          {
              int red = 0;
              int green = 0;
              int blue = 0;
  
  
              if (j - 1 &gt;= 0 &amp; j - 1  &lt;= 255)
              {
                  red += image[i][j - 1].rgbtRed;
                  green += image[i][j - 1].rgbtGreen;
                  blue += image[i][j - 1].rgbtBlue;
                  count++;
              }
  
              if (j + 1 &gt;= 0 &amp;&amp; j + 1  &lt;= 255)
              {
                  red += image[i][j + 1].rgbtRed;
                  green += image[i][j + 1].rgbtGreen;
                  blue += image[i][j + 1].rgbtBlue;
                  count++;
              }
  
              if (j - 1 &gt;= 0 &amp;&amp; j - 1  &lt;= 255 &amp;&amp; i - 1 &gt;= 0 &amp;&amp; i - 1 &lt;= 255)
              {
                  red += image[i - 1][j - 1].rgbtRed;
                  green += image[i - 1][j - 1].rgbtGreen;
                  blue += image[i - 1][j - 1].rgbtBlue;
                  count++;
              }
  
              if (i - 1 &gt;= 0 &amp;&amp; i - 1 &lt;= 255)
              {
                  red += image[i - 1][j].rgbtRed;
                  green += image[i - 1][j].rgbtGreen;
                  blue += image[i - 1][j].rgbtBlue;
                  count++;
              }
  
              if (j + 1 &gt;= 0 &amp;&amp; j + 1  &lt;= 255 &amp;&amp; i - 1 &gt;= 0 &amp;&amp; i - 1 &lt;= 255)
              {
                  red += image[i - 1][j + 1].rgbtRed;
                  green += image[i- 1][j + 1].rgbtGreen;
                  blue += image[i- 1][j + 1].rgbtBlue;
                  count++;
              }
  
              if (j - 1 &gt;= 0 &amp;&amp; j - 1  &lt;= 255 &amp;&amp; i + 1 &gt;= 0 &amp;&amp; i + 1 &lt;= 255)
              {
                  red += image[i + 1][j - 1].rgbtRed;
                  green += image[i + 1] [j - 1].rgbtGreen;
                  blue += image[i + 1][j - 1].rgbtBlue;
                  count++;
              }
  
              if (i + 1 &gt;= 0 &amp;&amp; i + 1 &lt;= 255)
              {
                  red += image[i+ 1][j ].rgbtRed;
                  green += image[i+ 1][j].rgbtGreen;
                  blue += image[i+ 1][j].rgbtBlue;
                  count++;
              }
  
              if (j + 1 &gt;= 0 &amp;&amp; j + 1  &lt;= 255 &amp;&amp; i + 1 &gt;= 0 &amp;&amp; i + 1 &lt;= 255)
              {
                  red += image[i + 1][j + 1].rgbtRed;
                  green += image[i+ 1][j + 1].rgbtGreen;
                  blue += image[i+ 1][j + 1].rgbtBlue;
                  count++;
              }
  
              copy[i][j].rgbtRed = round(red / count);
              copy[i][j].rgbtGreen = round(green / count);
              copy[i][j].rgbtBlue = round(blue / count);
  
          }
  
      }
  
      for (int i = 0; i &lt; height; i++)
      {
          for (int j = 0; j &lt; width; j++)
          {
              image[i][j].rgbtRed = copy[i][j].rgbtRed;
              image[i][j].rgbtGreen = copy[i][j].rgbtGreen;
              image[i][j].rgbtGreen = copy[i][j].rgbtGreen;
          }
      }
      return;
  }
  </code></pre>
  
  <p>update 2:</p>
  
  <pre><code>
  void blur(int height, int width, RGBTRIPLE image[height][width])
  {
      RGBTRIPLE copy[height][width];
  
  
      for (int i = 0; i &lt; height; i++)
      {
          for (int j = 0; j &lt; width; j++)
          {
              float red = 0;
              float green = 0;
              float blue = 0;
  
              int count = 0;
  
              if (j &gt;= 0 )
              {
                  red += image[i][j].rgbtRed;
                  green += image[i][j].rgbtGreen;
                  blue += image[i][j].rgbtBlue;
                  count++;
              }
  
              if (j - 1 &gt;= 0 &amp;&amp; j - 1  &lt; width- 1)
              {
                  red += image[i][j - 1].rgbtRed;
                  green += image[i][j - 1].rgbtGreen;
                  blue += image[i][j - 1].rgbtBlue;
                  count++;
              }
  
              if (j + 1 &gt;= 0 &amp;&amp; j + 1  &lt; width - 1)
              {
                  red += image[i][j + 1].rgbtRed;
                  green += image[i][j + 1].rgbtGreen;
                  blue += image[i][j + 1].rgbtBlue;
                  count++;
              }
  
              if (j - 1 &gt;= 0 &amp;&amp; j - 1  &lt; width - 1 &amp;&amp; i - 1 &gt;= 0 &amp;&amp; i - 1 &lt; height - 1)
              {
                  red += image[i - 1][j - 1].rgbtRed;
                  green += image[i - 1][j - 1].rgbtGreen;
                  blue += image[i - 1][j - 1].rgbtBlue;
                  count++;
              }
  
              if (i - 1 &gt;= 0 &amp;&amp; i - 1 &lt; height - 1)
              {
                  red += image[i - 1][j].rgbtRed;
                  green += image[i - 1][j].rgbtGreen;
                  blue += image[i - 1][j].rgbtBlue;
                  count++;
              }
  
              if (j + 1 &gt;= 0 &amp;&amp; j + 1  &lt; width - 1 &amp;&amp; i - 1 &gt;= 0 &amp;&amp; i - 1 &lt; height - 1)
              {
                  red += image[i - 1][j + 1].rgbtRed;
                  green += image[i- 1][j + 1].rgbtGreen;
                  blue += image[i- 1][j + 1].rgbtBlue;
                  count++;
              }
  
              if (j - 1 &gt;= 0 &amp;&amp; j - 1  &lt; width - 1 &amp;&amp; i + 1 &gt;= 0 &amp;&amp; i + 1 &lt; height - 1)
              {
                  red += image[i + 1][j - 1].rgbtRed;
                  green += image[i + 1] [j - 1].rgbtGreen;
                  blue += image[i + 1][j - 1].rgbtBlue;
                  count++;
              }
  
              if (i + 1 &gt;= 0 &amp;&amp; i + 1 &lt; height - 1)
              {
                  red += image[i+ 1][j ].rgbtRed;
                  green += image[i+ 1][j].rgbtGreen;
                  blue += image[i+ 1][j].rgbtBlue;
                  count++;
              }
  
              if (j + 1 &gt;= 0 &amp;&amp; j + 1  &lt; width- 1 &amp;&amp; i + 1 &gt;= 0 &amp;&amp; i + 1 &lt; height - 1)
              {
                  red += image[i + 1][j + 1].rgbtRed;
                  green += image[i+ 1][j + 1].rgbtGreen;
                  blue += image[i+ 1][j + 1].rgbtBlue;
                  count++;
              }
  
              copy[i][j].rgbtRed = round(red / count);
              copy[i][j].rgbtGreen = round(green / count);
              copy[i][j].rgbtBlue = round(blue / count);
  
  
          }
  
      }
  
      for (int i = 0; i &lt; height; i++)
      {
          for (int j = 0; j &lt; width; j++)
          {
              image[i][j].rgbtRed = copy[i][j].rgbtRed;
              image[i][j].rgbtGreen = copy[i][j].rgbtGreen;
              image[i][j].rgbtBlue = copy[i][j].rgbtBlue;
          }
      }
      return;
  }
  
  </code></pre>
  
  <p>error message after running test code:</p>
  
  <pre><code>
  :( blur correctly filters middle pixel
      expected "127 140 149\n", not "70 85 95\n"
  :( blur correctly filters pixel on edge
      expected "80 95 105\n", not "81 101 65\n"
  :( blur correctly filters pixel in corner
      expected "70 85 95\n", not "45 72 71\n"
  :( blur correctly filters 3x3 image
      expected "70 85 95\n80 9...", not "49 57 71\n61 6..."
  :( blur correctly filters 4x4 image
      expected "70 85 95\n80 9...", not "46 60 79\n85 8..."
  </code></pre>