CSCI 150: PreLab 4

Pictures and Functions
Due: 9AM on Wednesday, September 25th

In this prelab you will formulate some of the ideas necessary to complete Lab 04. Please turn in your solution on Gradescope. You can either turn it in as a PDF (e.g., saving a document as a PDF in Word), take a picture (e.g., with a smartphone), or scan it (e.g., at the library) to hand it in. Please remember, no late prelabs allowed!

Reading

Read this Scientific American article about how diversity makes us smarter. If you're so inclined, you should also check out the following embarrassing example of what can happen when your development and testing teams aren't diverse: Link

1. Did you do the reading? (Yes or no will suffice.)

Primes

As you may know, a number x is said to be prime if x is at least 2, and the only proper factors of x are itself and 1. So the first few primes are 2, 3, 5, 7, 11, 13, 17, 19, 23, etc. 4 isn't prime, since it is divisible by 2. Same goes for 6 and 8. 9 is out thanks to 3. And so on. There are a lot of primes. More precisely, there are infinitely many primes.

A twin prime is a pair of prime numbers that differ by exactly 2. So (3,5), (5,7), (11,13), (17,19) and (29, 31) are all twin primes. Note that not every prime is part of a twin prime. It is conjectured that there are infinitely many twin primes too, but no one knows for sure.

Describe the Problem:

The problem you will solve on your lab is as follows.

Input: An integer n from the user that represents the number of primes to print out.
Output: The first n primes, and the number of twin primes amongst these.

Understand the Problem:

If the user enters 13 then the output should be

        The first 13 primes are:
        2 3 5 7 11 13 17 19 23 29 31 37 41
        Amongst these there are 5 twin primes.

                
Note that (41, 43) is a twin prime, but we didn't count it since 43 wasn't amongst the first 13 primes.

Design an Algorithm:

Write pseudocode to solve this problem. You should decompose your main algorithm into small manageable chunks. For example, you should
  • design code that takes an integer x and determines whether x is prime. E.g. it should should be False for 30, but True for 31.
  • make repeated use of this code to generate the first n primes.
2. Suppose x and f are both integers. Find a boolean expression which is true if and only if f is a proper factor of x. That is, the boolean expression should be true if x is evenly divisible by f, and false otherwise. For example, if x and f are 18 and 6 respectively, then the expression should evaluate to true, since 6 goes evenly into 18. For 18 and 5, however, the expression should evaluate to false, since 5 doesn't go evenly into 18.

3. Use the expression from question 2 to create pseudocode for an algorithm that determines whether x is prime.

Image Manipulation

In this portion of the lab you'll create a nifty program that reads in an image and does a sequence of modifications to that image, as specified by the user. Things like negating the image, blurring it, changing the contrast, etc.

Describe the Problem:

Your program should prompt the user to enter the filename of an image to edit. It should then repeatedly prompt them for filters to apply, and show the cumulative effect of these filters on the specified image.

Understand the Problem:

original

Consider the image of crayons above. There are numerous transformations we might apply to this image. For the lab, you'll be creating a program capable of the following operations:
  1. Make Negative
  2. Make Grayscale
  3. Posterize
  4. Flip Horizontally
  5. Scroll Horizontally
  6. Zoom
  7. Blur
  8. Your Own Effect


Examples of these operations are given below.
Original Image:
original
Negative: Greyscale:
flip mirror
Flip: Scroll:
scroll negative
Zoom: Blur:
zoom poster
Posterize:
scroll
Other Ideas
Find Edges: Tiled:
find edges tiling
Shear: Bizarre:
shear bizarre

Design an Algorithm:

You will want to write pseudocode before you start writing code. Details for some of these operations are given below.

  • Negative: The negative of an image is creating by inverting each color channel. So if the red value of a pixel were 255, it should become 0. If it were 254, it should become 1, and so on, down to 0, which should become 255. Similarly for green and blue.

  • Grayscale: Shades of gray have the same red, green and blue value. To convert an image to grayscale, you want to set the red, green and blue values all to the average value of the three channels of the original pixel.

  • Posterize: A typical pixel can have one of 256 value for each color channel. In a posterized image, this number is drastically decreased. Each color channel value should be rounded to the nearest multiple of 32.

  • Scrolling: Scrolling should ask the user to specify some number of pixels, and should then shift the image that many to the right. Pixels that would fall off the edge of the image should wrap around to the other size. Modular arithmetic may come in handy here.

  • Zoom: This method result in an image of the same size as the original, but consist of the center of the image blown up by a factor of 2. So if the image has width w and height h, zooming should expand the middle w/2 by h/2 region to fill the whole picture.

  • Blur: When you blur an image, you set the color of each pixel to be the average of the 9 pixels in the 3 x 3 square centered at that pixel (i.e. the average of the original pixel and its original 8 neighbors). You'll probably want to create a new Picture object; otherwise, you'll be adjusting pixel values that you'll need for subsequent calculations. Be careful at the borders, not all pixels have 8 neighbors!
In the lab, you'll be writing a program that performs many of the above transformations on images. In preparation, you'll be generating pseudocode to perform some of these operations. As an example, we consider the task of removing all red from an image, and give a pseudocode solution is given below. Give your own pseudocode solutions to the remaining problems.

Hints:
  • For some of these operations, you may want to create a new image rather than by simply modifying the existing image. You may assume you can generate a copy of an image.

  • You can not change the position of a pixel. You can, however, read the r, g and b values for any pixel, and you can set the r, g and b values of any pixel.
Ex. Remove all red from an image.
  Sample answer:

  loop over x values from 0 to W - 1 inclusive (You can assume W is the width)
      loop over y values from 0 to H - 1 inclusive (You can assume H is the height)
	  change the red value of the pixel at (x,y) to 0
								    
4. Negate the image.

5. Convert the image to grayscale.

6. Scroll the image to the right by d pixels.

Honor Code

If you followed the Honor Code in this assignment, write the following sentence attesting to the fact at the top of your homework.

I affirm that I have adhered to the Honor Code in this assignment.