Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Can somebody help me do the math for this program (plz no Calculus, dont understand it)

+4 votes
asked Mar 3, 2020 by anonymous

/*

Write a program that prompts the user to enter the following:

The width of the river

The distance of the factory downstream on the other side of the river

The cost of laying the power line under water

The cost of laying the power line over land

The program then outputs the length of the power line that should run under water and the length that should run over land so the cost of constructing the power line is at the minimum. The program should also output the total cost of constructing the power line.

*/

#include <iostream>

#include <iomanip>

#include <cctype>

#include <cmath>

using namespace std;

double CostFunc(double x, double y, double w, double z);

int main()

{

double rWidth, dFactory;

double powCostWater, powCostLand;

cout << fixed << setprecision(2) << showpoint;

cout << "Enter the width of the river: ";

cin >> rWidth;

cout << endl << "Enter the factory's distance from the river: ";

cin >> dFactory;

cout << endl << "The cost for running power on land: ";

cin >> powCostWater;

cout << endl << "The cost for running power underwater: ";

cin >> powCostLand;

if (rWidth > 0 && dFactory > 0 && powCostWater > 0 && powCostLand > 0)

CostFunc(powCostWater,powCostLand,rWidth,dFactory);

else cout << "Input values greater then Zero!!!";

return 0;

}

double CostFunc(double x, double y, double w, double z)

{

double totalPrice = 5280*w*x + 5280 *z*y;

double minCost = totalPrice;

while(minCost >= totalPrice)

{

z -= 0.01;

w += 0.01;

minCost = 5280*w*x + 5280*z*y;

}

cout << "The minimum price is $" << minCost;

cout << endl << "The length underwater: " << w;

cout << endl << "The length on land: " << z;

return 0;

}

1 Answer

0 votes
answered Apr 3, 2023 by marijan (140 points)
Binary search  can be involved. It is not simpler, but you can reason

about it if calculus is not an option.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...