Hi Helios,
try following code:
for (int count = 1; count <= days; count++)
{
populationListBox.Items.Add(count + " " + population);
population += (population * dailyIncrease / 100);
}
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I'm stuck on this program where I have to create a for loop to predict the population after a given amount of days. Here it is:
Create an application that predicts the approximate size of a population of organisms. The application should use text boxes to allow the user to enter the starting number of organisms, the average daily population increase (as a percentage), and the number of days the organisms will be left to multiply. For example, assume the user enters the following values:
Starting number of organisms: 2
Average daily increase: 30%
Number of days to multiply 10
(Days) (Approximate Population)
1 2
2 2.6
3 3.38
4 4.394
5 5.7122
and so on.
But when I run my code this is all that appears:
(Days) (Approximate Population)
10 0
Here is part the code:
public partial class Population : Form
{
int count = 1;
double organisms;
double dailyIncrease;
double days;
double population;
public Population()
{
InitializeComponent();
}
private void calculateButton_Click(object sender, EventArgs e)
{
try
{
organisms = double.Parse(organismsTextBox.Text);
try
{
dailyIncrease = double.Parse(dailyIncreaseTextBox.Text);
try
{
days = double.Parse(daysTextBox.Text);
for (int count = 1; count <= days; count++)
population += (population * dailyIncrease / 100);
populationListBox.Items.Add(days + " " + population);
I'm not sure what I'm doing wrong here?
Hi Helios,
try following code:
for (int count = 1; count <= days; count++)
{
populationListBox.Items.Add(count + " " + population);
population += (population * dailyIncrease / 100);
}
First note that you posted with the dotnet-visual-basic
tag but it is not VB code, it is C#. You should change the
Forum Tag to dotnet-csharp.
Try this code in a simple console program to see if it helps
you focus your ideas for your own code:
int count = 1;
double dailyIncrease = 30;
double days = 10;
double population = 2;
Console.WriteLine("{0} {1}", count++, population);
for ( ; count <= days; count++)
{
population += (population * dailyIncrease / 100);
Console.WriteLine("{0} {1}", count, population);
}
Output:
1 2
2 2.6
3 3.38
4 4.394
5 5.7122
6 7.42586
7 9.653618
8 12.5497034
9 16.31461442
10 21.208998746