I am working on a recursion function that determines the amount of radioactive material that remains after a given number of years. The decay rate is 1/2 or 50% each year.
I am having a trouble trying to get the recursion setup.
i just cannot seem to get the recursive case right implementing the formula.
The correct result that should finally be returned is 31.25.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
int halflife(int, int);
int main()
{
cout << "The amount of 500 lbs of material after 5 years is "
<< halflife(500, 5) << endl;
}
int halflife(int initial, int year)
{
if (year == 0)
return 1;
elsereturn (halflife(initial, year - 1) * .5);
}
right now the issue is it is returning 0.03125. which as you can see i very close if i just multiply the result by 1000 however i cannot do that. Any help will be nice
Please clarify. Are you saying the radioactive level measures 500 at the beginning and the level will decrease by 50% each time interval and you have 5 time intervals?
If yes then your decay should look like:
5=500 then 4=250 then 3=125 then 2=62.5 then 1=31.25 then 0=15.6
How do you know that after n years, there will be exactly 1lb left?
1 2 3 4 5 6 7 8 9
double halflife( int initial, int year )
{
if( year == 0 ) return 1;
else {
double amount{ halflife( initial, year - 1 ) * .5 };
std::cout << "year = " << year << "\tamount = " << amount << "\n";
return amount;
}
}
year = 1 amount = 0.5
year = 2 amount = 0.25
year = 3 amount = 0.125
year = 4 amount = 0.0625
year = 5 amount = 0.03125
The amount of 500 lbs of material after 5 years is 0.03125
right now the issue is it is returning 0.03125. which as you can see i very close if i just multiply the result by 1000 however i cannot do that.
That is just a coincidence.
1 2 3 4 5 6
int main( )
{
constint initial{ 600 }, time{ 5 };
std::cout << "The amount of " << initial << " lbs of material after " << time << " years is " << halflife( initial, time ) << "\n";
}
year = 1 amount = 0.5
year = 2 amount = 0.25
year = 3 amount = 0.125
year = 4 amount = 0.0625
year = 5 amount = 0.03125
The amount of 600 lbs of material after 5 years is 0.03125
double halflife( int initial, int year )
{
if( year == 0 ) return initial;
else {
double amount{ halflife( initial, year - 1 ) * .5 };
std::cout << "year = " << year << "\tamount = " << amount << "\n";
return amount;
}
}
year = 1 amount = 250
year = 2 amount = 125
year = 3 amount = 62.5
year = 4 amount = 31.25
year = 5 amount = 15.625
The amount of 500 lbs of material after 5 years is 15.625
or
1 2 3 4 5 6 7
double halflife( double initial, int year )
{
std::cout << "year = " << year << "\tamount = " << initial << "\n";
if( year == 0 ) return initial;
elsereturn halflife( initial * 0.5, year - 1 );
}
year = 5 amount = 500
year = 4 amount = 250
year = 3 amount = 125
year = 2 amount = 62.5
year = 1 amount = 31.25
year = 0 amount = 15.625
The amount of 500 lbs of material after 5 years is 15.625
With recursive functions, it's extremely helpful to output the values of the parameters, so you can see how the computer unfolds the recursion.
Thanks, I was able to see that my multiplication of the 1/2 was in the wrong spot and I was returning the wrong value in my base case which is why my return was not coming up incorrectly.