YALE UNIVERSITY
DEPARTMENT OF COMPUTER SCIENCE
| CPSC 223b: Data Structures and Programming Techniques | Handout #3 (v. 2) | |
| Professor M. J. Fischer | January 22, 2008 | |
Problem Set 2
Due before midnight on Tuesday, January 29, 2008.
Wedding Bells Bakery is in the business of baking wedding cakes. It sells two styles of cakes, round and square. Each style can be made in various sizes in order to accommodate the anticipated number of guests.
Customers generally have no idea how big a cake is needed or which shape would better accommodate their needs. You are to write a program to help Wedding Bells Bakery assist their customers in choosing a cake. In particular, the customer will say how many guests are expected and what size portions they want to serve. The program will find the smallest standard round and the smallest standard square cake that accommodate the customer’s needs. For each cake, it will print out the size, number of servings, and cost.
WB cakes are made from stacks of successive layers of a given shape. Each layer is 1.5 inch thick. The smallest round layer is 6 inches in diameter. The smallest square layer is 6 inches on a side. Each successive layer is 2 inches larger than the previous. Thus, a 12 inch round cake would have 4 layers of sizes 6, 8, 10, and 12, respectively. Square cakes are similar, except that each layer is square. Round layers and square layers are never mixed in the same cake.
Each serving is cut from a single layer in the shape of a right prism whose base is a simple closed curve. Since all layers are the same thickness, the volume of a serving (in cubic inches) is proportional to the area of the enclosed surface (in square inches), so we talk about serving sizes in terms of the surface area of the piece rather than its volume.
The area of a round layer of diameter d is πd2∕4, and the area of a square layer of side length s is s2. There are many ways to obtain the value of π. One simple one is to use the fact that cos(π∕2) = 0; hence π = 2cos-1(0), where cos-1 is the arccosine function.
If the desired serving size is s and the surface area of a layer is A, we assume that layer can be cut into k = ⌊A∕s⌋ pieces, each of size s (although the shapes may vary).1 Any remaining partial serving is discarded.
The number of servings in a multilayer cake is just the sum of the number of servings that can be obtained from each layer.
The cost of a layer is $2 plus $0.50 per square inch of cake for a round cake, and $2 plus $0.55 per square inch for a square cake (higher because square cakes are more difficult to remove from the cake pans cleanly). The cost of a cake is the total cost of its layers. The final cost of the cake is rounded to the nearest dollar.
Your program should read two numbers from the user: the number of guests (a non-negative integer) and the desired serving size (a real number in the interval [1.0,8.0]). The program should check each input for validity and prompt the user to re-enter the value in case of error.
The number of guests should be stored in a variable of type int. The desired serving size should be stored in a variable of type double.
For each type of cake (round and square), the program should find the smallest sized cake that can serve all of the guests, and it should calculate how many servings that cake will yield and what it will cost. Finally, the program should print the following output:
Your program will need some functions from the C math library: acos(), floor() and round(). To use them, you must include math.h in your source file, and you must give the switch -lm (that’s ℓ, not the digit 1) to the linker.
A sample transcript of a run of the program is shown in Figure 1.
You should construct a sequence of test cases that give some confidence in the correctness of your program. In particular, your test cases should:
You should prepare a simple Makefile for compiling and linking your program with the required compiler switches as given in PS1. (Note that the -I switch is not needed for this assignment, but the others are.) To ease grading, your source file should be named cake.c and the executable named cake.
You should submit the source code files, Makefile, and a text file that gives your tests and the output of your program on each test. All submitted files should have your name, the name of this course (CPSC 223b), the term (Spring 2008), and the assignment number (Problem Set 2) included in a comment at the top.