URGENT HELP NEEDED! Short on time, Plz help!

I want to make a program to calculate cost of gas used. Plz if anyone can give algorithm. Here is what i have to do:

Suppose that charges by a gas company are based on consumption according to the following table:
Gas Used Rates
First 70 cubic meters Rs. 180/- minimum cost
Next 100 cubic meters Rs. 7/- per cubic meter
Next 230 cubic meters Rs. 5/- per cubic meter
Above 400 cubic meters Rs. 3/- per cubic meter

Write a program that computes the charges of given amount of gas usage. The program takes current meter reading and previous meter reading as input and then calculates and displays the amount of bill. The current meter reading cannot be less than the previous meter reading.
We won't do your homework for you, sorry. Come back if you have done an effort that can be shown (meaning show what you have gotten so far) and when you have a specific question that doesn't fall in the category of "I don't know a single thing about C++ so I need you guys to complete this big chunk for me".
I have recently started my Master in Computer Science. I have not yet used c++ but i have been asked to make algorithm of this program. I have comeup this far but its not working:
cur = InputBox("enter current meter meter reading")
pre = InputBox("enter current previous meter reading")
gas = cur - pre
If gas <= 70 Then
amt = 180
End If
If gas > 70 And gas <= 170 Then
amt = 180 + (7 * (gas - 70))
End If
If gas > 170 And gas <= 400 Then
amt = 180 + (5 * (gas - 170))
End If
If gas > 400 Then
amt = 180 + (3 * (gas - 400))
End If
Print "Current Meter Reading: ", cur
Print " Previous Meter Reading: ", pre
Print " Gas used : ", gas
Print " Charges: ", amt
I just need to know whats wrong with my logic?
Plz help :(
Your logic is incomplete, as I guess you realised.

It looks like the first two ranges are OK, but the others aren't charging for all the gas. For gas > 170 And gas <= 400 range, you need to the charge the minimum cost plus the cost for the gas in both the 7/- and 5/- ranges, not just the latter. Similarly for the gas > 400, but then you've got two lower ranges.

While you could extend the calculations so they do the whole of the required calculation, but I suggest you rework the calculation so you keep a running total to pay, and also a decreasing gas left to pay for.

* it they have bought any gas at all, they starts with 180
* if they have used equal to or less than 70 units, then theres nothing left to pay for, or you take 70 off the gas to pay for
* you repeat the process for the next range. if they have used more that 0 units for this range, they are charged accordingly.
* etc.

That is, you do the calculation in a step-wise fashion.

Andy

Last edited on
Thanks. I will try it.
Topic archived. No new replies allowed.