Computing Big Ideas

< CS101

Computing - The World of Computers

1. Data - Lots of Numbers

2. Code

3. Algorithm

Code vs. Algorithm

Here's our monkey/moon bluescreen example code

image = new SimpleImage("monkey.jpg");
back = new SimpleImage("moon.jpg");

for (pixel: image) {
  avg = (pixel.getRed() + pixel.getGreen() + pixel.getBlue())/3;
  if (pixel.getBlue() > avg * 0.92) {
    pixel2 = back.getPixel(pixel.getX(), pixel.getY());
    pixel.setRed(pixel2.getRed());
    pixel.setGreen(pixel2.getGreen());
    pixel.setBlue(pixel2.getBlue());
  }
}

print(image);

4. Bug

for (pixel: image) {
  avg = pixel.getRed() + pixel.getGreen() + pixel.getBlue()/3;
  pixel.setRed(avg);
  pixel.setGreen(avg);
  pixel.setBlue(avg);
}

The bug here is avg computation is missing a set of parenthesis. The intent is to do the addition, then the divide by 3. As written, only the blue value is divide by 3, the other two used as is, resulting in an avg number that is too large, like 300 or 400, so the result will be to mostly set the image to all white. The correct line for the intended algorithm is avg = (pixel.getRed() + pixel.getGreen() + pixel.getBlue())/3;

5. Abstraction