C211 Lab 2: ACME Sports Drinks

Help the company figure out if it can ship all of the drink it makes each day!


Summary

In this lab, you will calculate how many cans, boxes, and trucks ACME will need in order to ship all of its drink each day. If at any time you have any questions, please raise your hand or ask your neighbor.

Step 1: volume->cans

You should not have SWL open yet, in fact, this is the only thing that should be up on your computer screen.
Write a procedure that calculates the number of cans needed to ship a volume of drink given to you.
  1. Define cyl-vol (the procedure I just made in class). It should start with:
      (define cyl-vol
              (lambda (rad ht)
                      ...))
      
    HINT: copy it from the board!
     
  2. Write everything you're doing into the lab book. You don't have to write a book, just record your steps.
     
  3. Write a procedure that calculates the number of cans needed and returns the value it calculated. It should use cyl-vol and begin:
      (define vol->cans
            (lambda (vol can-rad can-ht)
                    ...))
      
    Fill in the "..." with code that uses the cyl-vol procedure to calculate the volume that will fit in each can, then figures out how many cans it will need. can-rad & can-ht are the radius and height of a can (respectively).
     
    The volume will most likely not fit in an exact amount of cans; there will be some left over that must be put into a can to partially fill it. So you could end up with 43 full cans and one that's only half full -- you still need 44 cans.
     
    HINT: there's a procedure in Scheme called ceiling. It rounds any non-whole number up. For example, 3.2 becomes 4.0 and 3.6 becomes 4.0.
     
  4. Think up and write down at least five test cases (think about possible can shapes and volumes that might be different than the rest).
    Here's a sample test case:
      > (vol->cans 1400 2 4)
      28.0
      
    As you can see, this means that 28 cans of radius 2 and height 4 are needed to pack 1400 units of Sports Drink.
    Put these test cases into your lab book and explain the expected result.
     
    Once you've got code written in your lab book and some cases you can use to test it (as well as full english sentences explaining briefly what you have to do), put your code into SWL and test it. Document in your lab book how each test turns out, and then any debugging you do as well.
     

Step 2: cans->boxes

Once you've finished vol->cans, this should be cake. Now you must write a procedure that returns the number of boxes needed to pack a given amount of cans.
  1. Define cans->boxes. It should begin like:
      (define cans->boxes
            (lambda (cans box-capacity)
                    ...))
      
    The parameter box-capacity represents the number of cans that fit in one box.
     
    Do this step much like the first, writing your code in the lab book first, and then desiging test cases. Once you've got those, put your code into SWL and try it out. Document all your steps and the results of what you do and change.

Step 3: boxes->trucks

This step is almost EXACTLY like step 2. Now you must write a procedure that returns the number of trucks needed to ship a given amount of boxes.
  1. Define boxes->trucks. It should begin like:
      (define boxes->trucks
            (lambda (boxes truck-capacity)
                    ...))
      
    NOTE: Remember ceiling? Recall also that even if there is one box more than it takes to fill a truck, a second truck must be used. If there are (+ 1 truck-capacity) boxes, it takes two trucks to move the whole lot!
     

Step 4: the fun part!

Now, you get to compile all these pieces into one! Write a procedure called volume->trucks that takes as parameters: and returns the number of trucks that are needed to ship that volume.
 
ASSUME from now on in this lab that the RADIUS OF A CAN IS 2, the HEIGHT OF A CAN IS 6, the CAPACITY OF A BOX IS 24 CANS, and THE CAPACITY OF A TRUCK IS 1000 BOXES.
 
HINT: Use the three procedures you've defined already.
 

Step 4.1: rewrite

Rewrite the procedure (this time call it volume->trucks-noprocs) in step 4 so that the only parameter it takes is volume Then answer these questions in your lab book:
 

Step 5: Make it useful!

Each day, a different number of truck drivers show up for work. Write a predicate procedure enough-drivers? without using the if keyword that returns #t if there are enough truckers and #f if there aren't. Your procedure should start like this:
(define enough-drivers?
      (lambda (num-drivers vol)
              ...))
Feel free to use either procedure from steps 4 or 4.1, but be sure to explain why you picked the one you did.
 

Challenge Step: Side effects and the if keyword

This step is a little harder, but if you can do this you can fly through the homework!
 
Write a new procedure that uses the enough-drivers? predicate and based on the results either prints out:
Today we have enough trucks to ship ## units of Sports Drink!
(Where the ## is the actual volume of Sports Drink)
or
We need ## more drivers to ship today's volume of Sports Drink.
(Where the ## here is the additional number of drivers needed to complete the shipment)

 
HINT: Use if and vol->trucks...
Solutions are available here
sstamm@cs.indiana.edu