Help on a for loop for triplicating bacteria!

I have to create a program that simulates cell reproduction of a bacteria.
The user is to input a number of days greater than 0 and up to 2 weeks, and an initial number of cells greater than 0, but less than or equal to 20.

I needed to do a robust check of those values, which I managed to do.

However, the third step is to create a for loop that will output a table of two columns: day and number of cells. The number of cells is to be calculated by multiplying the previous number of cells by 3.

I don't know how to make a for loop that will perform the calculations based on the previously calculated value of cells.

This is for an introductory C++ course, so the code has to be very basic, nothing fancy. The professor will deduct points if I use advanced concepts.

This is my code so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
Purpose: This program is to create a table that measures the number of cells that a daily
triplicating bacteria generates from any number of days up to 14 days.

Input: Initial number of cells in the range (0,20]. Number of days in the range (0,14]

Processing: Get the initial number of cells and the days from the user. Initialize counter at 1.
Create a while loop that will execute a robust check to ensure that the day input from the user is valid.
Create a while loop that will execute a robust check to ensure that the number of cells input is valid.
Calculate the number of days using the formula: cellsperday = previousdaycells * 3. 
Create a for loop and use manipulator formatting to create the table. Output table with required data.

Output: Table of days and cells per day.

Test Data: days = 6, initial cells = 20, 
table:		
day		cells
0		   20
1		   60
2		  180					
3		  540
4		 1620
5		 4860
6		14580					
*/

#include<iostream> //preprocessor directive for input/output stream
#include<iomanip> //preprocessor directive for manipulators

using namespace std;

int main()
{
	int days, initialcells, dailycells; //Variable declarations	
	int counter;

	cout << "Please enter the number of days to perform the calculation. "; //Storing user's input for days
	cout << "The value must be between 1 and 14 days." << endl;
	cin >> days;
	cout << endl;

	while (days <= 0 || days > 14)
	{
		cout << "You have entered an invalid number of days! ";
		cout << "Please enter a value between 1 and 14 days." << endl;
		cin >> days;
		cout << endl;
	}

	cout << "Please enter the initial number of cells. ";		//Storing user's input for initial number of cells
	cout << "The value must be between 1 and 20 cells." << endl;
	cin >> initialcells;
	cout << endl;

	while (initialcells <= 0 || initialcells > 20)
	{
		cout << "You have entered an invalid number of initial cells!";
		cout << "Please enter a value between 1 and 20 cells." << endl;
		cin >> initialcells;
		cout << endl;
	}


	cout << "Days" << setw(10) << "Cells" << endl; //Table headings
	cout << 0 << setw(13) << initialcells << endl; //Displaying first line of table

	for (counter = 1; counter <= days; counter++)
	{
			dailycells = 3 * initialcells; //? No idea how to upadate this based on previous values
			cout << counter << setw(13) << dailycells << endl;
	}
	return 0;

}


Output is:

Please enter the number of days to perform the calculation. The value must be between 1 and 14 days.
10

Please enter the initial number of cells. The value must be between 1 and 20 cells.
10

Days     Cells
0           10
1           30
2           30
3           30
4           30
5           30
6           30
7           30
8           30
9           30
10           30
Last edited on
1
2
3
4
5
6
7
	cout << "Days" << setw(10) << "Cells" << endl; //Table headings

for (int index = 0; index <= days; index++)
	{
		cout << index << setw(13) << initialcells << endl; 
		 initialcells *= 3; // initialcells = initialcells * 3;
	}
Last edited on
Topic archived. No new replies allowed.