CS 1101 (A05) Homework 2: Cond, Structs and Helpers

Due: September 6 (Tuesday) at 11:59pm via turnin (assignment name hwk2).

Assignment Goals


The Assignment

Remember to follow the Expectations on Homework when preparing your solutions, including the academic honesty policy.

Creating Helper Functions

The following code produces bar graphs for three pieces of data.

;; bar-graph : number number number -> image
;; consumes three numbers and produces bar graph of results
;;   NOTE: background image sized for inputs up to 20
(define (bar-graph num-a num-b num-c)
  (overlay/xy (overlay/xy (overlay/xy (rectangle 80 80 'solid 'tan)
                                      -25 (- 40 (* 1/2 3 num-a))
                                      (rectangle 15 (* 3 num-a) 'solid 'red))
                          0 (- 40 (* 1/2 3 num-b))
                          (rectangle 15 (* 3 num-b) 'solid 'blue))
              25 (- 40 (* 1/2 3 num-c))
              (rectangle 15 (* 3 num-c) 'solid 'green)))
  1. Copy this code to your homework file, then create a cleaner version of it using helper functions and constants. Turn in only your final version (with all helpers and constants). You do not need to include a copy of the original code. Your final version should have the same behavior as the original code (don't embellish it, just clean it up).

    Be sure to include contracts, purposes and test cases for your helper functions.

  2. Describe in a few sentences how you went about this exercise. Specifically, how did you decide when to create a helper function or constant?

Writing Scheme Programs with Cond and Structs

A company that organizes and runs conferences stores several pieces of information on the people who have registered for the conference: their name, registration category (regular, student, or member), hotel information, and whether they plan to attend the banquet dinner. Hotel information consists of the number of nights needed, what rating of hotel is requested (a number of stars from 2 to 4, inclusive), and the room type needed (double or king).

Your solutions to the following problems should use helper functions in place of repeated code (we will deduct points otherwise). You may also introduce helper functions to improve the readability of your code. Work through the reading on helper functions for examples of what we expect here.

  1. Develop a data definition for hotel information. Provide three examples of data with your solution.

  2. State all of the operators (with their contracts) that are created for your define-struct in the previous question.

  3. Develop a data definition for conference registrations. Include both the define-struct and three examples of data.

  4. Write a program registration-rate which consumes a registration category and returns the registration fee for that category. Fees are given in the following table:

    regular$400
    member$350
    student$150

  5. Write a program hotel-surcharge which consumes hotel information and produces the total charge for that hotel request (for all nights). One night starts at $109 for a double and $89 for a king at a 2-star hotel. Rates go up $30 per night for each room type for each additional star (so a 4-star king room should cost $149 a night). Compute the charges based on the formula given, rather than by explicitly coding in the rates for each room-type and star combination.

  6. Write a program fees-due which consumes a conference registration structure and produces the total fees due for the registration. The total fees should include the registration fee, hotel surcharge, and $55 if the person plans to attend the banquet.

  7. Write a program raise-stars which consumes a hotel info and a number (new number of hotel stars) and returns a hotel info. If the given number is larger than the number of hotel stars in the given hotel info, the produced hotel info has the new number of hotel stars (and all other information the same). Otherwise, all of the information in the produced hotel info should be the same as in the given hotel info.

  8. Write a program upgrade-hotel which consumes a conference registration (not a hotel info) and a number (new number of hotel stars) and returns a conference registration. The produced registration should raise the stars of the hotel in the registration according to the same rules as in the previous question. All other information should be the same between the consumed and produced registrations.

What to Turn In

Turn in a single file hwk2.ss or hwk2.scm containing all code and documentation for this assignment. Make sure that both students' names are in a comment at the top of the file.


Help! How Do I Get Started?

For the helper functions problems, read the notes on creating helpers and come to office hours if you have questions.

For the programming problems, data definitions first! Think about what data you need for this problem, which of it belongs together, and how the various forms of data should be related. Then write define-structs that capture your data. Then turn to the programs.

Post other questions to the discussion board or come to office hours.


Back to the Assignments page