CS 210 – Fundamentals
of Programming I
Spring 2007 – In-class Exercise for 2/1/07
Name(s):
(15 points) Complete this exercise in pairs. Write the answers to the written part in this assignment sheet and turn it in with a print out the program when you are done. The purpose of this exercise is to continue working with functions, including overloaded functions. It is based on the Pizza Buying Case Study on p. 209 of the textbook that was covered in lecture today.
Problem Statement
The large "economy" size of an item is not always a better buy than the smaller size. This is particularly true when buying pizzas. Pizza sizes are given as the diameter of the pizza in inches. However, the quantity of pizza is determined by the area of the pizza and the area is not proportional to the diameter. Most people cannot easily estimate the difference in area between a ten-inch pizza and a twelve-inch pizza, and so cannot easily determine which size is the best buy - that is, which size has the lowest price per square inch. Write a program that compares two sizes of pizza to determine which is the better buy.
Analysis & Design of Program
Completed during lecture portion of class.
Assignment
0. Create a Visual C++ project on your network drive. DO NOT create a source file. Download a copy of inclass07.cpp from the course website under today's date into your project folder. Add the file to your project. This program is incomplete, so it will not build.
1. (6 points) Complete this program using the analysis and design covered in lecture today. That is, you must provide:
A prototype for function ComputeUnitPrice declaring this function to the compiler
Function calls to ComputeUnitPrice in the main program to compute the cost per square inch for each size pizza
A definition for function ComputeUnitPrice implementing the function.
Build and test your program until you a confident that it is correct.
2. (4 points) Answer the following questions regarding this program.
a. What is the prototype of the function ComputeUnitPrice?
b. How many function calls are there in this program? What are they?
c. With respect to ComputeUnitPrice, which variables are (actual) arguments?
d. With respect to ComputeUnitPrice, which variables are (formal) parameters?
3. (5 points) Let the instructor know when you have completed the first two parts. For the third part, you will create a function to compute the unit price for a rectangular pizza, but please wait until the topic of overloaded functions has been covered in lecture.
After overloaded functions has been covered in lecture, add the following to the program:
A function prototype for the ComputeUnitPrice function for rectangular pizzas
Code in the main program to read in the length, width, and price of a rectangular pizza
Code in the main program to compute the unit price of a rectangular pizza using ComputeUnitPrice
A function definition for the ComputeUnitPrice function for rectangular pizzas
Code in the main program to determine whether the rectangular pizza is a better buy than both of the round pizzas. If it is, then the program should display an additional message saying so. If it is not, there should not be any additional output.
When you have completed this exercise, print out your program file and turn it in with one copy of this exercise sheet with your answers to the questions.
01/31/07