// Gravitational field of Earth. // Illustrates simple three dimensional // plotting routines. #include #include using namespace std; const double km = 1000; // Note this procedure! // An alternate naming // convention capitalizes // all global constants const double gravitationalConstant = 6.67e-11; const double earthMass = 5.97e24; const double earthRadius = 6380 * km; const int matSize = 40; // This MUST be declared a const int // to be used in the subsequent // type statements ! double gravitationalField (double aX, double aY) { // Returns gravitational field inside of Earth // if r < earthRadius, otherwise returns field // outside if r > earthRadius double r2 = aX * aX + aY * aY; // r^2 return (r2 < earthRadius * earthRadius) ? gravitationalConstant * earthMass * sqrt(r2) / pow(earthRadius,3) : gravitationalConstant * earthMass / r2; } int main() { double position[matSize]; // x and y coordinate space float offset = matSize / 2 - 0.5; // Determines the starting // point of the grid for (int loop = 0; loop < matSize; loop++) position[loop] = 0.1 * earthRadius * (loop - offset); float x, y; for (int outerLoop = 0; outerLoop < matSize; outerLoop++) { x = position[outerLoop]; for (int innerLoop = 0; innerLoop < matSize; innerLoop++) { y = position[innerLoop]; cout << x << " " << y << " " << gravitationalField(x,y) << endl; } } }