# Homework HELP! CProgram Electric Company

Printable View

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 03-03-2011
123456tacos
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```
• 03-03-2011
CommonTater
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; }```
• 03-03-2011
123456tacos
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
• 03-03-2011
Mr.777
Do you have the knowledge of conditional statements?
• 03-03-2011
Mr.777
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.....
• 03-03-2011
123456tacos
Um yeah sorta :/
you're talking about if/ else if
right?
• 03-03-2011
Mr.777
Sure, read my above message of initializing values and give that a hit...
If problem still persists, ask here....
• 03-04-2011
CommonTater
Code:

``` if (usage > 900)   rate = 90; else if (usage > 800)   rate = 100; else if ( usage > 700)   rate  = 110; total = usage * rate;```
• 03-04-2011
Mr.777
Quote:

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....
• 03-04-2011
Mr.777
So,
Code:

```int rem=0; int total=0; int usage=0; cout<<"Enter your total usage:\t"; cin>>usage; rem=usage; int count=1; while(rem>=0) {         if(rem>=300){         rem-=300;                               }         if(count==1){                           total=300*(your rate);                           }           else if(count==2){                           total+=(300*(your rate));                 }           and so on......                 count++; }```
This is only the logic, you'll have to modify this according to your own problem specification.....
• 03-04-2011
123456tacos
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....
• 03-04-2011
Adak
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; }```
• 03-04-2011
CommonTater
Quote:

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.

:confused: Carry on....
• 03-04-2011
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 :/
• 03-04-2011
Adak
Quote:

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! :p 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. :D

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; }```
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last