CS 1101 - C-term 10

Homework 1 - Composing Functions

Due: Wednesday, January 20 at 5pm

Read the expectations on homework. You will be doing this homework individually.

Assignment Goals


The Assignment

In Scheme, we write larger programs and functions by combining the results of smaller ones. This set of questions helps you practice combining programs.

  1. Start with this DrScheme file for HW1 that contains images of different kinds of foods (right-click on the link and save the file on your computer. Name the file according to the naming conventions for Homework 1.) Write your name and login name as a comment at the top of the file. Add the image teachpack to your program, and save the program

    As you answer each of the remaining problems, use comments in your file to clearly indicate the number of the problem you are solving.

  2. Define constants that represent the price of each food item, according to this price list:
    hot dog   2.95
    taco      1.50
    fries     1.50
    burger    3.95
    salad     5.00
    pizza    12.00
    

  3. You and your friends decide to order a pizza and share the cost according to how many slices each person eats. The pizza is cut into 8 slices. Write a Scheme function called share-pizza-cost that consumes the number of slices eaten by one person and determines that person's share of the cost of a pizza. For this and all remaining problems, make sure you write a contract and purpose for each function.

  4. Define a Scheme function called price-with-tip that will add a 15% tip to a given number. Write a contract and a purpose for your function.

  5. Define a function called share-with-tip that consumes the number of slices of pizza eaten by one person and computes that person's share of the cost of a pizza, including the cost of the tip.

  6. Write a function add-side that consumes two images (for a meal item and a side-dish item), and produces an image similar to this, picturing both the meal item and the side item:


    If you don't know how to do this problem, then you need to go back and do all the problems in Homework 0. Don't forget to document the function with a contract and purpose. Show two test cases for your function.

  7. A waiter working at the pizza shop can stack two items in his arm when he delivers an order to a table. Of course, he knows how to balance the items so they don't fall over. Write a function stack-two that consumes images of two items and returns an image with the second item just above the first one. To ensure that the stack of items is balanced, you should center the items one above the other, like this:

    The images must be centered regardless of their sizes (in other words, don't assume that the images supplied as inputs to the function are of any particular height or width).

    [HINT: It's difficult to know if your function is working correctly if you use only the images supplied in this assignment, since they are all of similar width and height. I suggest testing your function by stacking (and centering) two images of your own creation of obviously different dimensions.]

    Note that Scheme has a function max that consumes two numbers and produces the larger of the numbers. You should also be using the image-width and image-height functions provided with the image.ss teachpack.

    Is your function getting too complicated? Should you decompose the problem into multiple functions?

You will be able to complete the remaining problems after we cover the cond statement in Tuesday's lecture.

  1. Write a Scheme function called food-cost that consumes a string that represents the name of a food item, and produces the cost of that food item.

  2. Write a Scheme function called number-I-can-buy which consumes a number representing an amount of money, and a string that represents the name of a food item, and returns the number of food items of that type that can be purchased with the given amount of money. The DrScheme function floor truncates a real number.

What to Turn In

Using web-based turnin, turn in a single file containing all code and documentation for this assignment. Name your file according the the naming conventions for Homework 1. Your name must be listed in a comment at the top of your file. Files submitted after 5pm on Wednesday, January 20 will be considered late, and will be subject to the late homework policy.