Hello! Can someone please help me? I need some help with this problem. I am aske
ID: 3777750 • Letter: H
Question
Hello! Can someone please help me? I need some help with this problem. I am asked to write a recursive method for this (I have written a code for it but, it was not recursive):
I understand that recursive method is breaking down the problem into smaller parts until it gets so small that it's super easy to solve.
Write a recursive method to calculate the size of a population of organisms that increases at a specified rate each day.
The method header is: public int populationSize(int startingPopulation, double increaseRate, int numberOfDays)
Examples:starting population = 10, increase rate = 0.5, number of days = 5
day 1 population = 15
day 2 population = 22 (22.5 truncated down to 22)
day 3 population = 33
day 4 population = 49 (49.5 truncated down to 49)
day 5 population = 73 (73.5 truncated down to 73)
method returns 73
starting population = 2, increase rate = 1.0, number of days = 4
day 1 population = 4
day 2 population = 8
day 3 population = 16
day 4 population = 32
method returns 32
Explanation / Answer
#include<iostream.h>
#include<conio.h>
public int populationSize(int startingPopulation, double increaseRate, int numberOfDays);
void main()
{
int sPop, irate,days, popSize;
cout<<"Please enter the startingPopulation"<<endl;
cin>>sPop;
cout<<"Please enter the increase rate"<<endl;
cin>>irate;
cout<<"Please enter the number of days"<<endl;
cin>>days;
popSize = populationSize(sPop,irate,days);
cout<<"The size of population after"<<days<<"is"<<popSize;
}
public int populationSize(int startingPopulation, double increaseRate, int numberOfDays)
{int nextPopulation;
int rate = increaseRate;
int days = numberOfDays;
nextPopulation=(startingPopulation*increaseRate+startingPopulation);
if(days>0)
{
return populationSize(nextPopulation, rate, days-1);}
else
{cout<<"Number of days goes to zero";}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.