Exam 1: Scheme and Image Basics

Assigned: Wednesday, 19 September 2012

Due: 10:30 p.m., Tuesday, 25 September 2012


Exam format

This is a take-home examination. You may use any time or times you deem appropriate to complete the exam, provided you return it to me by the due date.

There are 10 problems on this examination. Each problem is worth 10 points, for a total of 100 points. Although each problem is worth the same amount, problems are not necessarily of equal difficulty.

Read the entire exam before you begin.

We expect that someone who has mastered the material and works at a moderate rate should have little trouble completing the exam in a reasonable amount of time. In particular, this exam is likely to take you about two to three hours, depending on how well you've learned the topics and how fast you work. You should not work more than four hours on this exam. Stop at four hours and write “There's more to life than CS” and you will earn at least a 80 on this exam if you give evidence of a serious attempt on at least 6 of the problems or come talk to me about the exam no later than one week after it is returned.

I would also appreciate it if you would write down the amount of time each problem takes. Each person who does so will earn two points of extra credit. Because I worry about the amount of time my exams take, I will give two points of extra credit to the first two people who honestly report that they have completed the exam in three hours or less or have spent at least three hours on the exam. In the latter case, they should also report on what work they've completed in the three hours. After receiving such notices, I may change the exam.

Academic honesty

This examination is open book, open notes, open mind, open computer, open Web. However, it is closed person. That means you should not talk to other people about the exam. Other than as restricted by that limitation, you should feel free to use all reasonable resources available to you.

As always, you are expected to turn in your own work. If you find ideas in a book or on the Web, be sure to cite them appropriately. If you use code that you wrote for a previous lab or homework, cite that lab or homework and the other members of your group. If you use code that you found on the course Web site, be sure to cite that code. You need not cite the code provided in the body of the examination.

Although you may use the Web for this exam, you may not post your answers to this examination on the Web. And, in case it's not clear, you may not ask others (in person, via email, via IM, by posting a please help message, or in any other way) to put answers on the Web.

Because different students may be taking the exam at different times, you are not permitted to discuss the exam with anyone until after I have returned it. If you must say something about the exam, you are allowed to say “This is among the hardest exams I have ever taken. If you don't start it early, you will have no chance of finishing the exam.” You may also summarize these policies. You may not tell other students which problems you've finished. You may not tell other students how long you've spent on the exam.

You must include both of the following statements on the cover sheet of the examination.

  1. I have neither received nor given inappropriate assistance on this examination.
  2. I am not aware of any other students who have given or received inappropriate assistance on this examination.

