title: embedded else-if loop is somehow changing values
body: <p>I'm completing an assignment for the CS50x course, and I'm confident, after many tests, that the <em>math</em> within my code is correct. However, an embedded else-if statement is changing the values of the temp[height][width] array, which stores the hexadecimal values for the Blue, Green, and Red part of the pixel (i.e., temp[][].rgbtBlue, temp[][].rgbtGreen, temp[][].rgbtRed. For some reason, the value of this and it seems like no one else has had this frustrating bug.</p>
  <p>To preface my code, the goal of the assignment is to filter an image in 4 different ways, and this final filter is edge detection. After initializing some arrays, I begin to go through each row (for loop with int i), then each pixel within that row (embedded for loop with int j), and height and width are the dimensions of the input image. Once within the embedded for loop, I put an if-else statement to check if the temp[i][j] pixel is a corner/edge, or neither, and I embedded further if-else-if statements within the if statement to determine if the temp[i][j] corner/edge pixel is a corner or an edge, which would slightly change how edges are detected.</p>
  <p>The bug occurs in the else-if statement checking if the pixel is the lower left corner. For example, when temp[0][0].rgbtBlue is supposed to be 4, it becomes 7. Before this else-if loop, temp[0][0] is 4. It stays 7 for the rest of the parent if-else statement (the one meant to check for a corner/edge pixel, just under the embedded for (int j) loop). However, once this if-else statement ends, temp[0][0].rgbtBlue is 4, again, but the damage has already been done.</p>
  <p>This bug is wild to me, especially because I'm fairly new to coding. I have absolutely no idea what to do. My best guess is that it has something to do with memory.</p>
  <pre><code>// Detect edges
  void edges(int height, int width, RGBTRIPLE image[height][width])
  {
      // Create temp array
      RGBTRIPLE temp[height][width];
      for (int i = 0; i &lt; height; i++)
      {
          for (int j = 0; j &lt; width; j++)
          {
              temp[i][j] = image[i][j];
          }
      }
  
      // Sobel operator array
      int Gx[3][3] = {{-1, 0, 1}, {-2, 0, 2}, {-1, 0, 1}};
      int Gy[3][3] = {{-1, -2, -1}, {0, 0, 0}, {1, 2, 1}};
  
      // Compute Gx &amp; Gy for each channel RGB
      for (int i = 0; i &lt; height; i++)
      {
          for (int j = 0; j &lt; width; j++)
          {
              // Reset values to 0 for each pixel
              int xB = 0;
              int xG = 0;
              int xR = 0;
  
              int yB = 0;
              int yG = 0;
              int yR = 0;
              
              // Check for edge-pixel
              if (i == 0 || j == 0 || i == height - 1 || j == height - 1)
              {
                  // Upper left corner
                  if (i == 0 &amp;&amp; j == 0)
                  {
                      for (int m = i, g = 1; m &lt; i + 2; m++, g++)
                      {
                          for (int n = j, h = 1; n &lt; j + 2; n++, h++)
                          {
                              xB += (temp[m][n].rgbtBlue * Gx[g][h]);
                              xG += (temp[m][n].rgbtGreen * Gx[g][h]);
                              xR += (temp[m][n].rgbtRed * Gx[g][h]);
  
                              yB += (temp[m][n].rgbtBlue * Gy[g][h]);
                              yG += (temp[m][n].rgbtGreen * Gy[g][h]);
                              yR += (temp[m][n].rgbtRed * Gy[g][h]);
                          }
                      }
                  }
                  // Lower left corner, THIS IS WHERE THE BUG 'STARTS'
                  else if (i == height - 1 &amp;&amp; j == 0)
                  {
                      for (int m = i - 1, g = 0; m &lt; i + 1; m++, g++)
                      {
                          for (int n = j, h = 1; n &lt; j + 2; n++, h++)
                          {
                              xB += (temp[m][n].rgbtBlue * Gx[g][h]);
                              xG += (temp[m][n].rgbtGreen * Gx[g][h]);
                              xR += (temp[m][n].rgbtRed * Gx[g][h]);
  
                              yB += (temp[m][n].rgbtBlue * Gy[g][h]);
                              yG += (temp[m][n].rgbtGreen * Gy[g][h]);
                              yR += (temp[m][n].rgbtRed * Gy[g][h]);
                          }
                      }
                  }
                  // Upper right corner
                  else if (i == 0 &amp;&amp; j == width - 1)
                  {
                      for (int m = i, g = 1; m &lt; i + 2; m++, g++)
                      {
                          for (int n = j - 1, h = 0; n &lt; j + 1; n++, h++)
                          {
                              xB += (temp[m][n].rgbtBlue * Gx[g][h]);
                              xG += (temp[m][n].rgbtGreen * Gx[g][h]);
                              xR += (temp[m][n].rgbtRed * Gx[g][h]);
  
                              yB += (temp[m][n].rgbtBlue * Gy[g][h]);
                              yG += (temp[m][n].rgbtGreen * Gy[g][h]);
                              yR += (temp[m][n].rgbtRed * Gy[g][h]);
                          }
                      }
                  }
                  // Lower right corner
                  else if (i == height - 1 &amp;&amp; j == width - 1)
                  {
                      for (int m = i - 1, g = 0; m &lt; i + 1; m++, g++)
                      {
                          for (int n = j - 1, h = 0; n &lt; j + 1; n++, h++)
                          {
                              xB += (temp[m][n].rgbtBlue * Gx[g][h]);
                              xG += (temp[m][n].rgbtGreen * Gx[g][h]);
                              xR += (temp[m][n].rgbtRed * Gx[g][h]);
  
                              yB += (temp[m][n].rgbtBlue * Gy[g][h]);
                              yG += (temp[m][n].rgbtGreen * Gy[g][h]);
                              yR += (temp[m][n].rgbtRed * Gy[g][h]);
                          }
                      }
                  }
                  // West edge
                  else if (j == 0 &amp;&amp; i != 0 &amp;&amp; i != height - 1)
                  {
                      for (int m = i - 1, g = 0; m &lt; i + 1; m++, g++)
                      {
                          for (int n = j, h = 1; n &lt; j + 2; n++, h++)
                          {
                              xB += (temp[m][n].rgbtBlue * Gx[g][h]);
                              xG += (temp[m][n].rgbtGreen * Gx[g][h]);
                              xR += (temp[m][n].rgbtRed * Gx[g][h]);
  
                              yB += (temp[m][n].rgbtBlue * Gy[g][h]);
                              yG += (temp[m][n].rgbtGreen * Gy[g][h]);
                              yR += (temp[m][n].rgbtRed * Gy[g][h]);
                          }
                      }
                  }
                  // East edge
                  else if (j == width - 1 &amp;&amp; i != 0 &amp;&amp; i != height - 1)
                  {
                      for (int m = i - 1, g = 0; m &lt; i + 1; m++, g++)
                      {
                          for (int n = j - 1, h = 0; n &lt; j + 1; n++, h++)
                          {
                              xB += (temp[m][n].rgbtBlue * Gx[g][h]);
                              xG += (temp[m][n].rgbtGreen * Gx[g][h]);
                              xR += (temp[m][n].rgbtRed * Gx[g][h]);
  
                              yB += (temp[m][n].rgbtBlue * Gy[g][h]);
                              yG += (temp[m][n].rgbtGreen * Gy[g][h]);
                              yR += (temp[m][n].rgbtRed * Gy[g][h]);
                          }
                      }
                  }
                  // North edge
                  else if (i == 0 &amp;&amp; j != 0 &amp;&amp; j != width - 1)
                  {
                      for (int m = i, g = 1; m &lt; i + 2; m++, g++)
                      {
                          for (int n = j - 1, h = 0; n &lt; j + 2; n++, h++)
                          {
                              xB += (temp[m][n].rgbtBlue * Gx[g][h]);
                              xG += (temp[m][n].rgbtGreen * Gx[g][h]);
                              xR += (temp[m][n].rgbtRed * Gx[g][h]);
  
                              yB += (temp[m][n].rgbtBlue * Gy[g][h]);
                              yG += (temp[m][n].rgbtGreen * Gy[g][h]);
                              yR += (temp[m][n].rgbtRed * Gy[g][h]);
                          }
                      }
                  }
                  // South edge
                  else if (i == height - 1 &amp;&amp; j != 0 &amp;&amp; j != width - 1)
                  {
                      for (int m = i - 1, g = 0; m &lt; i + 1; m++, g++)
                      {
                          for (int n = j - 1, h = 0; n &lt; j + 2; n++, h++)
                          {
                              xB += (temp[m][n].rgbtBlue * Gx[g][h]);
                              xG += (temp[m][n].rgbtGreen * Gx[g][h]);
                              xR += (temp[m][n].rgbtRed * Gx[g][h]);
  
                              yB += (temp[m][n].rgbtBlue * Gy[g][h]);
                              yG += (temp[m][n].rgbtGreen * Gy[g][h]);
                              yR += (temp[m][n].rgbtRed * Gy[g][h]);
                          }
                      }
                  }
              }
              // Pixel is not a corner nor an edge
              else
              {
                  for (int m = i - 1, g = 0; m &lt; i + 2; m++, g++)
                  {
                      for (int n = j - 1, h = 0; n &lt; j + 2; n++, h++)
                      {
                          xB += (temp[m][n].rgbtBlue * Gx[g][h]);
                          xG += (temp[m][n].rgbtGreen * Gx[g][h]);
                          xR += (temp[m][n].rgbtRed * Gx[g][h]);
  
                          yB += (temp[m][n].rgbtBlue * Gy[g][h]);
                          yG += (temp[m][n].rgbtGreen * Gy[g][h]);
                          yR += (temp[m][n].rgbtRed * Gy[g][h]);
                      }
                  }
              }
              // THIS IS WHERE THE BUG 'ENDS'
  
              // Computing new channel values
              int GzB = round(sqrt((xB*xB) + (yB*yB)));
              int GzG = round(sqrt((xG*xG) + (yG*yG)));
              int GzR = round(sqrt((xR*xR) + (yR*yR)));
  
              // Cap at 255
              if (GzB &gt; 255)
              {
                  GzB = 255;
              }
              if (GzG &gt; 255)
              {
                  GzG = 255;
              }
              if (GzR &gt; 255)
              {
                  GzR = 255;
              }
  
              // Assign new channel value to temp array
              temp[i][j].rgbtBlue = GzB;
              temp[i][j].rgbtGreen = GzG;
              temp[i][j].rgbtRed = GzR;
  
              // Assign pixel their channel value via temp array
              image[i][j] = temp[i][j];
          }
      }
  
      return;
  }
  </code></pre>