Pizza Pi
This programming challenge is from an initial C++ programming course I took in Fall 2007. The Chapter (3) was titled “Expressions and Interactivity”.
I post these old academic challenges for a few reasons. One is to demonstrate my programming learning, experience, and progression. Another reason is to make sure my code and solutions are indexed by search engines, so that other beginning programmers may get help if they need it.
3.18: Joe’s Pizza Palace needs a program to calculate the number of slices a pizza of any size can be divided into. The program should perform the following steps:
A) Ask the user for the diameter of the pizza in inches.
B) Calculate the number of slices that may be taken form a pizza of that size.
C) Display a message telling the number of slices.To calculate the number of slices that may be taken from the pizza, you must know the following facts:
- Each slice should have an area of 14.125 inches.
- To calculate the number of slices, simply divide the area of the pizza by 14.125.
- The area of the pizza is calculated with this formula: Area = pi * r ^ 2, where pi is 3.14159 and r is the variable pizza radius. Divide your diameter by 2 to get the radius.
Use a named constant for pi.
My solution:
/* Chapter 3 Challenge 18
Written by Kirk Hingsberger
September 19, 2007
Determine Pizza Pi
*/
#include
using namespace std;
#include
int main()
{
const float PI = 3.14159;
const float SLICE_SIZE = 14.125;
float rad, totalArea, dia, slices;
cout << "Joe's Pizza Palace requires " << SLICE_SIZE;
cout << " square inches per slice." << endl;
cout << "What is the diameter of your pizza?" << endl;
cin >> dia;
rad = dia / 2;
totalArea = PI * pow(rad,2);
slices = totalArea / SLICE_SIZE;
cout << "You should divide that pizza into " << slices << " slices." << endl;
return 0;
}
Here is the capture script I turned in for grading:
Script started on Thu 20 Sep 2007 10:11:35 AM MDT
$ cat chap3chlg18.cc
/* Chapter 3 Challenge 18
Written by Kirk Hingsberger
September 19, 2007
Determine Pizza Pi
*/
#include
using namespace std;
#include
int main()
{
const float PI = 3.14159;
const float SLICE_SIZE = 14.125;
float rad, totalArea, dia, slices;
cout << "Joe's Pizza Palace requires " << SLICE_SIZE;
cout << " square inches per slice." << endl;
cout << "What is the diameter of your pizza?" << endl;
cin >> dia;
rad = dia / 2;
totalArea = PI * pow(rad,2);
slices = totalArea / SLICE_SIZE;
cout << “You should divide that pizza into ” << slices << ” slices.” << endl;
return 0;
}
$ g++ -o chap3chlg18 chap3chlg18.cc -s
$ chap3chlg18
Joe’s Pizza Palace requires 14.125 square inches per slice.
What is the diameter of your pizza?
12
You should divide that pizza into 8.00688 slices.
$ exit
exit
Script done on Thu 20 Sep 2007 10:12:07 AM MDT










