/******************************************************** ** File: proj2.c ** Author: Dennis L. Frey ** Date: 10/17/99 ** ** Description: ** This program prints a table of astronomical units (AU) ** and miles from the sun to each of the planets. It calculates the ** values based on Bode's law, discovered in 1722. ** ** d[i] = (4 + b[i]) / 10 ** ** gives the distance (d[i]) from the sun to the i'th planet in AUs ** (1 AU = 93,000,000 miles, the distance from the Earth to the sun) ** where b[i] is the i'th term in Bode's power series ** ***********************************************************/ #include /* define each planet */ #define MERCURY 1 #define VENUS 2 #define EARTH 3 #define MARS 4 #define ASTEROIDS 5 #define JUPITER 6 #define SATURN 7 #define URANUS 8 /* function prototypes */ void PrintExplanation ( ); /* greeting to the user */ int GetTermValue (int index); /* b[i] in Bode's law */ double FindAU (int termvalue); /* d[i] in Bode's law = AU to sun */ double FindMiles (double au); /* calculates miles from AU */ main ( ) { int planet; /* for-loop index over planets */ double au; /* astronomical units */ double miles; /* from sun to planet */ int termValue; /* from Bode's series */ /* tell the user about Bode's law */ PrintExplanation (); /* calcuate AU and miles, then print the data ** for each planet */ for (planet = MERCURY; planet <= URANUS; planet++) { /* calc AU and miles for the planet */ termValue = GetTermValue (planet); au = FindAU (termValue); miles = FindMiles (au); printf ("\t\t"); /* print the planet name */ switch (planet) { case MERCURY: printf ("Mercury"); break; case VENUS: printf ("Venus "); break; case EARTH: printf ("Earth "); break; case MARS: printf ("Mars "); break; case ASTEROIDS: printf ("? "); break; case JUPITER: printf ("Jupiter"); break; case SATURN: printf ("Saturn "); break; case URANUS: printf ("Uranus "); break; default: printf ("Unknown"); break; } /* print the AU and miles */ printf ("%10.1f AU %15.6e Miles\n", au, miles); } printf ("\n\n"); } /************************************** ** Function: GetTermValue ** Usage: bi = GetTermValue (index) ** ** Input: the index into Bode's sequence for ** which to calculate the value ** Output: the i'th term in Bode's sequence ** ** Description: this function calculate the ** i'th term of Bode's power series ** b[1] = 1 ** b[2] = 3 ** b[i] = 2 * b[i-1] for i > 2 *****************************************/ int GetTermValue (int index) { int termValue; int i; /* b[1] = 1 */ if (index == 1) { termValue = 1; /* b[2] = 3 */ } else if (index == 2) { termValue = 3; /* start at b[2] and multiply by ** 2, index - 2 times */ } else { termValue = 3; for (i = 0; i < index - 2; i++) { termValue *= 2; } } return termValue; } /************************************ ** Function: FindAU ** Usage: au = FindAU (termValue) ** ** Input: a value from Bode's power series ** Output: astronomical units (AU) calculated ** from Bode's law *************************************/ double FindAU (int termValue) { /* return AU according to Bode's law */ return (4 + termValue) / 10.0; } /******************************** ** Function: FindMiles ** Usage: miles = FindMiles (au) ** ** Input: astronomical units (au) ** Output: returns the miles from the ** sun by converting au to miles ** ** Assumption: the distance from the Earth ** to the sun is 93,000,000 miles ***************************************/ double FindMiles (double au) { return au * 93000000; } /************************************************* ** Function: PrintExplanation ** Usage: PrintExplanation () ** ** Inputs: none ** Outputs: see description ** ** Description: this function informs the user ** about Bodes law and the information to be presented **************************************************/ void PrintExplanation ( ) { printf ("\n\n"); printf ("\t\tIn 1722, J. E. Bode discovered a \"law\"\n" "\t\tthat calculated the distance from the sun\n" "\t\tto all the planets known at that time.\n" "\t\tThis formula predicted the existance of a\n" "\t\tplanet between Mars and Jupiter, leading to\n" "\t\tthe discovery of the asteroid belt.\n\n" "\t\tThe table below gives the distance from the sun to\n" "\t\teach planet in astronomical units (AU) and miles\n" "\t\taccording to Bodes Law\n\n\n"); }