C Program to find Minimum Cost With Example
We can explain C Program for Minimum Cost With Example with simple Examples.
The cost of operation of a unit consists of two components C1 and C2 which can be expressed as functions of parameter p as follows:
C1 = 30 – 8p
C2 = 10 + p2
The parameter p ranges from 0 to 10. Determine the value of p with an accuracy of + 0.1 where the cost of operation would be minimum.
C Program for Minimum Cost With Example
Problem Analysis:
Total cost = C1 + C2 = 40 – 8p + p2
The cost is 40 when p = 0, and 33 when p = 1 and 60 when p = 10. The cost, therefore, decreases first and then increases. The program in below evaluates the cost at successive intervals of p (in steps of 0.1) and stops when the cost begins to increase. The C program employs break and continue statements to exit the loop.
main() { float p, cost, p1, cost1; for (p = 0; p <= 10; p = p + 0.1) { cost = 40 - 8 * p + p * p; if(p == 0) { cost1 = cost; continue; } if (cost >= cost1) break; cost1 = cost; p1 = p; } p = (p + p1)/2.0; cost = 40 - 8 * p + p * p; printf("\nMINIMUM COST = %.2f AT p = %.1f\n", cost, p); }
Output
MINIMUM COST = 24.00 AT p = 4.0
Let me know if you find any difficulty in understanding this C Program with example and I would be glad to explain it further.