Finding the average of all elements in a 2D vector

title: “Finding the Average of All Elements in a 2D Vector” date: “2021-07-14” tags: [programming, vectors, average]


If you’re working with a 2D vector in your programming code and need to find the average of all the elements within it, this blog post will guide you through the process. Whether you’re using C++, Java, Python, or any other programming language, the approach remains the same.

First, let’s start by creating a dummy 2D vector with some example values:

#include <iostream>
#include <vector>

int main() {
    std::vector<std::vector<int>> vec = {
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}
    };

    // Your code to find the average goes here

    return 0;
}

In the above code snippet, we have a 2D vector vec containing three rows and three columns. Now, let’s calculate the average of all elements.

#include <iostream>
#include <vector>

int main() {
    std::vector<std::vector<int>> vec = {
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}
    };

    int sum = 0;
    int count = 0;

    for (const auto& row : vec) {
        for (const auto& element : row) {
            sum += element;
            count++;
        }
    }

    double average = static_cast<double>(sum) / count;

    std::cout << "Average: " << average << std::endl;

    return 0;
}

In the modified code, we have added a sum variable to keep track of the sum of all elements and a count variable to keep track of the total number of elements. We then iterate through each row and each element in the 2D vector, incrementing both the sum and count variables accordingly.

Finally, we calculate the average by dividing the sum by the count. Since both sum and count are integers, we use static_cast<double>(sum) to convert the sum to a double before performing the division.

The calculated average is then printed to the console.

This approach can be easily translated to other programming languages. Just replace the syntax accordingly, but keep the logic intact.

Now that you have the code to find the average of all elements in a 2D vector, you can incorporate it into your own projects and applications. Happy coding!

#programming #vectors #average