# Thread: Homework HELP! CProgram Electric Company

1. ## Homework HELP! CProgram Electric Company

I need help with my hw. Its due tomorrow and what i have doesnt seem to work :/
This is the problem:

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.

Write a function to compute and return the total charge for each customer. Write a main function to call the charge calculation function using the following data:

Customer Number Kilowatt-hours used
123 725
205 115
464 600
596 327
601 915
613 1,011
722 47

The program should print a three-column chart listing the customer number, the kilowatt-hours used, and the charge for each customer. The program should also compute and print the number of customers, the total kilowatt-hours used, and the total charges.

and this is what i have so far :

Code:
```
#include <stdio.h>

int main (void)
{
int custnum;
int  kwh;
double totalo;
double totald;
double totalt;
double totalf;
double finaltotal;

printf("\nCustomer Number or Enter -1 to quit>");
scanf("%d", &custnum);

if (custnum == -1)
break;

if(custnum >= 0)
printf("\nEnter the number of kwh used");
scanf("%d", &kwh);

if (kwh <= 300)
totalo = 0.9 * kwh;

if(kwh >300 && kwh <= 600)
totald = 0.8 * kwh ;

if(kwh > 600  && kwh <= 1000)
totalt = 0.6 * kwh

if(kwh >1000)
totalf = 0.5 * kwh;

finaltotal = totalo + totald + totalt+ totalf```

2. You've forgotten the return value and closing brace on your main function.

The minimum C program skeleton is...
Code:
```int main (void)
{

// code goes in here

return 0;
}```

3. yeah i know that im still working on that. The code i posted is incomplete , but what im having trouble is with the calculations and how to write them in code. Like how to take the first 300 and multiply by 0.9 and the 600 multiply by 0.8 etc

4. Do you have the knowledge of conditional statements?

5. Also, initialize your double datatype variables with some value, else you will keep getting garbage... Like
Code:
```double totalo=0;
double totald=0;
double totalt=0;
double totalf=0;
double finaltotal=0;```
Like this.....

6. Um yeah sorta :/
you're talking about if/ else if
right?

7. Sure, read my above message of initializing values and give that a hit...
If problem still persists, ask here....

8. Code:
``` if (usage > 900)
rate = 90;
else if (usage > 800)
rate = 100;
else if ( usage > 700)
rate  = 110;

total = usage * rate;```

9. Originally Posted by CommonTater
Code:
``` if (usage > 900)
rate = 90;
else if (usage > 800)
rate = 100;
else if ( usage > 700)
rate  = 110;

total = usage * rate;```
This will not solve his problem any ways.....

If the usage is 900, he needs to divide first 300 for some rate, next 300 for some other rates and remaining for some other rates specified in the problem....

10. So,
Code:
```int rem=0;
int total=0;
int usage=0;
cin>>usage;
rem=usage;
int count=1;
while(rem>=0)
{
if(rem>=300){
rem-=300;
}
if(count==1){
}
else if(count==2){
}
and so on......
count++;
}```
This is only the logic, you'll have to modify this according to your own problem specification.....

11. thanks but i get a syntax error -_-'
Ive attached a pic of what the final program output should look like idk if that would help :/
And im obviously a amateur at this since i just started taking this class and its a online class so yaaaa....

12. With a few tweaks:

Almost what you need
Code:
```
#include <stdio.h>

int main (void)
{
int custnum;
int  kwh, copy;
double totalo=0.0;
double totald=0.0;
double totalt=0.0;
double totalf=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);
copy = kwh;
while(kwh > 1000) {  //needs to loop if kwh > 2000
totalf += (kwh - 1000) * 0.5;  //needs += instead of =
kwh -= 1000;
}
if(kwh > 600) {   //will never loop
totalt = (kwh - 600) * 0.6;
kwh -= 600;
}
if(kwh > 300) {  //will never loop
totald = (kwh - 300) * 0.8;
kwh -= 300;
}
if(kwh) {
totalo = 0.9 * kwh;
kwh = 0;
}
finaltotal = totalo + totald + totalt+ totalf;
kwh = copy;
printf("\nCustomer number %d used %d kwh, and charged \$%.2lf\n",
custnum, kwh, finaltotal);
//prepare for next customer:
finaltotal = totalo = totald = totalt = totalf = 0;
}

(void) getchar();
return 0;
}```

13. Originally Posted by Mr.777
This will not solve his problem any ways.....

If the usage is 900, he needs to divide first 300 for some rate, next 300 for some other rates and remaining for some other rates specified in the problem....
Yeah... Looking back, I see that. My mistake.

Carry on....

14. 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 :/

15. 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;
}```