In: Computer Science
C++
Part 2 - Serving Size
Part 3 - Supplementing the Budget (
Part 4 - The "Magic" of Avoiding Magic Numbers
Sometime it may feel like a nuisance to make sure to properly use constants for magic numbers, so here we give you the opportunity to see how useful it can be. By changing just some of the constants, your program can simulate any pizza sizes and prices, without having to touch any other part of your code. Those constants are how many people each pizza feeds, the dimensions of each pizza, and the cost of each pizza. Make sure that you have written your code so that by changing only those nine values, and re-compiling, you can simulate any arbitrary pizza values.
Since you will be changing constants and recompiling for Part 4, you will be submitting a different compiled program than used in Parts 1, 2, and 3. Thus, do not submit the updated program here. Instead, copy your program from here into the following zyBook section (Main Lab 2B), change 9 of your constants, and submit the program from there. The grading TA will combine the points from the auto-grade of both 2A (possible 85 points) and 2B (possible 15 points) to get your full auto-grade points. The new values you will use are given in section Main Lab 2B.
Sample Input
303
15
Sample Output
In this case the output would look like:
Please enter the number of guests: 43 large pizzas, 0 medium pizzas, and 2 small pizzas will be needed. A total of 13735 square inches of pizza will be purchased (45.3302 per guest). Please enter the tip as a percentage (i.e. 10 means 10%): The total cost of the event will be: $743
FOR Part-1 Part-2 And Part-3:
EXPLANATION: Modular division is used to calculate the remaining guests
Division is used to get the number of the pizzas that can be ordered based on its feeding capacity
Source Code :
#include <iostream>
#include<cmath>
//for round function
using namespace std;
void calculatePizza(int noOfGuests);
void calculatePizza(int noOfGuests){
//Declare the costs of each sized pizza
double
largeCost=30,mediumCost=20,smallCost=10,guests=noOfGuests;
//number of large count pizzas
int largeCount=noOfGuests/7;
noOfGuests%=7;
//number of medium count pizzas
int mediumCount=noOfGuests/3;
noOfGuests%=3;
//number of smallcount pizas
int smallCount=noOfGuests;
//Find the cost of pizzas
double
cost=largeCount*largeCost+mediumCount*mediumCost+smallCost*smallCount;
//Assuming the size of large size pizza is 30 , medium
size pizza is 20 small size pizza is 10 sqinches
double
size=largeCount*30+mediumCount*20+smallCount*10;
cout<<largeCount<<" Large Pizzas is,
"<<mediumCount<<" medium Pizzas,
"<<smallCount<<" small pizzas will be needed";
cout<<"A total of "<<size<< " square
inches of pizza will be purchased
("<<size/guests<<")";
int tipPercent;
cout<<"Please enter the tip as a percentage :
";
cin>>tipPercent;
double tip= round(cost*(tipPercent/100));
cout<<"The total cost of the event will be:
$"<<cost+tip;
}
int main()
{
cout<<"Please enter the number of guests: ";
int noOfGuests;
cin>>noOfGuests;
calculatePizza(noOfGuests);
}
Pizza Source Code Image
OUTPUT:
// Output is based on the price and size of which i have taken
PART_4:
Explanation: In the previous part we have taken all the things in a static way while calculating in this we declaring the variables for every size, feeding count, cost of pizza by changing the data for these values can lead to being desired output for the defined pizza options
SOURCE CODE:
#include <iostream>
#include<cmath>
using namespace std;
void calculatePizza(int noOfGuests);
void calculatePizza(int noOfGuests){
//Declare the costs of each sized pizza
double
largeCost=30,mediumCost=20,smallCost=10,guests=noOfGuests,sizeLarge=30,sizeMedium=20,sizeSmall=10;
int feedLarge=5,feedMedium=3,feedSmall=1;
//number of large count pizzas
int largeCount=noOfGuests/feedLarge;
noOfGuests%=feedLarge;
//number of medium count pizzas
int mediumCount=noOfGuests/feedMedium;
noOfGuests%=feedMedium;
//number of smallcount pizas
int smallCount=noOfGuests;
//Find the cost of pizzas
double
cost=largeCount*largeCost+mediumCount*mediumCost+smallCost*smallCount;
//Assuming the size of large size pizza is 30 , medium
size pizza is 20 small size pizza is 10 sqinches
double
size=largeCount*sizeLarge+mediumCount*sizeMedium+smallCount*sizeSmall;
cout<<largeCount<<" Large Pizzas is,
"<<mediumCount<<" medium Pizzas,
"<<smallCount<<" small pizzas will be needed";
cout<<"A total of "<<size<< " square
inches of pizza will be purchased
("<<size/guests<<")";
int tipPercent;
cout<<"Please enter the tip as a percentage :
";
cin>>tipPercent;
double tip= round(cost*(tipPercent/100));
cout<<"The total cost of the event will be:
$"<<cost+tip;
}
int main()
{
cout<<"Please enter the number of guests: ";
int noOfGuests;
cin>>noOfGuests;
calculatePizza(noOfGuests);
}
SOURCE CODE IMAGE:
OUTPUT: