C++ You have been given a flat cardboard of area, say, 70 square inches to make
ID: 3825881 • Letter: C
Question
C++
You have been given a flat cardboard of area, say, 70 square inches to make an open box by cutting a square from each corner and folding the sides. Your objective is to determine the dimensions, that is, the length and width, and the side of the square to be cut from the corners so that the resulting box is of maximum length.
Write a program that prompts the user to enter the area of the flat cardboard.
The program then outputs the length and width of the cardboard and the
length of the side of the square to be cut from the corner so that the resulting
box is of maximum volume. Calculate your answer to three decimal places.
Your program must contain a function that takes as input the length and width
of the cardboard and returns the side of the square that should be cut to
maximize the volume. The function also returns the maximum volume.
Explanation / Answer
// C++ code
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
double min(double,double);
void max(double,double,double&,double&);
int main()
{double area,length,width=.001,vol,height,maxLen,mWidth,maxHeight,maxVolume=-1;
cout<<setprecision(3)<<fixed<<showpoint;
cout<<"Enter the area of the flat cardboard: ";
cin>>area;
while(width<=area)
{length=area/width;
max(length,width,vol,height);
if(vol>maxVolume)
{maxLen=length;
mWidth=width;
maxHeight=height;
maxVolume=vol;
}
width+=.001;
}
cout<<"dimensions of card to maximize the cardboard box which has a volume "
<<maxVolume<<endl;
cout<<"Length: "<<maxLen<<" Width: "<<maxLen<<endl;
cout<<"dimensions of the cardboard box ";
cout<<"Length: "<<maxLen-2*maxHeight<<" Width: "
<<mWidth-2*maxHeight<<" Height: "<<maxHeight<<endl;
return 0;
}
void max(double l,double w,double& max, double& maxside)
{double vol,ht;
maxside=min(l,w);
ht=.001;
max=-1;
while(maxside>ht*2)
{vol=(l-ht*2)*(w-ht*2)*ht;
if(vol>max)
{max=vol;
maxside=ht;
}
ht+=.001;
}
}
double min(double l,double w)
{if(l<w)
return l;
return w;
}
/*
output:
Enter the area of the flat cardboard: 23
dimensions of card to maximize the cardboard box which has a volume 0.023
Length: 4.796
Width: 4.796
dimensions of the cardboard box
Length: 4.794
Width: 4.794
Height: 0.001
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.