Please sign and date each statement. Note that the statements must be true; if you are unable to sign either statement, please talk to me at your earliest convenience. You need not reveal the particulars of the dishonesty, simply that it happened. Note also that inappropriate assistance is assistance from (or to) anyone other than Professor Weinman (that's me) or Professor Price-Jones.

Presenting Your Work

You must present your exam to me in two forms: both physically and electronically. That is, you must write all of your answers using the computer, print them out, number the pages, put your name on the top of every page, and hand me the printed copy. You must also email me a copy of your exam. You should create the emailed version by copying the various parts of your exam and pasting them into an email message. In both cases, you should put your answers in the same order as the problems. Failure to name and number the printed pages will lead to a penalty of two points. Failure to turn in both versions may lead to a much worse penalty.

While your electronic version is due Tuesday night, your physical copy will be submitted in class on Wednesday. It is presumed the physical copy matches the electronic copy. Any discrepancies (other than formatting) will be considered a misrepresentation of your work and referred to the Committee on Academic Standing.

In many problems, I ask you to write code. Unless I specify otherwise in a problem, you should write working code and include examples that show that you've tested the code. You should use examples other than those that may be provided with the exam to indicate your tests. Do not include images; I should be able to regenerate those.

Unless I explicitly ask you to document your procedures, you need not write introductory comments. However, inline comments are often useful for explaining portions of your algorithm.

Just as you should be careful and precise when you write code and documentation, so should you be careful and precise when you write prose. Please check your spelling and grammar. Because I should be equally careful, the whole class will receive one point of extra credit for each error in spelling or grammar you identify on this exam. I will limit that form of extra credit to five points.

I will give partial credit for partially correct answers. I am best able to give such partial credit if you include a clear set of work that shows how you derived your answer. You ensure the best possible grade for yourself by clearly indicating what part of your answer is work and what part is your final answer.

Getting Help

I may not be available at the time you take the exam. If you feel that a question is badly worded or impossible to answer, note the problem you have observed and attempt to reword the question in such a way that it is answerable. If it's a reasonable hour (7 a.m. - 6 p.m.), feel free to try to call me in the office (269-9812).

I will also reserve time at the start of each class before the exam is due to discuss any general questions you have on the exam.


Part A: Numbers and procedures

Topics: Numeric values, procedures, documentation.

Problem 1: Calculating a circle's area

Write a procedure, (circle-area diameter), that calculates the area of a circle with the specified diameter.

Recall that the area of a circle is given by area = π * radius^2 where radius = diameter/2. Note the variable pi is predefined in Scheme with the expected geometric constant value.

Problem 2: Converting degrees Celsius to degrees Fahrenheit

Write a procedure, (celsius-to-fahrenheit ctemp), that computes the Fahrenheit equivalent of the Celsius temperature ctemp. You will use the formula ftemp = ctemp * 9 / 5 + 32, where ctemp is the temperature in degrees Celsius and ftemp is the same temperature expressed in degrees Fahrenheit. You must take care to ensure that you do not calculate an exact number. For example:

> (celsius-to-fahrenheit 37)

Problem 3: Reading code

Consider the following procedure.

(define fun
  (lambda (n)
    (/ (round (* n 10.0)) 10)))

Your job is to be a code detective. Read the procedure definition carefully and call the procedure with many different parameters to learn what the procedure does and how it works.

Hint: You can discover the purpose of the procedure either by trying many examples or by working through an example by hand.

a. In your own words, describe what fun computes. That is, explain the purpose of the procedure.

b. In your own words, explain how fun works. That is, explain the algorithm that is used.

Problem 4: Documenting fun

Give the procedure from problem 3 a meaningful name and parameters and write the 6-P documentation for it.

Problem 5: Calculating angles

Write the procedure angle-difference to conform to the following specification.

;;; Procedure:
;;;   angle-difference
;;; Parameters:
;;;   angle1, an integer
;;;   angle2, an integer
;;; Purpose:
;;;   Calculate the difference between two angles in degrees
;;; Produces:
;;;   diff, an integer
;;; Preconditions:
;;;   [No additional.]
;;; Postconditions:
;;;   0 <= diff <= 180
;;;   diff is the smaller of the two angles between angle1 and angle2

For example:

> (angle-difference 10 30)
> (angle-difference 10 -30)
> (angle-difference 90 -90)
> (angle-difference -10 180)

Part B: Modifying drawings

Problem 6: Aligning drawings

Topics: Procedures, drawings.

In vector graphics programs (such as those for generating presentation slides), a common operation is to align and adjoin two drawing elements. Write a procedure (drawing-abut-bottom reference abutter) where reference and abutter are both drawings. The procedure drawing-abut-bottom should produce a new drawing consisting of reference grouped with a version of abutter that has been translated so that its left edge is aligned with reference and its top edge adjoins the bottom edge of reference.

The example below uses drawings from the reading on procedures.

(image-show (drawing->image (drawing-abut-bottom (drawing-hshift red-eye 20) blue-i) 200 100))

Note: The built-in MediaScheme procedures drawing-left, drawing-right, drawing-height, drawing-bottom and/or drawing-top may be useful.

Problem 7: Drawing pedestals

Using your procedure drawing-abut-bottom, write a procedure (drawing-pedestal drawing height color) that creates a new drawing with an added pedestal box having the same width as drawing and the specified height and color abutting the bottom of drawing.

(image-show (drawing->image (drawing-pedestal (drawing-hshift red-eye 20) 20 "brown") 200 100))

Part C: Images and colors

Today I made a 10 by 8 image, a red rectangle called canvas and decided I wanted its corner pixels to be all blue. This is what I did in my MediaScript window:

(define canvas (image-new 10 8))
;; Now (image-show canvas)
;; and use GIMP to magnify to 800%
(image-show canvas)
(image-set-pixel! canvas 0 0 (rgb-new 0 0 255))
(image-set-pixel! canvas 0 7 (rgb-new 0 0 255))
(image-set-pixel! canvas 9 0 (rgb-new 0 0 255))
(image-set-pixel! canvas 9 7 (rgb-new 0 0 255))

I have an uneasy feeling that I will need to do something very similar every day for the rest of the semester! So I would like to generalize my steps and make a procedure that I can call any time I need to take an image and change the colors of four symmetrically arranged points within that image.

Note that I only need to specify one pixel by giving its row and column. The other three can be calculated since I know the width and height of the image. For example the horizontally symmetric point will be in the same row but its new column can be calculated by subtracting col from a number that is one less than the width of the image. I also need to parameterize the color so that I will be able to change the color of the four pixels to anything I choose. The code I wrote to generalize my steps above is:

;;; Procedure:
;;;   image-decorate!
;;; Parameters:
;;;   canvas, an image
;;;   col, a positive integer
;;;   row, a positive integer
;;;   color, an RGB color
;;; Purpose:
;;;   To recolor four symmetric points in an image
;;; Produces:
;;;   [Nothing. Called for its side-effect.]
;;; Preconditions:
;;;   0 <= row < (image-height image)
;;;   0 <= col < (image-width image)
;;; Postconditions:
;;;   Four symmetric points about (col,row) in canvas will be recolored to color.
(define image-decorate!
  (lambda (image col row color)
    (image-set-pixel! image col row color)  
    (image-set-pixel! image col (- (- (image-height image) 1) row) color)
    (image-set-pixel! image (- (- (image-width image) 1) col) row color)
    (image-set-pixel! image (- (- (image-width image) 1) col) 
                      (- (- (image-height image) 1) row) color)))

Note that we can put multiple expressions in the body of the procedure when expressing an "imperative" algorithm that is not called for its value, but called for its side-effect.

Go ahead and try (image-decorate! canvas 2 3 (rgb-new 0 255 255))

Problem 8: Generalizing code

Topics: Procedures, GIMP tools.

Now it's your turn. I'm going to give you a sequence of instructions and ask you to generalize the sequence by creating a procedure that can do exactly the same thing as the given sequence (with one assignment of values to the parameters). Other parameter values may cause the procedure to do similar actions while giving the user more options than existed in the given "pre-wired" sequence.

The following code draws a pretty good target.

(context-set-bgcolor! "white")
(define target (image-new 200 200))
(context-set-fgcolor! "red")
(image-select-ellipse! target REPLACE 0 0 200 200)
(image-select-ellipse! target SUBTRACT 30 30 140 140)
(image-select-ellipse! target ADD 60 60 80 80)
(image-select-ellipse! target SUBTRACT 90 90 20 20)
(image-fill-selection! target)
(image-select-nothing! target)
(image-show target)
;;;  Go ahead and test it to see what it does.

a. Define a procedure called image-target! that generalizes the code as much as possible. That is, you must imagine what things someone who calls your procedure might want to vary or customize.

b. Write a short statement explaining what parameters your procedure has and why. If appropriate, explain any parameters you considered but chose not to use.

Problem 9: Intensifying colors

Topics: RGB colors, procedures, documentation.

Sometimes we want to enhance images by increasing the contrast between light and dark colors multiplicatively, rather than brightening them additively. Write a color transform procedure (rgb-gain rgb-color percent) that increases the magnitude of each color channel by the specified percentage.

> (color->rgb-list "brown")
(165 42 42)
> (rgb->rgb-list (rgb-gain (color-name->rgb "brown") 50))
(247 63 63 )
> (color->rgb-list "seagreen")
(46 139 87)
> (rgb->rgb-list (rgb-gain (color-name->rgb "seagreen") 100))
(92 255 174)

Problem 10: Documenting gains

Write 6-P documentation for rgb-gain.

Some questions and answers

Here we will post answers to questions of general interest. Please check here before emailing your questions!

Problem 6

Question: I was wondering what you mean by a "pedestal box"? What exactly is that?
Answer: Simply: a rectangle. See the amended example.


Here you will find errors of spelling, grammar, and design that students have noted. Remember, each error found corresponds to a point of extra credit for everyone. We usually limit such extra credit to five points. However, if we make an astoundingly large number of errors, then we will provide more extra credit.

  • The example in Problem 6 should read (drawing-hshift red-eye 20). [JJW, 1 point]
  • The example in Problem 7 should call image-decorate!. [BW, 1 point]
  • Problem 9 had an extra definite article "the". [BW, 1 point]
  • Explaining” was misspelled in the Preliminaries. [AT, 1 point]

Jerod Weinman

Copyright 2007-2012 Janet Davis, Matthew Kluber, Samuel A. Rebelsky, and Jerod Weinman. (Selected materials copyright by John David Stone and Henry Walker and used by permission.)

This material is based upon work partially supported by the National Science Foundation under Grant No. CCLI-0633090. Any opinions, findings, and conclusions or recommendations expressed in this material are those of the author(s) and do not necessarily reflect the views of the National Science Foundation.

Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial 2.5 License .