How To average the Numbers in my Array?

• Question

• Hello,

Currently i have a program where i am using an array of numbers from a text file and listing them in a list box. also im am trying to get the average of the numbers in the array, but am having a problem i cant seem to figure out how to the average this is what i have right now. i have the program displaying the numbers in the text file in a list box.

The numbers in the text file are

1245.67
1189.55
1098.72
1456.88
2109.34
1987.55
1872.36

this is the code so far

private void button1_Click(object sender, EventArgs e)
{

int j;
int holder;

//display the array
for (j = 0; j < list.Length; j++)
{

holder = j + 1;
lstbxArray.Items.Add("Sale " + j +  " = " +list[j].ToString());

}

}

i you could help me get the average of those number that would be great

Thursday, December 4, 2014 7:37 PM

• ```   var average = Array.ConvertAll(list, Double.Parse).Sum() / list.Length;
Console.WriteLine(average);```

• Proposed as answer by Thursday, December 4, 2014 8:31 PM
• Marked as answer by Monday, December 15, 2014 7:26 AM
Thursday, December 4, 2014 8:20 PM

double total = 0;

for (j = 0; j < list.Length; j++)
{
holder = j + 1;
lstbxArray.Items.Add("Sale " + j +  " = " +list[j].ToString());
total += list[j];
}

double average = (double)total/list.length;

• Proposed as answer by Thursday, December 4, 2014 8:31 PM
• Marked as answer by Monday, December 15, 2014 7:26 AM
Thursday, December 4, 2014 8:14 PM

All replies

• To take the average you have to add them all up and then divide by the number of elements in the array.

Dim Blondedude092 as SkypeUser

Thursday, December 4, 2014 7:52 PM

double total = 0;

for (j = 0; j < list.Length; j++)
{
holder = j + 1;
lstbxArray.Items.Add("Sale " + j +  " = " +list[j].ToString());
total += list[j];
}

double average = (double)total/list.length;

• Proposed as answer by Thursday, December 4, 2014 8:31 PM
• Marked as answer by Monday, December 15, 2014 7:26 AM
Thursday, December 4, 2014 8:14 PM
• ```   var average = Array.ConvertAll(list, Double.Parse).Sum() / list.Length;
Console.WriteLine(average);```

• Proposed as answer by Thursday, December 4, 2014 8:31 PM
• Marked as answer by Monday, December 15, 2014 7:26 AM
Thursday, December 4, 2014 8:20 PM