CSCI 150: PreLab 4
Pictures and Functions
Due: 9AM on Wednesday, February 28
In this prelab you will formulate some of the ideas necessary to complete Lab 04. Please type up your solutions, and hand in a paper copy before the beginning of lecture on Wednesday. Remember, no late prelabs allowed!
Reading
Read about one of the first Computer Scientists, Ava Lovelace, who predated computers by a century.
Conditionals
if (n%2) == 0 :
print(n)
else :
print(n*2)
if (n%3) == 0 :
print(n//3)
What is the output of this code snippet for each of the following values of n?
 n = 10:
 n = 9:
 n = 6:
 n = 5:
Method Practice
def main() :
blarg(5)
blorf(3)
def blarg(x) :
print(x*x)
print(x)
def blorf(y) :
blarg(y+1)
print(y)
blarg(y1)
main()
4. What is the output of the following program?
def main() :
x = 2, y = 7
print(x, y, end="")
x = blunk(x, y)
print(x, y, end="")
def blunk(y, x) :
print(x, y, end="")
y = y * y
x = x//2
print(x, y, end="")
return x*y
main()
Part 2  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. This can actually be shown pretty easily; ask if you're curious.
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: 
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

6. Use the expression from question 5 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: 
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:
Examples of these operations are given below.


Design an Algorithm: 
Your program should begin by printing a welcome to the user, informing them just how fortunate they are to have stumbled upon your very own image editor. You should then prompt the user in the console to pick a file to load in. As will be standard from here on out, you'll want to be robust against bad user input, so if the user enters something that isn't a valid file and an exception is thrown, you can catch it and prompt them again for a file.
Once you have an image loaded, display it. Then use a while loop to repeatedly print a table of possible operations, prompt the user to select one of these operations to apply to their image, apply the selected operation (create a distinct function for each operation), and display the resulting image. Thus, the user might choose to reflect the image, then increase the contrast (of the now reflected image), and then blur (the now reflected and contrasty image). At each step, the user should be able to see the resulting image. Again, be sure to handle bad input from the user. Of course, this still leaves out the details of each operation, for which you will want to write pseudocode before you start writing functions. Details for some of these operations are given below.

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.
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 07. Negate the image.
8. Convert the image to grayscale.
9. 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.