Monday, April 1, 2013

Mission to the Moon

This program computes the length of a mission to take photographs around the Moon given the time it takes to get there and to go around the Moon once


/* Mission to the Moon */
#include <stdio.h> int main (void) { double transit, period, time; int n_orbits, orbit; /* 73 hours from Earth to Moon */ transit = 72; /* 1.5 hours per orbit around Moon */ period = 1.5; /* mission begins */ printf ("Mission to the Moon - Times in hours\n"); printf ("====================================\n"); time = 0.0; n_orbits = 10; /* mission is 10 orbits */ printf ("Time - %4.1lf - Mission begins\n", time); /* we are at the moon */ time = time + transit; printf ("Time = %4.1lf - At the moon\n", time); orbit = 1; /* doing the orbits around the Moon */ while (orbit <= n_orbits) { time = time + period; printf ("Time = %4.1lf - %2d orbits completed\n", time, orbit); orbit = orbit + 1; } /* back to Earth */ time = time + transit; printf ("Time = %5.1lf - Mission complete.\n", time); return (0); }
Legend: preprocessor directives | variable declarations | main program | helper functions | user-defined structures | comments
Mission to the Moon - Times in hours
====================================
Time -  0.0 - Mission begins
Time = 72.0 - At the moon
Time = 73.5 -  1 orbits completed
Time = 75.0 -  2 orbits completed
Time = 76.5 -  3 orbits completed
Time = 78.0 -  4 orbits completed
Time = 79.5 -  5 orbits completed
Time = 81.0 -  6 orbits completed
Time = 82.5 -  7 orbits completed
Time = 84.0 -  8 orbits completed
Time = 85.5 -  9 orbits completed
Time = 87.0 - 10 orbits completed
Time = 159.0 - Mission complete.

No comments:

Post a Comment