Exercise 11: Best Pizza Value

For this assignment you will finish writing a program that asks for the size and price of several pizzas, and prints the pizza that is the best value. The pizza that is the best value is the one that has the lowest price per square inch.

The portion of the program that asks for user input and prints the result is already written for you. You will need to implement 3 functions in pizza.c to finish the functionality of the program.

Code Organization

There is a Makefile provided with this assignment, so you can compile the project by running make. This produces the executable best_pizza_value which you can run with ./best_pizza_value.

The structure used in this assignment is struct pizza. This structure is defined as follows:

struct pizza {
    unsigned int size;
    double price;
};

As mentioned in the last assignment dealing with money, double is in general not a great type to use for monetary values, but it will work for our purposes here.

main() builds an array of struct pizza instances, and passes it to best_value_pizza(). The prototype and documentation for this function are in pizza.h, you must implement the function in pizza.c.

In addition to best_value_pizza(), you must implement two helper functions: circle_area() and price_per_square_inch(). These will be very simple functions. The purpose of writing these functions is for organization. By moving these calculations into their own functions, best_value_pizza() will be less cluttered with calculation details. Your best_value_pizza() function must use price_per_square_inch() when figuring out how good of a value the pizza is, and price_per_square_inch() must use circle_area() when calculating the price per square inch.

In circle_area(), use the M_PI constant as the value of pi. This is defined in math.h, which is already included for you in pizza.h. Note that M_PI is not part of the C standard, but is an extension added by GCC. This is why the Makefile for this project uses -std=gnu99 instead of -std=c99.

Notice that circle_area() takes the radius of a circle as an argument, while the diameter is stored in struct pizza. This is because the area of a circle is typically expressed using the radius, but pizzas are usually marketed using the diameter of the pizza.

In best_value_pizza() you need to keep track of the pizza with the lowest price per square inch as you loop through the pizzas. To avoid re-calculating the price per square inch of the best pizza so far in every iteration of the loop, it is a good idea to have another variable to keep track of the price per square inch of the best pizza so far.

Testing

You can run the GitHub tests with the command:

make gh-test-pizza

These tests will tell you explicitly when the tests for each function pass, and if any fail details on what the input, output, and expected output should be.

Submission

Push your modified pizza.c to GitHub. All 3 of your functions will be tested.

Grading

Your grade for this assignment will be out out of 10 points: