Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to find the average of elements of an integer array in C#?
Finding the average of elements in an integer array is a common programming task in C#. The average is calculated by dividing the sum of all elements by the total number of elements in the array.
Syntax
The basic approach involves three steps −
int[] array = {element1, element2, element3, ...};
int sum = 0;
for (int i = 0; i < array.Length; i++) {
sum += array[i];
}
int average = sum / array.Length;
Using For Loop to Calculate Average
The traditional approach uses a for loop to iterate through the array and accumulate the sum −
using System;
public class Program {
public static void Main() {
int[] myArr = new int[6] {
8,
4,
2,
5,
9,
14
};
int len = myArr.Length;
int sum = 0;
int average = 0;
for (int i = 0; i < len; i++) {
sum += myArr[i];
}
average = sum / len;
Console.WriteLine("Array elements: [" + string.Join(", ", myArr) + "]");
Console.WriteLine("Sum = " + sum);
Console.WriteLine("Average of integer elements = " + average);
}
}
The output of the above code is −
Array elements: [8, 4, 2, 5, 9, 14] Sum = 42 Average of integer elements = 7
Using LINQ Average Method
C# provides a built-in Average() method through LINQ that simplifies the calculation −
using System;
using System.Linq;
public class Program {
public static void Main() {
int[] myArr = {15, 10, 5, 20, 25, 30};
double average = myArr.Average();
Console.WriteLine("Array elements: [" + string.Join(", ", myArr) + "]");
Console.WriteLine("Average using LINQ: " + average);
}
}
The output of the above code is −
Array elements: [15, 10, 5, 20, 25, 30] Average using LINQ: 17.5
Handling Decimal Precision
When calculating averages, consider using double to preserve decimal precision −
using System;
public class Program {
public static void Main() {
int[] numbers = {7, 8, 9};
// Integer division (truncates decimal)
int intAverage = numbers.Sum() / numbers.Length;
// Double division (preserves decimal)
double doubleAverage = (double)numbers.Sum() / numbers.Length;
Console.WriteLine("Array elements: [" + string.Join(", ", numbers) + "]");
Console.WriteLine("Integer average: " + intAverage);
Console.WriteLine("Double average: " + doubleAverage);
}
}
The output of the above code is −
Array elements: [7, 8, 9] Integer average: 8 Double average: 8
Comparison of Methods
| Method | Advantages | Disadvantages |
|---|---|---|
| For Loop | Full control, no external dependencies | More verbose, manual implementation |
| LINQ Average() | Concise, built-in method | Requires System.Linq namespace |
| Double casting | Preserves decimal precision | May not be needed for integer results |
Conclusion
Finding the average of an integer array in C# can be accomplished using a traditional for loop or the built-in LINQ Average() method. Consider using double data types when decimal precision is important for your calculations.
