Exam 1: Scheme and Image Basics


Assigned: Wednesday, 9 February 2010

Due: Beginning of class, Wednesday, 16 February 2010

Preliminaries

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. Since 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 Davis.

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.

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. 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.

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.

Problems

Part A: Fun with Numbers

Topics: Numeric functions, reading code, procedures, documentation.

Problem 1: Reading Code

Consider the following procedure.

(define fun
  (lambda (n i)
    (quotient (remainder n (expt 10 (+ i 1))) (expt 10 i))))

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.

a. In your own words, describe what fun computes.

b. In your own words, explain how fun works.

Problem 2: Calculating Tips

Suppose we make it our policy to give a tip of as close to 17% as possible on any meal. We will likely find it useful to write a procedure that computes the tip for us.

Write a procedure, (tip cost), that computes the tip and presents it in a reasonable form.

> (tip 100)
17.0
> (tip 99)
16.83
> (tip 0.99)
0.17
> (* .17 .99)
0.1683

As the last example suggests, you'll need to do a bit more than just multiply by 17%. Hint: The round procedure is likely to be helpful.

Problem 3: Documenting round-to

In the previous problem, you rounded a real number to two digits after the decimal place. While such rounding is useful, it is even more useful to let the client of your procedure choose how many digits after the decimal point to use.

Write the documentation in six Ps format for a procedure, (round-to number places), that rounds number to places digits after the decimal point. For example:

> (round-to (* 0.17 0.99) 2)
0.17
> (round-to (exact->inexact 1/3) 4)
0.3333
> (round-to pi 4)
3.1416
> (round-to (sqrt 2) 3)
1.414

Problem 4: Implementing round-to

Implement the procedure round-to.

As you write round-to, you may find the expt procedure useful. (expt b p) computes bp.

Part B: Scripting the GIMP Tools

Topics: GIMP tools, procedures, generalization, documentation.

Problem 5: Making a Flag with GIMP Tools

Write a sequence of instructions using GIMP tools that create the flag of Aruba.

Note that to create the star, you will need to use a polygonal selection. The MediaScheme procedure image-select-polygon! allows you to do this by giving it a sequence of positions, which are created with the procedure (position-new col row). For example, here is a sequence of instructions that adds a triangle to an image.

  (context-set-bgcolor! "red")
  (define canvas (image-new 100 100))
  (image-select-polygon! canvas REPLACE
    (position-new 50 0)
    (position-new 0 75)
    (position-new 75 75))
  (context-set-fgcolor! "black")
  (image-fill! canvas)

Problem 6: I Was Framed!

The following code draws a white frame around a 100x100 image named picture.

  (context-set-fgcolor! "white")
  (image-select-rectangle! picture REPLACE 0 0 100 100)
  (image-select-rectangle! picture SUBTRACT 10 10 80 80)
  (image-fill-selection! picture)
  (image-select-nothing! picture)
  (context-update-displays!)

a. Define a procedure named image-frame! 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. In addition, explain parameters you chose not to use.

Problem 7: Documenting Your Frame Procedure

Using the six Ps, document the image-frame! procedure that you wrote for the previous problem.

Part C: Drawings as Values

Topics: Drawings as values, procedures.

Problem 8: The Center of a Drawing

Additional topics: Numeric functions.

The procedure drawing-left finds the left edge of a drawing and the procedure drawing-width finds the width of a drawing. Similarly, the procedure drawing-top finds the top edge of a drawing and the procedure drawing-height finds the height of a drawing.

Write two procedures, drawing-center-x and drawing-center-y, that find the x- and y-coordinates of the center of a drawing.

Problem 9: Another Way to Scale Drawings

Some of you have criticized the drawing-scale procedure because it scales not only the size of the drawing, but also the position of the center of the drawing. How can we solve that problem? For simple drawings (that is, just a rectangle or just an ellipse), the strategy is pretty simple: Shift the center of the drawing to (0,0), scale the drawing, and then shift the center back to its old place.

Implement a procedure, (drawing-resize drawing factor) that resizes a drawing using this strategy. You can assume that you have working drawing-center-x and drawing-center-y procedures.

Problem 10: Outlining Drawings

Implement a procedure, (drawing-outline drawing color) that simulates an outline around drawing by placing a copy of drawing behind the original that is resized to be 25% larger and recolored to color.

You can assume you have a working drawing-resize procedure. For example, using the values purple-ellipse and blue-i from the reading on writing procedures:

(image-show (drawing->image (drawing-outline purple-ellipse "red") 100 100))
(image-show (drawing->image (drawing-outline blue-i "grey") 100 100))

Some questions and answers

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

Errata

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.


    Jerod Weinman

    Copyright © 2007-2011 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 .