Exam 1: Scheme and Image Basics


Assigned: Wednesday, 14 September 2011

Due: 11:59 p.m., Tuesday, 20 September 2011

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

While your electronic version is due at 11:59 p.m. Tuesday, 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. 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: Numbers and procedures

Topics: Numeric values, procedures.

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: Calculating a right-triangle's hypotenuse

Write a procedure, (right-triangle-hypotenuse base height), that computes the hypotenuse of a right triangle given its other two sides. You will use the Pythagorean theorem, which states that a^2 + b^2 = c^2, where a and b are the base and height of the triangle, and c is the hypotenuse.

Problem 3: Re-calculating time units

While it is sometimes useful to count or tally times in the same units (i.e., minutes), these tallies often exceed our ability to reason with them meaningfully. For instance, as the number of minutes increases past sixty, it would be much nicer to have a representation of the time in a format such as HH:MM.

a. Write a procedure (time-in-hours total-minutes) that takes a number of minutes and produces the number of (whole) hours represented by that number of minutes.

> (time-in-hours 30)
0
> (time-in-hours 90)
1
> (time-in-hours 417)
6

b. Write a procedure (time-in-minutes total-minutes) that takes a number of minutes and produces the leftover minutes corresponding to the "fractional hour" represented by that number of minutes.

> (time-in-minutes 30)
30
> (time-in-minutes 90)
30
> (time-in-minutes 417)
57

Problem 4: Reading code

Topics: Procedures, numeric values, documentation.

Consider the following procedure.

(define fun
  (lambda (n p)
    (/ (round (* n (expt 10 p))) (expt 10 p))))

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.

Part B: Image paradigms

Consider the following two images. For each image described below in problems 5 and 6, write a program (that is, a sequence of expressions) to generate the image, using either the Scheme-based GIMP tools or drawings as values. Your goals are to correctly generate the image and to make your program for generating the image as concise as possible. (Reminder: In considering conciseness, we care more about units of expression, rather than number of characters.)

Note that you may only use one of the models for each image, and you must use different models for the different images in problems 5 and 6.

Problem 5: Drawing a flag, take one

Write a sequence of instructions that creates the flag of Laos.

Problem 6: Drawing a flag, take two

Write a sequence of instructions that creates the flag of Greenland.

Problem 7: Generalizing frames

Topics: Procedures, GIMP tools, generalization.

The following code draws a brown, rectangular frame on an image called canvas (image 11, in this session).

> (context-set-fgcolor! "brown")
()
> (image-select-rectangle! canvas REPLACE 95 75 50 40)
11
> (image-select-rectangle! canvas SUBTRACT 100 80 40 30)
11
> (image-fill-selection! canvas)
> (image-select-nothing! canvas)
11

a. Define a procedure called image-draw-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. If appropriate, explain any parameters you considered but chose not to use.

Problem 8: 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-right reference abutter) where reference and abutter are both drawings. The procedure drawing-abut-right should produce a new drawing consisting of reference grouped with a version of abutter that has been translated so that its top is aligned with reference and its left edge adjoins the right edge of reference.

The example below uses drawings from the reading on procedures.

(image-show (drawing->image (drawing-abut-right (drawing-vshift red-eye 20) blue-i) 200 100))
	  

Note: The built-in MediaScheme procedures drawing-left, drawing-right, and drawing-top will be useful.

Part C: Colors

Topics: RGB colors, procedures, documentation.

Problem 9: RGB redirection

Independent light elements generate each of the red, green, and blue channels at each pixel on your monitor. Imagine some wires were crossed at the monitor factory so that the green signal got redirected to (and combined additively with) the blue channel. Write a transform procedure (rgb-redirect rgb-color) that implements this behavior.

> (rgb->rgb-list (rgb-redirect (color-name->rgb "white")))
(255 0 255)
> (color->rgb-list "brown")
(165 42 42)
> (rgb->rgb-list (rgb-redirect (color-name->rgb "brown")))
(165 0 84)
> (color->rgb-list "seagreen")
(46 139 87)
> (rgb->rgb-list (rgb-redirect (color-name->rgb "seagreen")))
(46 0 226)

Problem 10: Documenting redirection

Write 6-P documentation for rgb-redirect.

Some questions and answers

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

Problem 3

Question: Do we need to account for non-whole numbers?
Answer: No. Your minutes should be integers.

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.

  • Hypotenuse” was consistently misspelled as “hypoteneuse”. [PM, 1 point]
  • image-fill! should have been image-fill-selection!. [PM, 1 point]
  • electronic” was misspelled in the introduction. [AL, 1 point]

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 .