# CS101 2012 Midterm Solution

This is the CS101 midterm from 2012 (with solutions). That year covered similar but not identical material to this year. Note that I re-used most the short-answer questions at the start of this exam as our homework-4.

a. True or false - the color (r:50, g:50, b:50) is darker than the color (r:25, g:25, b:25):

b. What exactly does the following code do to the image:

```image = new SimpleImage("something.jpg");
pixel = image.getPixel(1, 0);
pixel.setRed(255);
pixel.setGreen(255);
pixel.setBlue(255);
```

c. Number of bits in a byte:

d. For each row, write the number of distinct patterns possible for the given number of bits (do not write the actual patterns, just the number of patterns)

• 1 bit, number of patterns:
• 2 bits, number of patterns:
• 3 bits, number of patterns:
• 4 bits, number of patterns:

e. A hard drive records 1's and 0's on a spinning platter as what physical phenomenon:

f. You have 100 image files, each taking up 300 KB. How much space do they take up overall (include the units in your answer):

g. What is the total capacity of four 500 MB hard drives plus one 2 GB flash drive (include the units in your answer):

h. What specifically is the type of input and output of a C++ compiler (i.e. the compiler scenario from lecture):

3 points apiece unless noted otherwise

• a. false
• b. Sets the pixel at (1, 0) (one to the right of the upper left corner) to white
• c. 8 bits in a byte (2 points)
• d. 2, 4, 8, 16 (4 points)
• e. magnetism (we also accepted "electromagnetism" as close enough)
• f. 30 MB or equivalently 30,000 KB
• g. 4 GB or equivalently 4,000 MB
• h. input: C++ source code, output: program file (.exe) / machine code instructions

### Coding Problems

Problem 1 (8 points)

Write code to make the following change to an image. For every pixel, set the red and green value to 200 and the blue value to 0.

(b) What does resulting image look like when you have done this (2 word answer): (answer yellow rectangle)

```image = new SimpleImage("something.jpg");
for (pixel: image) {
pixel.setRed(200);
pixel.setGreen(200);
pixel.setBlue(0);
}
```
• +6 set colors
• +2 image will be "yellow" or "yellow rectangle"

Problem 2 (8 points)

Write code to make the following change to an image. For every pixel in the right half of the image, set the red value to 255.

```image = new SimpleImage("something.jpg");
for (pixel: image) {
if (pixel.getX() > image.getWidth() / 2) {
pixel.setRed(200);
}
}
```
• +4 correct if-condition
• +4 set red value
Problem 3 (10 points)

Write code to make the following change to an image. For every pixel, double the red value, and change the green value to be triple the blue value.

```image = new SimpleImage("something.jpg");
for (pixel: image) {
pixel.setRed(pixel.getRed() * 2);
pixel.setGreen(pixel.getBlue() * 3);
}
```
• +5 set red value
• +5 set green value

Problem 4 (20 points)

This problem concerns a sort of bluescreen computation. As usual, we have two images -- image and back, and the back image is at least as large as the main image. Write code that for each pixel in image, if the blue value is greater than double the green value, copy over the corresponding pixel from the back image. Exception: For this problem, our standard definition of "copy over a pixel" is changed as follows: copy over the pixel normally, except do not copy over the blue value.

```image = new SimpleImage("image.jpg");
back = new SimpleImage("back.jpg");
for (pixel: image) {
if (pixel.getBlue() > pixel.getGreen() * 2) {
pixel2 = back.getPixel(pixel.getX(), pixel.getY())
pixel.setRed(pixel2.getRed());
pixel.setGreen(pixel2.getGreen());
}
}
```
• +6 correct if-condition
• +6 get the back pixel
• +6 copy over the back pixel (minus the blue channel)
• +2 correct structure

Problem 5 (10 points)

Write code to make the following change to an image. For every pixel, consider the average value of its red, green, and blue values. If the average value is more than 200, set the red value to 0.

```image = new SimpleImage("image.jpg");
for (pixel: image) {
avg = (pixel.getRed() + pixel.getGreen() + pixel.getBlue())/3;
if (avg > 200) {
pixel.setRed(0;
}
}
```
• +5 calculate average
• +4 correct if-condition
• +1 set red value

Problem 6 (20 points)

Write code to make the following change to an image. For every pixel, consider the average value of its red, green, and blue values. If the red value is more than double the average value, set the red value to be the same as the average value. In all cases (i.e. regardless of the red/average relationship) set the green value to 0.

```image = new SimpleImage("image.jpg");
for (pixel: image) {
avg = (pixel.getRed() + pixel.getGreen() + pixel.getBlue())/3;
if (pixel.getRed() > avg * 2) {
pixel.setRed(avg);
}
pixel.setGreen(0);
}
```
• +6 calculate average
• +6 correct if-condition
• +2 set red value (inside if-body)
• +6 set green value (outside if-body)