Using MatLab An insect population doubles every generation W

Using MatLab: An insect population doubles every generation. Write a while loop that iterates numGeneration times. Inside the while loop, write a statement that doubles currentPopulation in each iteration of the while loop.

I have set up the following code, but it only works for the first and third assessement checks.

function currentPopulation = CalculatePopulation(numGeneration, initialPopulation)
% numGeneration: Number of times the population is doubled
% currentPopulation: Starting population value

i = 2; % Loop variable counts number of iterations
currentPopulation = initialPopulation;
  
% Write a while loop that iterates numGeneration times
while ( i <= numGeneration )
currentPopulation = currentPopulation * (i * 2);
i = i * 2;
end
% Write a statment that doubles currentPopulation in
% each iteration of the while loop
  
% Hint: Do not forget to update the loop variable  
end

Assessment Checks:

1. CalculatePopulation(2, 5) returns 20

2. CalculatePopulation(10, 4) returns 4096 << This is the one that won\'t work

3. Calculate Population (0, 78) returns 78

Could someone tell me where I went wrong with my coding? I have tried absolutely everything and nothing will get me to the second assessment check.

Solution

function currentPopulation = CalculatePopulation(numGeneration, initialPopulation)
% numGeneration: Number of times the population is doubled
% currentPopulation: Starting population value

    i = 0;    % Loop variable counts number of iterations
    currentPopulation = initialPopulation;
  
    % Write a while loop that iterates numGeneration times
    while ( i < numGeneration )
        currentPopulation = currentPopulation * 2;
        i = i + 1;
    end
end

Using MatLab: An insect population doubles every generation. Write a while loop that iterates numGeneration times. Inside the while loop, write a statement that

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site