An array is a collection of elements stored at contiguous memory locations. Even if you're a beginner, you've likely heard of them as they're the most used data structures in programming.

You must know how to perform basic operations on an array like finding the sum of elements of an array, finding the product of elements of an array, reversing an array, finding the largest and smallest element in an array, etc. to be fully prepared for coding interviews.

In this article, you'll learn how to find the mean of an array using Python, C++, JavaScript, and C.

Problem Statement

You're given an array arr. You need to find the mean of arr.

Example 1: Let arr = [1, 2, 3, 4, 5, 6, 7, 8]

Mean of arr = (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8) / 8 = 4.5

Thus, the output is 4.5.

Example 2: Let arr = [1, 1, 1, 1, 1, 1]

Mean of arr = (1 + 1 + 1 + 1 + 1 + 1) / 6 = 1

Thus, the output is 1.

Formula to find the mean of an array:

Mean of an array = sum of all elements of the array / total no. of elements in the array

Approach to Solve the Problem

You can find the mean of an array by following the approach outlined below:

  1. Initialize a variable sumOfElements (with a value of 0) to store the sum of all elements in the array.
  2. Iterate through the array and add each element of the array with sumOfElements.
  3. Finally, return sumOfElements / sizeOfArray.

C++ Program to Find the Mean of an Array

Below is the C++ program to find the mean of an array:

        // C++ program to find the mean of an array
#include <iostream>
using namespace std;

float calculateMean(int arr[], int size)
{
    int sumOfElements = 0;
    for(int i=0; i<size; i++)
    {
        sumOfElements += arr[i];
    }
    return (float)sumOfElements/(float)size;
}

void printArrayElements(int arr[], int size)
{
    for(int i=0; i<size; i++)
    {
        cout << arr[i] << " ";
    }
    cout << endl;
}

int main()
{
    int arr1[] = {1, 2, 3, 4, 5, 6, 7, 8};
    int size1 = sizeof(arr1)/sizeof(arr1[0]);
    cout << "Array 1:" << endl;
    printArrayElements(arr1, size1);
    cout << "Mean of the array: " << calculateMean(arr1, size1) << endl;

    int arr2[] = {1, 1, 1, 1, 1, 1};
    int size2 = sizeof(arr2)/sizeof(arr2[0]);
    cout << "Array 2:" << endl;
    printArrayElements(arr2, size2);
    cout << "Mean of the array: " << calculateMean(arr2, size2) << endl;

    return 0;
}

Output:

        Array 1:
1 2 3 4 5 6 7 8
Mean of the array: 4.5
Array 2:
1 1 1 1 1 1
Mean of the array: 1

Related: How to Find the Product of All Elements in an Array

Python Program to Find the Mean of an Array

Below is the Python program to find the mean of an array:

        # Python program to find the mean of an array

def calculateMean(arr, size):
    sumOfElements = 0
    for i in range(size):
        sumOfElements += arr[i]
    return sumOfElements/size

def printListElements(arr, size):
    for i in range(size):
        print(arr[i], end=" ")
    print()

arr1 = [1, 2, 3, 4, 5, 6, 7, 8]
size1 = len(arr1)
print("Array 1:")
printListElements(arr1, size1)
print("Mean of the array:", calculateMean(arr1, size1))

arr2 = [1, 1, 1, 1, 1, 1]
size2 = len(arr2)
print("Array 2:")
printListElements(arr2, size2)
print("Mean of the array:", calculateMean(arr2, size2))

Output:

        Array 1:
1 2 3 4 5 6 7 8
Mean of the array: 4.5
Array 2:
1 1 1 1 1 1
Mean of the array: 1.0

Related: How to Remove Duplicate Elements From an Array in JavaScript, Python, and C++

JavaScript Program to Find the Mean of an Array

Below is the JavaScript program to find the mean of an array:

        // JavaScript program to find the mean of an array

function calculateMean(arr, size) {
    let sumOfElements = 0;
    for(let i=0; i<size; i++) {
        sumOfElements += arr[i];
    }
    return sumOfElements/size;
}

function printArrayElements(arr, size) {
    for(let i=0; i<size; i++) {
        document.write(arr[i] + " ");
    }
    document.write("
");
}


var arr1 = [1, 2, 3, 4, 5, 6, 7, 8];
var size1 = arr1.length;
document.write("Array 1:" + "
");
printArrayElements(arr1, size1);
document.write("Mean of the array: " + calculateMean(arr1, size1) + "
");

var arr2 = [1, 1, 1, 1, 1, 1];
var size2 = arr2.length;
document.write("Array 2:" + "
");
printArrayElements(arr2, size2);
document.write("Mean of the array: " + calculateMean(arr2, size2) + "
");

Output:

        Array 1:
1 2 3 4 5 6 7 8
Mean of the array: 4.5
Array 2:
1 1 1 1 1 1
Mean of the array: 1

Related: How to Reverse an Array in C++, Python, and JavaScript

C Program to Find the Mean of an Array

Below is the C program to find the mean of an array:

        // C program to find the mean of an array
#include <stdio.h>

float calculateMean(int arr[], int size)
{
    int sumOfElements = 0;
    for(int i=0; i<size; i++)
    {
        sumOfElements += arr[i];
    }
    return (float)sumOfElements/(float)size;
}

void printArrayElements(int arr[], int size)
{
    for(int i=0; i<size; i++)
    {
        printf("%d ", arr[i]);
    }
    printf("\⁠n");
}

int main()
{
    int arr1[] = {1, 2, 3, 4, 5, 6, 7, 8};
    int size1 = sizeof(arr1)/sizeof(arr1[0]);
    printf("Array 1: \⁠n");
    printArrayElements(arr1, size1);
    printf("Mean of the array: %f \⁠n", calculateMean(arr1, size1));

    int arr2[] = {1, 1, 1, 1, 1, 1};
    int size2 = sizeof(arr2)/sizeof(arr2[0]);
    printf("Array 2: \⁠n");
    printArrayElements(arr2, size2);
    printf("Mean of the array: %f \⁠n", calculateMean(arr2, size2));

    return 0;
}

Output:

        Array 1: 
1 2 3 4 5 6 7 8
Mean of the array: 4.500000
Array 2:
1 1 1 1 1 1
Mean of the array: 1.000000

Related: An Introduction to the Merge Sort Algorithm

Solve Problems Based on Arrays

Arrays are one of the most asked topics in programming interviews. It's wise to practice some of the most common problems based on arrays like finding the maximum and minimum elements of an array, finding the product of all elements in an array, removing duplicate elements from an array, reversing an array, sorting an array, etc. if you're serious about getting a job in the programming field.