Averages/Arithmetic mean: Difference between revisions

Content added Content deleted
(→‎{{header|Python}}: Works with 3.0)
Line 152: Line 152:


=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
<lang csharp>static float avg(ICollection<int> i)
<lang csharp>static double Average(int[] numbers)
{
{
if (numbers == null || numbers.Length == 0)
return i.Sum() / (float)i.Count;
return 0;

double sum = 0;
for (int i = 0; i < numbers.Length; i++)
sum += numbers[i];
return sum/numbers.Length;
}
}


static void Main(string[] args)
static void Main()
{
int[] numbers = new int[] {1, 2, 3, 4, 5, 6, 7, 8};
Console.WriteLine(avg(numbers));
}</lang>

C# already has a builtin Avarage function.

<lang csharp>static void Main(string[] args)
{
{
int[] numbers = new int[] {1, 2, 3, 4, 5, 6, 7, 8};
int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8 };
Console.WriteLine(numbers.Average());
Console.WriteLine(Average(numbers));
}</lang>
}</lang>