Originally Posted by
123456tacos
Thanks the program works perfect except i get the wrong answer enter 725 kwh im suppossed to get $58.50 but i get $18.75 :/
Actually, with 725 kwh, you would get 187.50, with the arithmetic I posted up. (I used yours, ehem, ehem...).
Since cents means hundreds, you need to add one zero after all the decimal points in the charge per kwh:
<< 9 cents / kwh = 0.09, not 0.9, etc. >>
The electric company charges according to the following rate schedule:
9 cents per kilowatt-hour (kwh) for the first 300 kwh
8 cents per kwh for the next 300 kwh (up to 600 kwh)
6 cents per kwh for the next 400 kwh (up to 1,000 kwh)
5 cents per kwh for all electricity used over 1,000 kwh.
So, some arithmetic:
725 kwh:
total = (.09 * 300) + (.08 * 300) + (.06* 125)
total = 27 + 24 + 7.50
total = 58.50
Yep! Well, I knew the cents was off, but I was going to leave that to you to fix.
Unfortunately, I misinterpreted the rate scheme, so I'll fix it, since it's not a minor touch up.
OK, I re-did the charge arithmetic logic, and I believe it's OK, BUT I only tested it once, with 725 kwh.
So you need to test it < thoroughly > before adopting something similar.
I got rid of a few variables because they were so annoying.
Code:
/*
The electric company charges according to the following rate schedule:
9 cents per kilowatt-hour (kwh) for the first 300 kwh
8 cents per kwh for the next 300 kwh (up to 600 kwh)
6 cents per kwh for the next 400 kwh (up to 1,000 kwh)
5 cents per kwh for all electricity used over 1,000 kwh.
725 kwh = $58.50
*/
#include <stdio.h>
int main (void)
{
int custnum;
int kwh, copy;
double total=0.0;
double finaltotal=0.0;
while(1) {
printf("\nEnter Customer Number or enter -1 to quit>");
scanf("%d", &custnum);
if (custnum == -1)
break;
printf("Enter kwh for customer %d>", custnum);
scanf("%d", &kwh);
(void) getchar();
copy = kwh;
if(kwh > 300) {
total += 300 * 0.09;
kwh -= 300;
}else{
total += kwh * 0.09;
kwh = 0;
}
if(kwh > 300) {
total += 300 * 0.08;
kwh -= 300;
}else {
total += kwh * 0.08;
kwh = 0;
}
if(kwh > 400) {
total += 400 * 0.06;
kwh -= 400;
}else {
total += kwh * 0.06;
kwh = 0;
}
if(kwh > 1000) {
total += kwh * 0.05;
}
finaltotal = total;
kwh = copy;
printf("\nCustomer number %d used %d kwh, and charged $%.2lf\n",
custnum, kwh, finaltotal);
//prepare for next customer:
finaltotal = total = 0;
}
(void) getchar();
return 0;
}