In: Computer Science
Create in C Sharp : 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 in text boxes:
Starting number of organisms: 2
Average daily increase: 30%
Number of days to multiply: 10
Hint: (new number) = (old number) * (1 + rate/100)
When the user clicks on a button, the application should use a ListBox control to display the data
using a for loop instead of a while loop
SOLUTION-
I have solve the problem in C# code with comments and screenshot
for easy understanding :)
Note: I have developed this application according to your given formulae. But I think the formulae is wrong. you didn't use a number of days .Anyhow, I developed it.simply replace your new formulae otherwise leave a comment with updated formula.
CODE-
Designing code: Form2.Design.cs
namespace WindowsFormsApplication2
{
partial class Form2
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should
be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.listBox1 = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(41, 41);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(143, 13);
this.label1.TabIndex = 0;
this.label1.Text = "Starting number of organisms";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(208, 38);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(136, 20);
this.textBox1.TabIndex = 1;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(56, 79);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(128, 13);
this.label2.TabIndex = 0;
this.label2.Text = "Average daily increase(%)";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(54, 117);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(130, 13);
this.label3.TabIndex = 0;
this.label3.Text = "Number of days to multiply";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(208, 114);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(136, 20);
this.textBox2.TabIndex = 1;
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(208, 76);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(136, 20);
this.textBox3.TabIndex = 1;
//
// button1
//
this.button1.Font = new System.Drawing.Font("Microsoft Sans Serif",
8.25F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.button1.Location = new System.Drawing.Point(153, 157);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(110, 23);
this.button1.TabIndex = 2;
this.button1.Text = "Population Size";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new
System.EventHandler(this.button1_Click);
//
// listBox1
//
this.listBox1.FormattingEnabled = true;
this.listBox1.Location = new System.Drawing.Point(137, 203);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(136, 160);
this.listBox1.TabIndex = 3;
//
// Form2
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(484, 391);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "Form2";
this.Text = "Form2";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.ListBox listBox1;
}
}
c# code:Form2.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int oldNumber;
if (listBox1.Items.Count > 0)
{
oldNumber = Convert.ToInt32(listBox1.Items[0].ToString());
}
else
{
oldNumber = Convert.ToInt32(textBox1.Text.Trim());
}
int newNumber = oldNumber * (1 + (Convert.ToInt32(textBox3.Text.Trim()) / 100));
listBox1.Items.Clear();
listBox1.Items.Insert(0, newNumber);
}
}
}
Output:
IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I WILL
SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK
YOU!!!!!!!!----------