A containing tank is to be constructed that will hold some given
amount of oil when filled. The shape of the tank is to be a rectangular prism (including a base)
surmounted by a rectangular pyramid. The pyramid's height is always equal to half the length of
the prism. The cost to construct the prism portion of the tank is $300 per square meter, less a
10% discount for prism surface area in excess of 300 square meters and a 15% discount for
prism surface area in excess of 500 square meters, while the cost for the pyramid portion is $400
per square meter, less a 20% discount for pyramid surface area in excess of 150 square meters
and a 40% discount for pyramid surface area in excess of 300 square meters.Write a program that, for a given tank volume and a series of prism heights, finds the optimal prism length and width (to the closest 0.1 meters) that minimizes cost for that prism height.
Here is the sample input/output:
Enter the minimum prism height (decimal number, meters) >>> 11.0
Enter the maximum prism height (decimal number, meters) >>> 11.5
Enter the tank volume (integer, cubic meters) >>> 4000
Prism Height (m) Prism Length (m) Prism Width (m) Minimum Cost ($)
---------------------------------------------------------------------------
11.0 17.0 17.0 415156.35
11.1 17.0 16.9 415142.54
11.2 16.9 16.9 415135.85
11.3 16.8 16.9 415138.10
11.4 16.8 16.8 415146.58
11.5 16.7 16.8 415162.07
The minimum cost is $415135.85.
It is achieved with a prism height of 11.2, a length of 16.9 and a width of
16.9.
Here is my code:
class Cost
{
public:
int Cost_prism_initial;
int Cost_pyr_initial;
Cost()
{
Cost_prism_initial = 300;// Initialize the class objects.
Cost_pyr_initial = 400;
}
};
// Class for storing the optimal values.
class OptimalValues
{
public:
double optLength;
double optWidth;
double Min;
OptimalValues()// Initialize class objects.
{
optLength = 999999999;
optWidth = 99999999999;
Min = 99999999999;
}
};
// Prototype the function.
OptimalValues computeOptimalValues(double);
// Declare the objects.
double SA_prism,SA_pyramid,Cost_prism,Cost_pyramid,Cost_pyr_discount1,
Cost_pyr_discount2,Cost_prism_discount1,Cost_prism_discount2,Width,
Length,Height=0.0,minHeight,maxHeight,BestoptMinLength,BestHeight,BestoptMinWidth;
int Volume;
double optMin=99999999999999;
// Start the main function.
int main()
{
// Enter the max and min heights.
cout << "Enter the minimum prism height (decimal number, meters) >>> ";
cin >> minHeight;
cout << "Enter the maximum prism height (decimal number, meters) >>> ";
cin >> maxHeight;
// Enter the volume.
cout << "Enter the tank volume (integer, cubic meters) >>> ";
cin >> Volume;
// Print the header of the table.
cout << " Prism Height (m) Prism Length (m) Prism Width (m) Minimum Cost ($)" << endl;
cout << "---------------------------------------------------------------------------" << endl;
// For loop to compute the values at each height.
for(Height = minHeight; Height<=maxHeight; Height+=0.1)
{
// Instantiate the class in the main fuction.
OptimalValues InMain;
InMain = computeOptimalValues(Height);
double MinLength = InMain.optLength;
double MinWidth = InMain.optWidth;
double Min = InMain.Min;
// Print height, length, width, and cost in a table.
cout << setiosflags(ios::fixed) << setprecision(1) << " " << Height << " "
<< setiosflags(ios::fixed) << setprecision(1) << MinLength << " "
<< setiosflags(ios::fixed) << setprecision(1) << MinWidth << " " << setiosflags(ios::fixed)
<< setprecision(2) << Min << endl;
cout << endl;
// Compute the absolute min price.
if(Min < optMin)
{
optMin = Min;
BestoptMinLength = MinLength;
BestoptMinWidth = MinWidth;
BestHeight = Height;
}
}
// Print the absolute min cost and the size at which it occurs.
cout << "The minimum cost is $" << setiosflags(ios::fixed) << setprecision(2) << optMin << "." << endl;
cout << "It is achieved with a prism height of " << setiosflags(ios::fixed) << setprecision(1) << BestHeight
<< ", a length of " << setiosflags(ios::fixed) << setprecision(1) << BestoptMinLength << " and a width of " <<endl;
cout << setiosflags(ios::fixed) << setprecision(1) << BestoptMinWidth << "." << endl;
return 0;// End program
}
// Function for computing the optimal values.
OptimalValues computeOptimalValues(double Height)
{
double Cost_total=0.0;
// Instantiate the classes.
Cost N1;
OptimalValues InFunction;
for(Length = 2.0; Length<=40.0; Length+=0.1)
{
// Calculate width.
Width = Volume/(((Length*Length)/6.0)+(Height*Length));
double Width_round = static_cast<double>(Width*10 + 0.5)/10;
// Calculate surface area of the prism.
SA_prism = (Length*Width_round) + (2*(Height*Length)) + (2*(Height*Width_round));
// Calculate the surface area of the pyramid.
SA_pyramid = (Length*(sqrt(((Width_round/2)*(Width_round/2)) + ((Length/2)*(Length/2))))) + (Width_round*(sqrt(((Length/2)*(Length/2)) + ((Length/2)*(Length/2)))));
// If/else structure for claculation of cost of the prism.
if (SA_prism <= 300)
{
Cost_prism = (SA_prism)*(N1.Cost_prism_initial);
}
else
{
if (SA_prism > 300 && SA_prism <= 500)
{
Cost_prism_discount1 = ((SA_prism - 300)*((0.9)*(N1.Cost_prism_initial)));
Cost_prism = 90000 + Cost_prism_discount1;
}
else
{
Cost_prism_discount2 = ((SA_prism - 500)*((0.85)*(N1.Cost_prism_initial)));
Cost_prism = 143730 + Cost_prism_discount2;
}
}
// If/else structure for calculation of pyramid cost.
if (SA_pyramid <= 150)
{
Cost_pyramid = (SA_pyramid)*(N1.Cost_pyr_initial);
}
else
{
if (SA_pyramid > 150 && SA_pyramid <= 300)
{
Cost_pyr_discount1 = ((SA_pyramid - 150)*((0.80)*(N1.Cost_pyr_initial)));
Cost_pyramid = 60000 + Cost_pyr_discount1;
}
else
{
Cost_pyr_discount2 = ((SA_pyramid - 300)*(0.60*N1.Cost_pyr_initial));
Cost_pyramid = 107680 + Cost_pyr_discount2;
}
}
// Calculation of total cost.
Cost_total = Cost_prism + Cost_pyramid;
if(Cost_total < InFunction.Min)
{
InFunction.Min = Cost_total;
}
InFunction.optLength = Length - 0.1;
InFunction.optWidth = Width + 0.1;
if(InFunction.Min<Cost_total)
break;
}
return InFunction;// Return the values to the main function.
}

New Topic/Question
Reply



MultiQuote





|