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 |
<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( |
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 = |
int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8 }; |
||
Console.WriteLine( |
Console.WriteLine(Average(numbers)); |
||
}</lang> |
}</lang> |
||