3

What i am trying to do is to make an WPF Application which allows the user to enter 5 numbers, separated by space, in a text box. Next they press the calculate button which gives them the average of those 5 numbers.

I did this successfully in console mode but now I am really confused on how to go about with this.

I have created the textbox and the button and a textblock where I will show the number. I think I have managed to split the text entered. However, I can't seem to figure out what to do next.

namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        txtbox.Text.Split(' ');
    }
}
}

Thank you very much for the help.

user3639101
  • 31
  • 1
  • 5
  • Should it be hard-coded only for 5 numbers? And what happens if the users exceeds the limit of 5 numbers? – Robert Langdon Jun 25 '14 at 06:33
  • 1
    1) Get your numbers as a sting with `.Text` property of your textbox. 2) Use `String.Split()` method to get each numbers 3) Parse them to `int`. 4) Get sum of them and divide `5`. 5) If your count of numbers can change, assign your int values an array, use `Enumerable.Sum()` method to get total and divide it with `.Count` property. – Soner Gönül Jun 25 '14 at 06:33
  • you need to split your text data by space... – Rahul Jun 25 '14 at 06:33
  • In your console app, create a method takes a string as parameter and return the sum as you want. Simply copy that method to your wpf app and send it the content of your textbox. – liran63 Jun 25 '14 at 06:35
  • @RobertLangdon No it doesn't have to be hardcoded for 5 number. If it exceeds 5 numbers it should include the 6th number as well when calculating the average. – user3639101 Jun 25 '14 at 06:35
  • @liran63 how do I send it to the content of my textbox? – user3639101 Jun 25 '14 at 06:38
  • Don't send it to the content of your textbox; send the content of your textbox to the method as parameter. – liran63 Jun 25 '14 at 06:40
  • @SonerGönül can you show me a code i can use a reference because what you say makes sense to me. But I don't know how to execute it. – user3639101 Jun 25 '14 at 06:52

3 Answers3

6

You can use the array returned by Split to calculate the average of elements using the Average but first you have to convert the array of string to array of numbers like int or double etc.

var avg = txtbox.Text.Split(' ').Select(int.Parse).Average(c=>c);

Assuming you want to get average of double instead of int.

var avg = txtbox.Text.Split(' ').Select(double.Parse).Average(c=>c);

Edit Since we are returning the same value that is being passed to Average method we can use parameter less Average method.

var avg = txtbox.Text.Split(' ').Select(double.Parse).Average();
Adil
  • 146,340
  • 25
  • 209
  • 204
  • what exactly does this do: Average(c=>c) – user3639101 Jun 25 '14 at 06:41
  • The is lambda expression, the first c is for each element of array that is passed as parameter and second c is value being returned from the anonymous method. Although we can omit this. You can read more about the lambda here, http://msdn.microsoft.com/en-us/library/bb397687.aspx – Adil Jun 25 '14 at 06:50
  • Ah ok thanks. Once i use the solution you suggested. I seem to be struggling to get the avg variable to show on the wpf window. Furthermore, when i click the button nothing happens. – user3639101 Jun 25 '14 at 06:56
  • You need to assign the result to some GUI control in order to see result of Form. Suppose you have txtResult in your form then txtResult.Text = avg.ToString(); would show the result on form. – Adil Jun 25 '14 at 06:59
  • ah but when i use the toString method it says there is no double when in fact the parsedouble method is used – user3639101 Jun 25 '14 at 07:05
  • private string Text; public MainWindow() { InitializeComponent(); } private void Button_Click_1(object sender, RoutedEventArgs e) { String[] numbers = txtbox.Text.Split(' '); int sum = 0; foreach (var num in numbers) { sum += Convert.ToInt32(num); } double avg = sum / (double)numbers.Length; txtResult = avg.ToString; } } } – user3639101 Jun 25 '14 at 07:10
  • i just posted the code in the question...well the updated one. – user3639101 Jun 25 '14 at 07:12
  • avg.ToString should be avg.ToString(); – Adil Jun 25 '14 at 07:18
  • ah. cannot implicitly convert type string to system.windows.control.textblock – user3639101 Jun 25 '14 at 07:22
  • Use txtResult.Text = avg.ToString(); – Adil Jun 25 '14 at 07:23
2
String [] numbers = textBox2.Text.Split(' ');
int sum=0;
foreach (var num in numbers)
{
    sum += Convert.ToInt32(num);
}

double avg=sum/(double)numbers.Length;
Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67
1

Split returns a String[]. Save the value in a variable and parse the items to ints/doubles when you sum up the value.

private void Button_Click_1(object sender, RoutedEventArgs e)
{
    string[] items = txtbox.Text.Split(' ');

    //cast and add the values.
}
Hjalmar Z
  • 1,591
  • 1
  • 18
  • 36