Average

Given an array of integers, we want to implement a method to calculate the average of all the numbers in the array. The average of a set of numbers is the sum of those numbers divided by the count of numbers in the set.

Solution

Java

In Java, we can use a for loop to calculate the sum of the elements in the array and then divide it by the length of the array.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
public class AverageCalculator {
    public static double average(int[] numbers) {
        int sum = 0;
        for (int number : numbers) {
            sum += number;
        }
        return (double) sum / numbers.length;
    }

    public static void main(String[] args) {
        int[] numbers = {10, 20, 30, 40};
        System.out.println("Average: " + average(numbers)); // Output: Average: 25.0
    }
}

C++

In C++, we can use the STL function std::accumulate to calculate the sum, and then divide by the number of elements to calculate the average.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#include <iostream>
#include <numeric>
#include <vector>

double average(const std::vector<int>& numbers) {
    int sum = std::accumulate(numbers.begin(), numbers.end(), 0);
    return static_cast<double>(sum) / numbers.size();
}

int main() {
    std::vector<int> numbers = {10, 20, 30, 40};
    std::cout << "Average: " << average(numbers) << std::endl; // Output: Average: 25
    return 0;
}

Python

In Python, we can use the built-in sum function to calculate the sum and then divide by the length of the list to find the average.

1
2
3
4
5
def average(numbers):
    return sum(numbers) / len(numbers)

numbers = [10, 20, 30, 40]
print("Average:", average(numbers)) # Output: Average: 25.0

In each language, we’re following the same fundamental approach: calculating the sum of the numbers and dividing by the count to get the average. The differences in implementation are mostly due to language-specific features and idioms.

Without Using Builtin Functions

Below are revised solutions for calculating the average of an array of integers without using any built-in functions.

Java:

In Java, we can iterate through the array using a for loop to calculate the sum and then divide by the length of the array.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
public class AverageCalculator {
    public static double average(int[] numbers) {
        int sum = 0;
        for (int number : numbers) {
            sum += number;
        }
        return (double) sum / numbers.length;
    }

    public static void main(String[] args) {
        int[] numbers = {10, 20, 30, 40};
        System.out.println("Average: " + average(numbers)); // Output: Average: 25.0
    }
}

C++

In C++, we can use a loop to iterate through the elements in the vector and add them together, then divide by the size of the vector to find the average.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#include <iostream>
#include <vector>

double average(const std::vector<int>& numbers) {
    int sum = 0;
    for (int number : numbers) {
        sum += number;
    }
    return static_cast<double>(sum) / numbers.size();
}

int main() {
    std::vector<int> numbers = {10, 20, 30, 40};
    std::cout << "Average: " << average(numbers) << std::endl; // Output: Average: 25
    return 0;
}

Python

In Python, we can use a for loop to iterate through the list and add the numbers together. Then we can divide the sum by the length of the list.

1
2
3
4
5
6
7
8
def average(numbers):
    sum = 0
    for number in numbers:
        sum += number
    return sum / len(numbers)

numbers = [10, 20, 30, 40]
print("Average:", average(numbers)) # Output: Average: 25.0

Each of these solutions manually calculates the sum of the elements and then divides by the number of elements in the collection. They provide a clear, simple approach to calculating the average without relying on any language-specific built-in functions.

Coding Skill Exercise #13

Average

Implement a method to find the average of all the numbers in an array.

Knowledge Gap Finder

If you are unable to code the solution, answer the following questions and reply to this email to get customized lesson.

Was the problem statement clear to you when you read it? What did you think of doing first? Were you reminded of a construct in general or a general structure of solution that you thought would be useful? Have you previously seen problems that resemble this one? Did you feel stuck at any point while working on this problem? What did you choose as your test case? Do you think you’ve covered all possible scenarios with your tests? What program design techniques did you apply to solve this problem? Are there any constructs of the programming language that you find difficult or confusing to use? What issues make programming constructs difficult to use? For example, the keyword used, the syntax, the examples, the documentation for the construct, etc.

Feel free to forward this email to your friends so they can subscribe here. https://codingskill.biz