The problem is towards the end where it prints out the status report, if you type in values which require it, as the altitude reaches 0, it should stop at 0, but instead it goes beyond which messes up the rest of the calculations. The early outputs are correct though. Also, another problem is how do I put in a loop which asks the user if he or she wants to do it again. Would I do a while loop which starts from the beginning to the end?
Code:
int main()
{
int ship_altitude,
ship_fuel_supply,
beam_strength,
time;
float fuel_required;
char restart;
printf ("\nEnter the current altitude of the ship: ");
scanf ("%d", &ship_altitude);
printf ("\nEnter the current fuel supply of the ship: ");
scanf ("%d", &ship_fuel_supply);
printf ("\nEnter the strength of the enemy tractor beam: ");
scanf ("%d", &beam_strength);
printf ("\n\n\nSimulation:\n\n\n");
printf ("\n***********************Computer Report on Ship Status************************\n\n\n");
printf ("The Enterprise has gone into orbit around Delta Tau at an altitude of %d.\n", ship_altitude); /* Print Altitude */
printf ("It has a fuel supply of %d Kg. of fuel.\n", ship_fuel_supply); /* Print Fuel Supply */
printf ("It has been attacked by a tractor beam of strength %d Km/min/min.\n", beam_strength); /* Print Beam Strength */
fuel_required = ((1 - (double)ship_altitude / 200000) * (double)beam_strength); /* Formula */
printf ("At this altitude, %.1f Kg. of fuel are required to escape.\n\n", fuel_required); /* Print fuel requirement */
/* Test between having enough fuel and not */
if (ship_fuel_supply >= fuel_required)
printf ("There is efficient fuel to escape.\n\n\nRecommendation: Please start the engines and initiate your escape!\n");
else if ( ship_fuel_supply < fuel_required)
{
printf ("There is not enough fuel to escape.\n\n\nRecommendation: Please tell Trip to start shoveling!\n\n");
printf ("Here is what to expect:\n\n");
printf ("Time Altitude Fuel Available Fuel Required\n");
printf ("__________________________________________________________\n");
time = 0;
do
{
time = time + 1;
ship_altitude = ship_altitude - beam_strength * time * time;
ship_fuel_supply = ship_fuel_supply + 10;
fuel_required = ((1 - (double)ship_altitude / 200000) * (double)beam_strength);
printf ("%d min. ", time);
printf ("%d KM. ", ship_altitude);
printf ("%d KG. ", ship_fuel_supply);
printf ("%.1f KG. \n", fuel_required);
}
while (ship_fuel_supply < fuel_required && ship_altitude >= 0);
if (ship_altitude == 0)
{
printf ("In %d minutes, the ship will crash!\n", time);
printf ("I recommend sending a farewell message to Star Fleet!");
}
else if (ship_fuel_supply > fuel_required)
{
printf ("In %d minutes, it will be safe to escape.\n", time);
printf ("I recommend turning on the engines by then.\n");
}
}
return 0;
}