Like Tree1Likes
  • 1 Post By Wexy

algorithm for rate based billing?

This is a discussion on algorithm for rate based billing? within the C Programming forums, part of the General Programming Boards category; I need to write a program which calculates an electric bill based on several tiers.. ex: Fixed Rate (upto 100 ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    6

    Unhappy algorithm for rate based billing?

    I need to write a program which calculates an electric bill based on several tiers..
    ex:
    Fixed Rate (upto 100 KWh.): $100.00
    Rate/KWh (100 upto 200 KWh.): $1.20
    Rate/KWh (200 upto 300 KWh.): $1.50
    Rate/KWh (above 300 KWh.): $250.00

    the user then will input the total KWh, and then the program calculates the bill and displays it..

    can someone point me in the right direction for writing an algorithm which calculates the bill? I've tried a few different ways, but none work..and each confuses me further..

    thanks for any help..
    gtyagi likes this.

  2. #2
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198
    Well you can do it with a series of if statments.

    Ask the user for the input, validate it, and then go the if statments.

    So you will have one

    if (amount < 100) - print out price, or whatever....$100.00
    if ( (amount>= 100) && (amount< 200) $1.20
    etc
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>I've tried a few different ways, but none work..and each confuses me further.
    Post what you've thought of so far, you might find with a little help you're almost there.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    6
    This is what i have so far..everything works, except function calculatebill()..so it returns the wrong answer:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int GetRates();
    int CalculateBill();
    int GetOptions();
    const float cutoff1 = 100.0;
    const float cutoff2 = 200.0;
    const float cutoff3 = 300.0;
    
    int main()
    {
      GetOptions();
      return 0;
    }
    
    int GetOptions()
    {
    char choice;
     start:
    
     printf("Selection?:\n");
     printf("\t -> (R)ates\n");
     printf("\t -> (B)illing\n");
     printf("\t -> (Q)uit\n");
     printf("\n\nYour choice:  ");
     scanf("\n%c",&choice);
     printf("--------------\n");
    
     switch(choice)
       {
       case 'r': case 'R':
         GetRates();
         GetOptions();
       case 'b': case 'B':
         CalculateBill();
         GetOptions();
       case 'q': case 'Q':
         exit(EXIT_SUCCESS);
       default:
         printf("Invalid key pressed, Try Again\n\n");
         goto start;
       }
    }
    
    int GetRates()
    {
      float fixed, oneup, twoup, threeup;
    
      printf("\nFixed Rate (upto 100KWh.) : ");
      scanf("%f",&fixed);
      printf("\nRate/KWh. (100 upto 200 KWh.) : ");
      scanf("%f",&oneup);
      printf("\nRate/KWh (200 upto 300 KWh.) : ");
      scanf("%f",&twoup);
      printf("\nRate/KWh (above 300 KWh.) : ");
      scanf("%f",&threeup);
    }
    
    
    int CalculateBill()
    {
      float usage,fixed,oneup,twoup,threeup,totalbill,difference;
    
      printf("\nEnter usage in KWh.: ");
      scanf("%f",&usage);
    
      while (usage > 0)
     {
      if (usage <= 100)
        totalbill = fixed;
      if (usage > 100);
        difference = usage - 200;
        totalbill = (difference * oneup);
      if (usage <= 300);
        difference = usage - 300;
        totalbill = (difference * twoup);
        usage = difference;
      if (usage > 300);
        totalbill = (totalbill + threeup);
    
     }
      printf("Total bill is %f",totalbill);
    }

  5. #5
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    Code:
    int GetRates()
    {
      float fixed, oneup, twoup, threeup;
    
      printf("\nFixed Rate (upto 100KWh.) : ");
      scanf("%f",&fixed);
      printf("\nRate/KWh. (100 upto 200 KWh.) : ");
      scanf("%f",&oneup);
      printf("\nRate/KWh (200 upto 300 KWh.) : ");
      scanf("%f",&twoup);
      printf("\nRate/KWh (above 300 KWh.) : ");
      scanf("%f",&threeup);
    }
    
    
    int CalculateBill()
    {
      float  usage,fixed,oneup,twoup,threeup,totalbill,differen
    ce;
    
      printf("\nEnter usage in KWh.: ");
      scanf("%f",&usage);
    
      while (usage > 0)
    You are redefining the variables in each of these functions. You are making them local. You should pass them as parameters, then maybe you will get results. Do you know anything about pointers?

    Try
    Code:
    int getRates (float *fixed, float *oneup, float *twoup, float *threeup)
    {
    
      ...
    
    }
    Last edited by Mister C; 11-28-2002 at 10:23 PM.
    Mr. C: Author and Instructor

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 03-09-2009, 10:15 PM
  2. Implement of a Fast Time Series Evaluation Algorithm
    By BiGreat in forum C Programming
    Replies: 7
    Last Post: 12-04-2007, 01:30 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

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