How to create an array from a while loop

26 views (last 30 days)
Alright so my professor gave me this prompt, and i normally do well in this class. However, this one totally has me stumped. I'll post the prompt and then my attempt and any advice or criticism would be much appreciated.
3. Experiment shows that bacteria reproduce once every 30 minutes. Assume that the experiment starts with three bacteria. Write a program that calculates the number of bacteria present at intervals of one half hour from the beginning of the experiment until 6 hours have elapsed. Your program must create two arrays namely; time and bact. Array time contains the time values and array bact contains the bacteria count. Plot number of bacteria versus time. Your plot must have axes labels, grid and a title. The title is “your name – Bacteria Growth”. Use while loop.
Here's what i have so far:
clc
clear
time=(0:0.5:6);
b=1.5;
n=0;
while n<13
b=b*2;
n=(n)+1;
fprintf('\n%2d',b);
end
bact=b(1:12);
fprintf('\n %5d',bact);
I am aware that i do have to graph this, i can do that with ease, i just want to make sure im producing the right numbers and the two appropriate arrays. The problem appears to arise when i try to draw upon the b values from the loop. Am i not able to do this?
Thank you.

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 29 Mar 2014
Edited: Azzi Abdelmalek on 29 Mar 2014
clc
clear
time=(0:0.5:6);
b=1.5;
bact=zeros(1,12);
n=1;
while n<13
b=b*2;
bact(n)=b;
n=(n)+1;
fprintf('\n%2d',b);
end
bact

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!