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 an array = (sum of all the elements of the array) / n.

Example (Find mean of an Array):

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

The steps to find mean of an 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 mean of an array is as follows:

/* Program to find mean of an 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 count occurrences of a particular element in an array.
  2. Program to search an element in an Array (Linear Search).
  3. Program to Rotate Array Elements by ‘D’ positions.
  4. Program to find most frequent element in an array.
  5. Program to find pair in an array with given sum.
  6. Program to find first repeating element of an array.
  7. Program to merge two sorted arrays.
  8. Program to find missing number in an array.
  9. Program to sort if array is sorted.
  10. Program to print Alternate Elements of an Array.
  11. Program to Concatenate Two Character Arrays.
  12. Program to Check whether given String is Palindrome or not.
  13. Program to convert lower-case alphabet to upper-case alphabet and vice-versa.
  14. Program to count total number of words in a string.
  15. Program to find smallest and largest word of the String.
  16. Program to find Most Frequent Character of the String.
  17. Program to Remove all the blank spaces from the String.
  18. Program to check if String is isogram or not.
  19. Program to Reverse Each word of the String.
  20. Program to Print All the Substring of the Given String.

You may also like...

Leave a Reply

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