Find mean of an Array

Find mean of an Array” is a basic problem of array data structure. Here, we are given an array of size ‘n’ and our task is to find and print the mean of the array.

Mean of array = (sum of all the elements of the array) / n.

Example:

INPUT:
Arr[5] = {11, 12, 13, 14, 15}
OUTPUT:
13
Explanation:
Mean = (11+12+13+14+15)/5 = 13

The steps to find the mean of the array are as follows:

  1. Scan array size(N).
  2. Scan array elements.
  3. Compute the sum of all the elements of array by 
  4. Divide the sum by N.
  5. Print the result.

C++ Program to find the mean of the array is as follows:

/* Program to find the mean of the array */  
#include<bits/stdc++.h>  
using namespace std;  
int main()  
{  
    /* Scan array size */  
    int n;  
    cout<<"Enter array size:";  
    cin>>n;  
      
    /*Scan array elements */  
    int arr[n];  
    cout<<"\nEnter array elements: ";  
    for(int i = 0; i < n; i++)  
    cin>>arr[i];  
      
    /* Compute sum */  
    int sum = 0;  
    for(int i = 0; i < n; i++)  
    sum = sum + arr[i];  
      
    /* Divide sum by n */  
    float mean;  
    mean = sum/n;  
      
    /* Printing result */  
    cout<<"\nThe mean of the array is "<<mean;  
}  
OUTPUT:
Enter array size: 5
Enter array elements:  11 12 13 14 15
The mean of the array is 13

Time Complexity:
O(n), where n is the size of the array

Related Posts:

  1. Program to merge two sorted arrays.
  2. Program to find missing number in an array.
  3. Program to sort if array is sorted.
  4. Program to print Alternate Elements of an Array.
  5. Program to swap kth element from beginning to kth element from end in an Array.
  6. Program to print all possible subarrays of the given array.
  7. Program to print kth smallest and kth largest element of an Array.
  8. Program to find equilibrium index of an Array.
  9. Program to find majority element of an Array.
  10.  Program to sort an Array of 0s and 1s.
  11. Program to Reverse an Array.
  12. Program to count number of odd numbers and even numbers in an array.
  13. Program to find absolute difference between sum of odd index elements and even index elements.
  14. Program to find smallest and largest element of the array.
  15. Program to count occurrences of a particular element in an array.
  16. Program to search an element in an Array (Linear Search).
  17. Program to Rotate Array Elements by ‘D’ positions.
  18. Program to find most frequent element in an array.
  19. Program to find pair in an array with given sum.
  20. Program to find first repeating element of an array.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *