Ryan Dougan Ryan Dougan - 5 months ago 27
C# Question

I want to pull values from a text box to int array. Then the array will be calculated to find the mean of numbers input

Okay so I want to calculate the mean of an array based on users input. I have a textbox where users will enter numbers and the numbers will be split by a comma. So the text box will look like 23, 11, 24, 45 etc. As you see below I currently have it pulling from hardcoded array for now.

Front End Code

<p>Please enter your numbers within the textbox provided</p>

<asp:TextBox ID="userInput" runat="server"></asp:TextBox>
<br />
<p>The mean is:</p>

<asp:TextBox ID="meanTextBox" runat="server"></asp:TextBox>

Back End Code of Button

protected void calculateResults_Click(object sender, EventArgs e)

int[] numbers = { 4, 4, 4, 4, 3, 2, 2, 2, 1 };

double mean = numbers.Average();

meanTextBox.Text = mean.ToString();

    decimal valueOf = 0;

    var myInts = Array.ConvertAll(userInput.Text.Split(' '), s => decimal.TryParse(s, out valueOf) ? valueOf: 0);

    var mean= myInts.Average() / myInts.Count();
    meanTextBox.Text = mean.ToString();

Here we go.