C++ problem...Plz

closed account (ECMN8vqX)
Hi everyone,,,,this problem is from my C class...
The question is below,,,,I finished almost of then,,,but I'm confused about the calculation for the cost..
Can anyone give me the code for this function:
"double costFunc(double x, double y)"???



(The power station problem) A power station is on one side of a river
that is one-half mile wide, and a factory is eight miles downstream on the
other side of the river (see Figure 7-21). It costs $7 per foot to run power
lines over land and $9 per foot to run them under water. Your objective is
to determine the most economical path to lay the power line. That is,
determine how long the power line should run under water and how long
it should run over land to achieve the minimum total cost of laying the
power line.

Write a program that prompts the user to enter:
3. The width of the river
b. The distance of the factory downstream on the other side of the river
c. The cost of laying the power line under water
d. 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.



Here is what I have so far....Thank you very much!!!
----------------------------------------------------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>

using namespace std;

void conversionFunc(double& dFactory, double& wRiver);
double costFunc(double, double);

int main()
{
	double wRiver;
	double dFactory;
	double cUnderWater;
	double cOverLand;


	cout << "Please enter the width of the river: ";
	cin >> wRiver;
	cout << endl;

	cout << "Please enter the distance of the factory downstream on the other side of the river in miles: ";
	cin >> dFactory;
	cout << endl;

	conversionFunc(dFactory, wRiver);

	cout << "Enter the cost of laying the power line under water: ";
	cin >> cUnderWater;
	cout << endl;

	cout << "Enter the cost of laying the power line over land: ";
	cin >> cOverLand;
	cout << endl;

	costFunc(dFactory, wRiver);




	return 0;
}

void conversionFunc(double& x, double& y)
{
	y /= 5280;
	x /= 5280;

	cout << "converted miles to feet, dFactory is: " << x 
		<< ", and wRiver is: " << y << endl;
}

double costFunc(double x, double y)
{
	return 0;

}
Last edited on
[code] "Please use code tags" [/code]

Your problem is equivalent to the refraction of the light. (the cost is the inverse of the refraction coefficient).
http://en.wikipedia.org/wiki/Snell%27s_law
http://en.wikipedia.org/wiki/Fermat%27s_principle
Last edited on
This is a minimization problem. Do you know calculus?

C = Cost
X = Cost of power line over land
Y = Cost of power line underwater

C = X + Y

X = Cx * x

where Cx = cost per foot over land, x = distance in feet over land.

Y = Cy * y

where Cy = cost per foot underwater, y = distance in feet underwater.

You also know that x is:

x = 42240 - z

where z is the distance in feet parallel to the river from the baseline of the factory up to the point where the underwater lines start, and 42240 is 8 miles in feet.

You also know that:

y = Sqrt(Sqr(2640) + Sqr(z))

which is just Pythagoras (2640 is half a mile in feet).

So you need to find the values of x, y and z so that

C = Cx * x + Cy * y = minimum possible value.

How? Derivation. Well, let's substitute first.

From the last, first, second and third equations:

C = Cx * x + Cy * Sqrt(Sqr(2640) + Sqr(z))

Now substitute x too:

C = Cx * (42240 - z) + Cy * Sqrt(Sqr(2640) + Sqr(z))

At this point just calculate the derivate of C in function of z, dC/dz:

dC/dz = Cx * (-1) + Cy * z /Sqrt (Sqr(2640) + Sqr(z)) = 0

And that's the final equation, if my (veeeery) rusty math is OK. Solve the above for variable z and you solve the problem.
Last edited on
Oops, I just saw that the end user has to input the distances from the power plant to the factory. Change my numeric constants for variables.
The points were "attached" to the river?
In that case I think that the solution to the equation proposed by webJose is the same that to calculate the critical angle.
http://en.wikipedia.org/wiki/Total_internal_reflection#Critical_angle

By the way, I made a mistake in the previous post, the cost is the refraction coefficient.
Topic archived. No new replies allowed.