This program needs to be modified. This is a current sample output after a user
ID: 3630006 • Letter: T
Question
This program needs to be modified.
This is a current sample output after a user entered the values 5, 8, 7, and 9.
/*Sample output************
Enter the width of the river: 5
Enter distance of the factory downstream : 8
Enter the cost of laying the power line under water: $7
Enter the cost of laying the power line over land: $9
Length of power line under water: 9.43398
Length of power line over land: 1.24345e-014
Total Cost of constructing the power line: $66.0379
************************************************************************/
Can you modify the program so that the values in red only have two digits on the right side of the decimal point? The values on the left side of the decimal point can stay the way they are.
Everything else on this code works fine, and does not need changing.
Here is the code:
#include
using namespace std;
int main( )
{
//variables
double w; //width of river=w
double x; //distance of factory=x
double y; //cost of under water=y
double z; //cost of over land=z
double i = 0.0, distance, totalCost;
double widthMin = 0, DistanceMin = 0, minCost;
//width of the river from user
cout << " Enter the width of the river: ";
cin >> w;
//factory distance from user
cout << " Enter distance of the factory downstream : ";
cin >> x;
//cost of power line under water
cout<<" Enter the cost of laying the power line under water: $";
cin >> y;
//cost of power line over land
cout<<" Enter the cost of laying the power line over land: $";
cin >> z;
//Calculate minimum cost
minCost = ( w * y ) +( x * z );
do
{
distance = sqrt( (w * w) + ( i*i ));
//cost with this dimensions
totalCost = distance * y;
totalCost += (x - i) * z;
// cost is less than existing minimum cost
// then change minimum values of width and distance
if ( minCost > totalCost )
{
widthMin = distance;
DistanceMin = x - i;
minCost = totalCost;
}
i += 0.1;
} while ( i < x );
cout <<" Length of power line under water: "<< widthMin;
cout<<" Length of power line over land: " << DistanceMin;
cout<< " Total Cost of constructing the power line: $"<< minCost;
cout << " ";
system("PAUSE");
return 0;
} //End main
Explanation / Answer
#include<iomanip>
cout << setprecision(2);
cout <<setprecision(2) <<" Length of power line under water: "<< widthMin;
cout<<setprecision(2) <<" Length of power line over land: " << DistanceMin;
cout<<setprecision(2) << " Total Cost of constructing the power line: $"<< minCost;
cout << " ";
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.