Homework HELP! CProgram Electric Company

This is a discussion on Homework HELP! CProgram Electric Company within the C Programming forums, part of the General Programming Boards category; I need help with my hw. Its due tomorrow and what i have doesnt seem to work :/ This is ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    39

    Unhappy 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. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    39
    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. #4
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Do you have the knowledge of conditional statements?

  5. #5
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    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. #6
    Registered User
    Join Date
    Feb 2011
    Posts
    39
    Um yeah sorta :/
    you're talking about if/ else if
    right?

  7. #7
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Sure, read my above message of initializing values and give that a hit...
    If problem still persists, ask here....

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
     if (usage > 900)
      rate = 90;
    else if (usage > 800)
      rate = 100;
    else if ( usage > 700)
      rate  = 110;
    
    total = usage * rate;

  9. #9
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Quote Originally Posted by CommonTater View Post
    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. #10
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    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.....

  11. #11
    Registered User
    Join Date
    Feb 2011
    Posts
    39
    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....
    Attached Images Attached Images  

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Mr.777 View Post
    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. #14
    Registered User
    Join Date
    Feb 2011
    Posts
    39
    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. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by 123456tacos View Post
    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;
    }
    Last edited by Adak; 03-04-2011 at 07:41 PM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  4. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM
  5. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 04:44 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21