Robson » Little Red Book » Electricity Bill

Chapter 4 - Question 6

Computing an electricity bill involves calculating the number of units consumed and allocating them to a variety of tariffs.

Tariffs are based on two components - a standing charge and a price per unit. There may be more than one price range - perhaps the first 50 units are charged at a higher rate than subsequent units.

Given that the initial tariffs offered are:

TariffCostsPrice
AStanding charge10.00
First 80 units.105
Subsequent units.03
BStanding charge3.00
First 250 units0.10
Subsequent units.075
CStanding chargeNIL
All units0.125

Customers must choose the tariff they wish to be charged under. Design a program that accepts the number of units consumed and reports the most economic tariff.

Solution 1

C++.

#include <stdio.h>
#include <stdlib.h>
 
using namespace std;
 
// iUnits = The total amount of units consumed
// fStart = The standing charge
// fSwitch = The amount that electricity switches to lower rate
// fStandardRate = The high rate that applies to the first units
// fLowRate = The low rate that applies to subsequent units
float ic_CalculateCost(int iUnits, float fStart,
     float fSwitch, float fStandardRate, float fLowRate=0)
{
   // stores the total
   float fCost = fStart;  
   
   // work out the price for this tariff
   // see if they used enough units to use both brackets
   if (fSwitch && iUnits > fSwitch)
   {
       // the first few units are at standard rate
       fCost += fSwitch * fStandardRate;
       // remaining units at low rate
       fCost += (iUnits - fSwitch) * fLowRate;
   }
   else
       // all units at standard rate
       fCost += iUnits * fStandardRate;
       
   // return the total price
   return fCost;  
}
 
int main(int argc, char *argv[])
{
   // convert the units to a number and store
   int iUnits = atoi(argv[1]);
   // this stores the prices for each company
   float fPrices[2];
   
   // work out the price for tariff a
   fPrices[0] = ic_CalculateCost(iUnits, 10, 80, 0.105, 0.03);
   // work out the price for tariff b
   fPrices[1] = ic_CalculateCost(iUnits, 3, 250, 0.1, 0.075);
   // work out the price for tariff c
   fPrices[2] = ic_CalculateCost(iUnits, 0, 0, 0.125);    
   
   // show all the total prices
   printf("Tariff A: %c%.2f\nTariff B: %c%.2f\nTariff C: %c%.2f",
                  156, fPrices[0],
                  156, fPrices[1],
                  156, fPrices[2]);            
}
 

Solution 2

Excel formulas.

Tariff A:

=IF(A1 > 80, 10 + (80 * 0.105) + ((A1 - 80) * 0.03), 10 + (A1 * 0.105))

Tariff B:

=IF(A1 > 250, 3 + (250 * 0.1) + ((A1 - 250) * 0.075), 3 + (A1 * 0.1))

Tariff C:

=A1*0.125

The formulas above were then used to construct the following chart:

Excel chart showing the cost of different amounts of units

Tariff C is the best choice for less than 120 units. Tariff B is the best choice for 120 to 190 units. Tariff A is the best choice for more than 190 units.

Log

© 2004-17 robson | cc unless stated