Monday, April 1, 2013

The fuel tank program

A program that monitors the amount of fuel in a tank and gives a warning if the supply falls below 10% of the tank's capacity.

/* The fuel tank program */
#include <stdio.h> int main (void) { double capacity, supply, pumped; /* in liters */ /* initialize capacity of tank */ capacity = 5000.0; /* ask user for initial supply already in tank */ printf ("Enter the the initial supply: "); scanf ("%lf", &supply); /* the program loops until supply falls below 10% */ while (supply > capacity * 0.10) { /* ask user for quantity removed or delivered */ printf ("\nEnter the amount delivered(+)/removed(-): "); scanf ("%lf", &pumped); supply = supply + pumped; /* test so that you don't pump more when tank is empty */ if (supply < 0.0) supply = 0.0; /* test so that you don't fill more when tank is full */ if (supply > capacity) supply = capacity; } printf ("\nSupply below 10%% (%.2lf l remaining)\n", supply); return (0); }
Legend: preprocessor directives | variable declarations | main program | helper functions | user-defined structures | comments
Enter the the initial supply: 2000

Enter the amount delivered(+)/removed(-): -900

Enter the amount delivered(+)/removed(-): 1100

Enter the amount delivered(+)/removed(-): -1400

Enter the amount delivered(+)/removed(-): -200

Enter the amount delivered(+)/removed(-): -250

Supply below 10% (350.00 l remaining)

No comments:

Post a Comment