Lab · C++ Programming (modern C++ and DSA)
Lab: slab-rate electricity bills
The brief
A housing society in Nagpur collects the meter readings of its houses and shops and wants each bill worked out the way the power board does it: slab rates plus a fixed charge. You write the slab function, a switch for the fixed charge, a loop over the readings and a check that skips bad readings.
Why it matters
Slab tariffs are everywhere in India: electricity, water, mobile data and income tax all charge different rates for different bands. Billing software for power distribution companies and society-management apps is built from exactly these if/else chains, switches and loops, and a wrong boundary (100 or 101?) means thousands of wrong bills.
Skills you practise
- if / else if chains
- switch with break
- functions and default arguments
- for loops reading input
- continue for bad data
You need
- g++ 11 or newer (MinGW-w64 via MSYS2 on Windows, build-essential on Ubuntu, Xcode command line tools on macOS, where g++ runs Apple clang)
- A terminal (Git Bash, MSYS2 or PowerShell on Windows)
What you will have at the end
bill.cpp: a society billing program that reads meter readings, bills each house with slab rates and a fixed charge, skips bad readings and prints a summary.
How it is marked (rubric, 100 points)
| Correct billsEvery check passes, including the slab boundaries at 100 and 300 units and the skipped readings. | 45 |
|---|---|
| Good functionsenergyCharge, fixedCharge and totalBill each do one job; main only reads, calls and prints. | 25 |
| Clean buildNo warnings with -Wall -Wextra; every switch case ends with break or return. | 15 |
| ExplainedYou can explain slab billing, switch fall-through and why the loop runs from 1 to n. | 15 |
0/4 steps done
Step 1 of 4
The slab function
energyCharge(units) charges each slab at its own rate: 0, 300.00, 1125.00 and 2360.00 for 0, 100, 250 and 420 units.
Slab rates charge each band of units at its own rate, not the whole reading at the top rate. For 250 units: the first 100 cost 100 x 3.00 = 300, and the next 150 cost 150 x 5.50 = 825, so 1125.00. Write energyCharge as an if / else if chain: up to 100 units, up to 300, and above. Call it from main for the four readings and print one line each, as shown.
double energyCharge(int units) {
if (units <= 100) return units * 3.0;
// ... the other two slabs
}$ g++ -std=c++17 -Wall -Wextra -o bill bill.cpp; ./billYour notes: bill.cpp
Write down each command you ran. It goes into your project summary.