Question #46315

PGandE.cpp) Consider a gas and electric bill that
sets two levels of use as follows:
Gas Electric
Baseline Quantities 31 therms 238.7 kwh
Baseline price $.504 /therm $.094 /kwh
Over Baseline Price $.824 /therm $.133 /kwh
Write a program that takes starting and ending
readings for gas and electric meters and calculates
charges to the nearest cent. Use one function to
calculate charges for both gas and electric so as to
eliminate duplicate calculations.
1

Expert's answer

2014-10-01T01:24:37-0400

Answer on Question #46315, Programming, C++

Problem.

PGandE.cpp) Consider a gas and electric bill that sets two levels of use as follows:

Gas Electric

Baseline Quantities 31 therms 238.7 kwh

Baseline price .504/therm.504 /therm.094 /kwh

Over Baseline Price .824/therm.824 /therm.133 /kwh

Write a program that takes starting and ending readings for gas and electric meters and calculates charges to the nearest cent. Use one function to calculate charges for both gas and electric so as to eliminate duplicate calculations.

Solution.

Code

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
// Compute total price
float compute(float used, float quantity, float price, float overPrice) {
    float total;
    if (used < quantity) {
        total = used * price;
    } else {
        total = quantity * price + (used - quantity) * overPrice;
    }
    return total;
}
int main() {
    float sGas, sElectric;
    float fGas, fElectric;
    // Input/Output
    cout << "Start gas: ";
    cin >> sGas;
    cout << "End gas: ";
    cin >> fGas;
    cout << "Gas price: $";
    cout << fixed << setprecision(2) << compute(fGas - sGas, 31, 0.504, 0.824) << endl;
    cout << "Start electric: ";
    cin >> sElectric;
    cout << "End electric: ";
    cin >> fElectric;
    cout << "Electric price: $";
    cout << fixed << setprecision(2) << compute(fElectric - sElectric, 238.7, 0.094, 0.133) << endl;
    return 0;
}

Result

Start gas: 123

End gas: 170

Gas price: 28.81Startelectric:134Endelectric:180Electricprice:28.81 Start electric: 134 End electric: 180 Electric price:4.32

http://www.AssignmentExpert.com/

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!
LATEST TUTORIALS
APPROVED BY CLIENTS