Hi, so the problem is this:
In the 18th century, the astronomer Johann Daniel Titius proposed a rule, later
recorded by Johann Elert Bode, for calculating the distance from the sun to
each of the planets known at that time. To apply that rule, which is now known
as the Titius-Bode Law, you begin by writing down the sequence
b_1 = 1 b_2 = 3 b_3 = 6 b_4 = 12 b_5 = 24 b_6 = 48 . . .
where each subsequent element in the sequence is twice the preceding one. It
turns out that an approximate distance to the ith planet can be computed from
this series by applying the formula
d_i = (4 + b_i) / 10
The distance d_i is expressed in astronomical units (AU), which correspond to
the average distance from the earth to the sun (approximately 93,000,000
miles). Except for a disconcerting gap between Mars and Jupiter, the
Titius-Bode law gives reasonable approximations for the distances to the seven
planets known at the time:
Mercury 0.5 AU
Venus 0.7 AU
Earth 1.0 AU
Mars 1.6 AU
? 2.8 AU
Jupiter 5.2 AU
Saturn 10.0 AU
Uranus 19.6 AU
Write a recursive function getTitiusBodeDistance(k) that calculates
the expected distance between the sun and the k^th planet, numbering outward
from Mercury starting with 1. Test your function by writing a program that
displays the distances to each of these planets in tabular form.
I cannot think how to make it to simple problems and solve recursively. Currently, I have a very short program solved with iterations. I cannot think of any simple problems for this problem. I also think my if statement is wrong
if (k < 1 ) return 0.5
:) I am trying to write this program using recursion!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
#include <iostream>
using namespace std;
double arr [] = { 1, 3, 6, 12, 24, 48, 96, 192 }, d;
double getTitiusBodeDistance(int k)
{
if (k < 1) {
return 0.5;
}
else
double arr [] = { 1, 3, 6, 12, 24, 48, 96, 192 }, d;
double d = (4 + arr[k]) / 10;
return d;
}
int main()
{
for (int i=0; i < 8; i++)
cout << "Planet " << i << " | " << getTitiusBodeDistance(i) << endl;
return 0;
}
|
Output:
Planet 0 | 0.5
Planet 1 | 0.7
Planet 2 | 1
Planet 3 | 1.6
Planet 4 | 2.8
Planet 5 | 5.2
Planet 6 | 10
Planet 7 | 19.6 |