# Thread: algorithm for rate based billing?

1. ## 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..

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

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

4. 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. 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)
{

...

}```

Popular pages Recent additions