Homework 2:
Basic Functions in Racket

Due: Monday, January 29, at 11:59 PM

Introduction

This assignment gives you a chance to write Racket expressions that will be the body of a function.

Template Source File

Download this template file and use it as the starting point for your submission. Please name the file homework02.rkt!

homework02.rkt includes a require expression at the top. It imports the rackunit module and enables you to write test cases for your solutions [ example code ]. The template file contains several test cases for each problem.

Do Not Use...

You do not need any advanced Racket features to solve these problems, only simple expressions.

Helpful Functions

You may find these Racket primitives useful on this assignment:

You may want to use each for at least one problem.

To cause a number to evaluate as a floating-point number, you can make one of the arguments floating-point. In Problem 1, dividing by 180.0 instead of 180 will cause Racket to return a floating-point number.

Problems

  1. Write a Racket function named total-grade that takes exactly three integer arguments: a homework average, a quiz average, and a final exam average. Each argument will be in the range 0 .. 100. total-grade returns the overall grade for the course, based on the weights defined in the syllabus: 30%, 40%, and 30%, respectively. For example:
    > (total-grade  90  80  90)      ; 0.3*90 + 0.4*80 + 0.3*90
    86.0
    > (total-grade  60  50  40)
    50.0
    > (total-grade 100 100 100)      ; perfect score: 100%!
    100.0
    
    I have provided check-= expressions for these three examples in your template file.

  2. I buy soda in 12-packs of 12-ounce cans or 6-packs of 24-ounce bottles. Sometimes, stores sell 6-packs of 16.9-ounce bottles and charge the price usually associated with the larger 24-ounce bottles. I am not amused by this marketing strategy. I need a function that helps me compare prices by the ounce.

    Write a Racket function named price-per-ounce that takes three arguments: the number of units (bottles or cans) in the pack, the size of each unit in ounces, and the price for a pack. price-per-ounce returns the price of the pack per ounce. For example:
    > (price-per-ounce 6 24 1.44)     ; a 6-pack of 24-ounce bottles costs $1.44
    0.01                              ; ... which is $0.01/ounce
    
    > (price-per-ounce 6 16.9 1.44)   ; I am not amused
    0.01420118
    
    I have provided check-= expressions for these two examples in your template file.

  3. concentric rings, which look like a doughnut
    Suppose we have two concentric circles. The outer circle has a radius of ro, and the inner circle has a radius of ri. The area of the ring they form is the difference between the areas of the two circles.

    Write a Racket function named ring-area that takes two arguments, ri and ro, both in inches. The function returns the area of the ring formed by the concentric circles. For example:
    > (ring-area 1 2)
    9.42477796076938
    
    You may want to write a function to compute the area of a circle and use it to compute ring's area.

    I have provided a check-= expression in your template file for the example above, as well as two more check-= expressions to test your solution further.

  4. According to The Joy of Cooking, when you are cooking candy syrups, you should cook them 1 degree cooler than listed in the recipe for every 500 feet of elevation you are above sea level. For example, the recipe for Chocolate Carmels calls for a temperature of 244° Fahrenheit. If you were making your Chocolate Carmels in Denver, the Mile-High City, you would want to cook the syrup at 233.44°.

    Write a Racket function named candy-temperature that takes two arguments, the recipe's temperature in degrees Fahrenheit and the elevation in feet, and returns the temperature to use at that elevation. For example:
    > (candy-temperature 244 5280)    ;; Denver, baby!
    233.44
    > (candy-temperature 302 977.69)  ;; the highest point in Cedar Falls
    300.04462                         ;;     is approx. 298m above sea level
    > (candy-temperature 302 -1401)   ;; the Dead Sea 1401 ft below sea level
    304.802
    
    I have provided check-= expressions for these three examples in your template file.

  5. Generally, the dimensions of engineered components are not exactly the specified value, but rather within a certain tolerance of the specified value. The tolerance generally depends upon the application and the material being used. For example, a metal piece used in construction that is listed as 5 cm in length might actually be any length within 1 mm of 5 cm, that is, between 4.9 cm and 5.1 cm, inclusive.

    Write a Racket function named in-range? that takes three numbers as arguments: two numbers to compare, and a tolerance, epsilon. in-range? returns true if its first two arguments are within epsilon of one another, and false otherwise. For example:
    > (in-range? 4.95 5.0 0.1)
    #t
    > (in-range? 4.95 5.0 0.01)    ;; not anymore!
    #f
    > (in-range? 5.0 4.95 0.1)     ;; works both ways
    #t
    > (in-range? 5.0 5.95 0.1)
    #f
    > (in-range? 5.5 5.95 0.5)
    #t
    
    I have provided check-true and check-false expressions for all of these examples in your template file.

Deliverables

Use Save Definitions to save the file of function definitions you create using the template you downloaded. It will have the file extension rkt. Be sure to use the specified name for your file! This enables the autograder to find and run your code.

By the due time and date, use the course submission system to submit the following files electronically:

No hard copy is required.

Be sure that your submission follows the submission requirements.