Skip to content

Instantly share code, notes, and snippets.

@CompSciRocks
Last active November 27, 2018 18:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CompSciRocks/d83bf0f9e8f9b24ddce9851e5d3a0cdb to your computer and use it in GitHub Desktop.
Save CompSciRocks/d83bf0f9e8f9b24ddce9851e5d3a0cdb to your computer and use it in GitHub Desktop.
Solution for GrayImage free response question on 2012 AP Computer Science exam - https://compsci.rocks
public int countWhitePixels() {
int cnt = 0;
for (int r=0; r<pixelValues.length; r++) {
for (int c=0; c<pixelValues[r].length; c++) {
if (pixelValues[r][c] == WHITE)
cnt++;
}
}
return cnt;
}
public int countWhitePixels() {
int cnt = 0;
for (int[] row: pixelValues) {
for (int val: row) {
if (val == WHITE)
cnt++;
}
}
return cnt;
}
public class GrayImage {
public static final int BLACK = 0;
public static final int WHITE = 255;
private int[][] pixelValues;
public int countWhitePixels() {
// Part A
}
public void processImage() {
// Part B
}
public void processImage() {
for (int r=0; r<pixelValues.length-2; r++) {
for (int c=0; c<pixelValues[r].length-2; c++) {
pixelValues[r][c] -= pixelValues[r+2][c+2];
if (pixelValues[r][c] < BLACK)
pixelValues[r][c] = BLACK;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment