Tutorial 5.5: Electricity Bill Problem

PROBLEM
We are going to write a program that computes a customer's electricity bill. The rate of charge for the first 200 unit of electricity used is RM0.218. For the following unit of electricity used is RM0.334. A penalty of 1.5% of the unpaid balance if the unpaid balance is greater than zero. Electricity unit, unpaid balance are inputted by user.

SOLUTION
PROGRAM DATA REQUIREMENT
List of data required in this problem. There are three (3) categories of data.

Constants
RATELESS 0.218 /* rate of charge first 200 unit */
RATEMORE 0.334 /* rate of charge following unit */
PEN 0.015 /* penalty for unpaid balance */


Inputs
int unit /* unit of electricity used */
float UnpaidBal /* unpaid balance */


Outputs
float Penalty /* charge of penalty */
float UseCharge /* charge for the current electricity use */
float TotalBill /* total charge */


PROGRAM ALGORITHM (FLOW OF PROGRAM)
1. Display user instructions
2. Get data: unpaid balance and electricity unit used
3. Determine electricity unit used and compute use charge
4. Determine unpaid balance and compute penalty
5. Compute total charge
6. Display the total bill amount

DESIGN OF COMPUTATION OF USE CHARGE
The data required to compute the use charge are listed. We separate the involved data categories into Input, Process and Output.

Input Data
int unit /* unit of electricity used */

Process Data
RATELESS 0.218 /* rate of charge first 200 unit */
RATEMORE 0.334 /* rate of charge following unit */


Output Data
float UseCharge /* charge for the current electricity use */

Algorithm for Computation of Use Charge
We know that different rate will be used if the electricity unit used is more than 200. Thus we use if else selection to design the algorithm.
if unit > 200
compute use charge for more than 200 unit
else
compute use charge for less than 200 unit


Formula for Use Charge
UseCharge=(unit-200)*RATEMORE+200*RATELESS /* more than 200 */
UseCharge=unit*RATELESS /* less than 200 */


DESIGN OF COMPUTATION OF PENALTY
The data required to compute the penalty are listed. We separate the involved data categories into Input, Process and Output.

Input Data
float UnpaidBal /* unpaid balance */

Process Data
PEN 0.015 /* penalty for unpaid balance */

Output Data
float Penalty /* charge of penalty */

Algorithm for Computation of Penalty
Penalty is dealt when there is an unpaid balance. We use if selection to design the algorithm.
if unpaid balance > 0
compute penalty


Formula for Penalty
Penalty=PEN*UnpaidBal

DESIGN OF ELECTRICITY BILL LAYOUT
Displaying bill involves only input data (variables).

Input Data
float Penalty
float UnpaidBal
float UseCharge.

Algorithm for Displaying Bill
if unpaid balance > 0
compute penalty


IMPLEMENTATION
Begin by writing the #define directives for the constant data. In the main(), declare the input and output variables. Then start coding each program algorithm steps. Use printf() to display the user instruction/prompt and scanf() to read the user inputs (UnpaidBal and unit). Implement the computation of use charge and penalty using if else selection. Then, use relevant arithmetic operators to construct the formulas under their corresponding if else selection. Finally use comments in your program so as you won't get lost. Good luck and Happy Coding!

No comments:

Post a Comment