Thread: Help for a HW assignemnt. Most of its done, just need help on something small

  1. #1
    Registered User
    Join Date
    Mar 2012

    Red face Help for a HW assignemnt. Most of its done, just need help on something small

    Hey all, for my assignment, im suppost to do the following:

    "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."

    It is suppose to look like this: Help for a HW assignemnt. Most of its done, just need help on something small-ch6002-jpg

    Now for my coding, everthing works great! The only problem I have is getting the file part into the program as intructed by the picture where it adds the total number of customers, kwh and charged. Im not exactly sure how to go about doing that and where to add it at.

    My Code:

    #include <stdio.h>
    int main (void)
      int custnum;
      int  kwh, copy;
      double total=0.0;
      double finaltotal=0.0;
      while(1) {
        printf("Enter customer number and kwh (-1 to quit):");
        scanf("%d", &custnum);
        if (custnum == -1)
        scanf("%d", &kwh);
        (void) getchar();
        copy = kwh;
        if(kwh > 300) {
          total += 300 * 0.09;
          kwh -= 300;
          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 += 1000 * 0.05;
          kwh -= 1000;
        }else {
          total += kwh * 0.05;
          kwh -= 0;
        finaltotal = total; 
        kwh = copy;
        printf("\nCustomer Num: %d            KWH used:%10.2d            Charge:%10.2f\n",
        custnum, kwh, finaltotal);
        finaltotal = total = 0; 
      (void) getchar();
      return 0;
    Any help would really be great!


  2. #2
    Registered User
    Join Date
    Sep 2006
    Sounds like the assignment wants THOSE specific customer numbers and kwh figures to be calculated - not some numbers you put in.

    I'd suggest using an int array[][] (2 dimension), to hold the cust. no, and the kwh. (Where each customer has their own row of data), but you could use two arrays of 1 dimension, ( array[] ) and then work through the data in parallel array style (where XXXXXXX is in row N, and his kwh is in row N of the second int array)., so it's consistently arranged by rows, even though it's in separate arrays - treat them like they're just one array.

    You need a variable for the number of customers processed, and their total charges and kwh's used. (Just a simple tally). Total should be set back to zero for each new customer, but not FinalTotal - that's a tally for all the customers bills, and I'd rename it either finalCharges, or finalKwh, and be sure your program has both these variables being tallied up.

  3. #3
    Registered User
    Join Date
    Mar 2012
    I don't think we learned how to do arrays yet. I do know that last assignment we used do whiles to gather the information with if else if's but I'm just not sure how to come about this problem

  4. #4
    Registered User
    Join Date
    Mar 2011
    keep separate variables for the overall totals (total_customers,total_kwh,total_charge). after you printf the per customer data, add the respective values to the overall totals and after you exit the loop print those totals. (initialize them to 0 before the loop)

    imagine how you would do it on paper. you would keep separate running totals of each value.

  5. #5
    Registered User
    Join Date
    Mar 2012
    How would I go about adding the total number of charges then? I think i know how to add the total cust. and kwh with using something like total_customers ++, but im not sure how to add numbers like that

  6. #6
    Registered User
    Join Date
    Sep 2008
    Toronto, Canada
    Your logic is not correct. Once you subtract 300, then the next interval 300-600 is checked wrong. Likewise interval 600-1000.
    To accumulate totals of all customers simply add to total_kwh and total_charge variables.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. one small help
    By kumaravel_futu in forum C Programming
    Replies: 20
    Last Post: 04-29-2008, 04:22 AM
  2. Let us see your small #%$^
    By vasanth in forum A Brief History of
    Replies: 4
    Last Post: 06-01-2003, 06:24 AM
  3. small bug
    By doublin in forum C++ Programming
    Replies: 6
    Last Post: 10-24-2002, 09:28 AM
  4. A small problem with a small program
    By Wetling in forum C Programming
    Replies: 7
    Last Post: 03-25-2002, 08:45 PM