DEV Community

theohartsook
theohartsook

Posted on

Debugging in CS50

Over the summer I have been working on Harvard's CS50 EdX course and it's been a really fun experience (working on C++ is on pause until I finish CS50). After working with pointers in C I finally feel like I understand (big picture) how memory works.
One of the assignments is to create a simple box blur. One of the neat features of CS50 is that they provide tests for the code to see if we completed the assignment correctly or not. I really appreciate this as I can't visually assess if a blurred pixel is the right kind of blurred.
I had been pretty dispirited this week because I wasn't sure why my blur was failing. I was getting most of the blur correct, but it would fail for one pixel in one test case and two pixels in the second test case.
Alt Text
I knew I had solved it in a suboptimal way because I was treating each corner and edge differently, rather than working with a buffer or some of the other solutions I saw online later. It's a pretty janky solution, but it's one I came up with independently. I figured there would be some kind of problem here, and my intuition was ultimately right.
My first thought was that since it only seemed to the last row that failed, I should check that I wasn't accidentally taking the average of the wrong color channel or indexing wrong. Those turned out to be correct, no matter how many times I reread them.
And then this morning, when I had a free 15 minutes, I decided I would take a look at it again. I copied the expected output and my output into a table so I could look for exactly what was wrong. This instantly revealed the problem. My corner pixels were correct, but the bottom edge pixels were failing. I checked that section of my code again and everything looked correct... until I finally noticed the mistake.
// bottom edge
else if (top_edge == 1)
{
... // calculate the average for each color channel
}

I was checking the wrong condition. I corrected that, recompiled, reran the tests, and passed. I'm glad I finally fixed that mistake.
Alt Text

Top comments (0)