Kotlin Program to Calculate Average Using Arrays

Calculating the average of numbers is a common task in programming. Kotlin, with its expressive syntax and powerful standard library, offers multiple ways to compute the average of values stored in an array. This article will demonstrate three different Kotlin Program to Calculate Average Using Arrays each with detailed examples and outputs.

1. Introduction to Calculating Average Using Arrays

In many applications, calculating the average (or mean) of a set of numbers is essential. The average is the sum of all numbers divided by the count of numbers. Kotlin makes this task straightforward and efficient with its built-in functions and concise syntax.

2. Using Basic Iteration Method

The most fundamental way to calculate the average is to iterate through the array, compute the sum of its elements, and then divide by the number of elements.

Kotlin
fun calculateAverage(arr: Array<Int>): Double {
    var sum = 0
    for (element in arr) {
        sum += element
    }
    return sum.toDouble() / arr.size
}

fun main() {
    val numbers = arrayOf(10, 20, 30, 40, 50)
    val average = calculateAverage(numbers)
    println("Average of the array is: $average")
}

Output

Kotlin
Average of the array is: 30.0

Explanation

In this example, the calculateAverage function iterates through each element in the array, computes the total sum, and then divides the sum by the size of the array to get the average. This method is straightforward and easy to understand.

3. Using Kotlin Standard Library average Functions

Kotlin’s standard library provides built-in functions that can simplify the process of calculating the average.

Kotlin
fun main() {
    val numbers = arrayOf(10, 20, 30, 40, 50)
    val average = numbers.average()
    println("Average of the array is: $average")
}

Output

Kotlin
Average of the array is: 30.0

Explanation

In this example, we use the average extension function provided by Kotlin’s standard library. This function calculates the average of the elements in the array, making the code more concise and readable.

4. Using reduce function

The reduce function applies a binary operation to the elements of the array, reducing them to a single value, which can be used to compute the sum before calculating the average.

Kotlin
fun calculateAverageUsingReduce(arr: Array<Int>): Double {
    val sum = arr.reduce { acc, element -> acc + element }
    return sum.toDouble() / arr.size
}

fun main() {
    val numbers = arrayOf(10, 20, 30, 40, 50)
    val average = calculateAverageUsingReduce(numbers)
    println("Average of the array is: $average")
}

Output

Kotlin
Average of the array is: 30.0

Explanation

In this example, the reduce function is used to calculate the sum of the array’s elements by iterating through them and applying the sum operation. The resulting sum is then divided by the size of the array to compute the average. This method leverages Kotlin’s functional programming capabilities for a more expressive solution.

5. Conclusion

Calculating the average of numbers in an array is a common task that can be achieved in various ways in Kotlin. Depending on the specific requirements and coding preferences, you can choose from different methods:

  1. Basic Loop Method: Manually iterate through the array and compute the sum.
  2. Using average Function: Utilize Kotlin’s standard library for a more concise solution.
  3. Using reduce Function: Apply functional programming techniques to compute the sum and then the average.

Each method has its advantages, and understanding these options allows you to write efficient and readable Kotlin code.

By following these examples, you can easily calculate the average of an array’s elements in Kotlin, whether you’re working with simple arrays or leveraging more advanced functional programming constructs.