CPSC 203: Introduction to computers |
Write all your answers in the starting [outline file]. The solutions have been tested using JES 3.1 which you can download to run on your own computer following the instruction available at UofC Wiki.
1. Define a function named computeSum that takes as parameter a non-negative integer n and returns the value of S such that Write your answer under the comment “Start of Solution to Question 1”.
2. Define a function named computePartialSum that takes as parameters and where and then prints the value of S such that Make use of the function computeSum to define the function computePartialSum. Write your answer under the comment “Start of Solution to Question 2”.
3. Define a function named convertTo2ColorImage that loads a color image using the file dialog and displays an approximation of the loaded color image using only the colors of black and white. Write your answer under the comment “Start of Solution to Question 3”. You can use the [cartoon cow picture] to test your results.
4. Define a function named rotate90Degrees that takes as parameter an image and returns a version of that image which is rotated clockwise by 90 degrees. Write your answer under the comment “Start of Solution to Question 4”. You can use the [bird picture] to test your results.
5. Define a function named rotate180Degrees that loads an image using the file dialog and then displays a version of the loaded image which is rotated clockwise by 180 degrees. Make use of the function rotate90Degrees to define the function rotate180Degrees. Write your answer under the comment “Start of Solution to Question 5”. You can use the [bird picture] to test your results.
6. Define a function named flipHorizontally that loads an image using the file dialog and then displays a version of the loaded image which is flipped horizontally. Write your answer under the comment “Start of Solution to Question 6”. You can use the [bird picture] to test your results.
7. In the function drawSmileyFace, add code to perform the following operations in the given order under the comment “Start of Solution to Question 7”. You will need the function in the file [smiley.py] to complete this question.
(a) Draw a yellow filled-oval of width 200 pixels and height 200 pixels so that its left edge touches the 100th pixel from the left of the image and its top edge touches the 100th pixel from the top of the image.
(b) Draw a black oval of width 200 pixels and height 200 pixels so that its left edge touches the 100th pixel from the left of the image and its top edge touches the 100th pixel from the top of the image.
(c) Draw a black filled-oval of width 30 pixels and height 40 pixels so that its left edge touches the 150th pixel from the left of the image and its top edge touches the 150th pixel from the top of the image.
(d) Draw a black filled-oval of width 30 pixels and height 40 pixels so that its left edge touches the 220th pixel from the left of the image and its top edge touches the 150th pixel from the top of the image.
(e) Draw a black filled-oval of width 100 pixels and height 65 pixels so that its left edge touches the 150th pixel from the left of the image and its top edge touches the 200th pixel from the top of the image.
(f) Draw a yellow filled-oval of width 100 pixels and height 50 pixels so that its left edge touches the 150th pixel from the left of the image and its top edge touches the 180th pixel from the top of the image.
(g) Draw a red filled-oval of width 40 pixels and height 40 pixels so that its left edge touches the 180th pixel from the left of the image and its top edge touches the 180th pixel from the top of the image.
(h) Add the text “Happy Halloween!” in blue color to position (150, 140) on the image.
8. If you call the function printProduct with parameters 5 and 19 (You will need the function in the file [printProduct.py] to complete this question):
(a) What will be the final values of product and factor? Write your answer at the end of the comment “#Solution to Question 8(a):”.
(b) How many times the if-block will be executed? Write your answer at the end of the comment “#Solution to Question 8(b):”.
(c) Which line(s) of code are executed as part of the if-block? Write your answer at the end of the comment “#Solution to Question 8(c):”.
(d) How many times the else-block will be executed? Write your answer at the end of the comment “#Solution to Question 8(d):”.
(e) Which line(s) of code are executed as part of the else-block? Write your answer at the end of the comment “#Solution to Question 8(e):”.
The solution to this problem set can be found in this [compressed file].