Given a class interval and frequency distribution and the task is to find Arithmetic mean. In case of frequency distribution the raw data is arranged by intervals having corresponding frequencies. So if we are interested to find arithmetic mean of the data having class interval we must know the mid variable x. This variable can be calculated by using mid point of interval.
Let lower limit of interval are lower_limit[] = {1, 6, 11, 16, 21}
Upper limit of interval are upper_limit[] = {5, 10, 15, 20, 25}
and frequency freq[] = {10, 20, 30, 40, 50} are given.
Where mid(x) = (lower[i] + upper[i]) / 2;
and mean = (freq[0] * mid[0] + freq[1] * mid[1] + . . . + freq[n – 1] * mid[n – 1]) / (freq[0] + freq[1] + . . . + freq[n-1])
= 2450 / 150
= 16.3333
Examples:
Input : lower_limit[] = {1, 6, 11, 16, 21}
upper_limit[] = {5, 10, 15, 20, 25}
freq[] = {10, 20, 30, 40, 50}
Output : 16.3333
Input : lower_limit[] = {10, 20, 30, 40, 50}
upper_limit[] = {19, 29, 39, 49, 59}
freq[] = {15, 20, 30, 35, 40}
Output : 38.6429
C++
#include <bits/stdc++.h>
using namespace std;
float mean( int lower_limit[], int upper_limit[],
int freq[], int n)
{
float mid[n];
float sum = 0, freqSum = 0;
for ( int i = 0; i < n; i++) {
mid[i] = (lower_limit[i] +
upper_limit[i]) / 2;
sum = sum + mid[i] * freq[i];
freqSum = freqSum + freq[i];
}
return sum / freqSum;
}
int main()
{
int lower_limit[] = { 1, 6, 11, 16, 21 };
int upper_limit[] = { 5, 10, 15, 20, 25 };
int freq[] = { 10, 20, 30, 40, 50 };
int n = sizeof (freq) / sizeof (freq[0]);
cout << mean(lower_limit, upper_limit, freq, n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static float mean( int lower_limit[],
int upper_limit[], int freq[], int n)
{
float mid[] = new float [n];
float sum = 0 , freqSum = 0 ;
for ( int i = 0 ; i < n; i++) {
mid[i] = (lower_limit[i] +
upper_limit[i]) / 2 ;
sum = sum + mid[i] * freq[i];
freqSum = freqSum + freq[i];
}
return sum / freqSum;
}
public static void main (String[] args) {
int lower_limit[] = { 1 , 6 , 11 , 16 , 21 };
int upper_limit[] = { 5 , 10 , 15 , 20 , 25 };
int freq[] = { 10 , 20 , 30 , 40 , 50 };
int n = freq.length;
mean(lower_limit, upper_limit, freq, n);
System.out.println(mean(lower_limit,
upper_limit, freq, n));
}
}
|
Python3
def mean(lower_limit, upper_limit, freq, n):
mid = [ 0.0 ] * n
sum = 0
freqSum = 0
for i in range ( 0 , n):
mid[i] = ((lower_limit[i] +
upper_limit[i]) / 2 )
sum = sum + mid[i] * freq[i]
freqSum = freqSum + freq[i]
return sum / freqSum
lower_limit = [ 1 , 6 , 11 , 16 , 21 ]
upper_limit = [ 5 , 10 , 15 , 20 , 25 ]
freq = [ 10 , 20 , 30 , 40 , 50 ]
n = len (freq)
print ( round (mean(lower_limit, upper_limit,
freq, n), 4 ))
|
C#
using System;
class GFG {
static float mean( int []lower_limit,
int []upper_limit, int []freq, int n)
{
float []mid = new float [n];
float sum = 0, freqSum = 0;
for ( int i = 0; i < n; i++) {
mid[i] = (lower_limit[i] +
upper_limit[i]) / 2;
sum = sum + mid[i] * freq[i];
freqSum = freqSum + freq[i];
}
return sum / freqSum;
}
public static void Main () {
int []lower_limit = { 1, 6, 11, 16, 21 };
int []upper_limit = { 5, 10, 15, 20, 25 };
int []freq = { 10, 20, 30, 40, 50 };
int n = freq.Length;
mean(lower_limit, upper_limit, freq, n);
Console.WriteLine(mean(lower_limit,
upper_limit, freq, n));
}
}
|
PHP
<?php
function mean( $lower_limit , $upper_limit ,
$freq , $n )
{
$mid = array ();
$sum = 0; $freqSum = 0;
for ( $i = 0; $i < $n ; $i ++)
{
$mid [ $i ] = ( $lower_limit [ $i ] +
$upper_limit [ $i ]) / 2;
$sum = $sum + $mid [ $i ] * $freq [ $i ];
$freqSum = $freqSum + $freq [ $i ];
}
return $sum / $freqSum ;
}
$lower_limit = array ( 1, 6, 11, 16, 21 );
$upper_limit = array ( 5, 10, 15, 20, 25 );
$freq = array ( 10, 20, 30, 40, 50 );
$n = count ( $freq );
echo mean( $lower_limit , $upper_limit ,
$freq , $n );
?>
|
Javascript
<script>
function mean(lower_limit,
upper_limit, freq, n)
{
let mid = [];
let sum = 0, freqSum = 0;
for (let i = 0; i < n; i++) {
mid[i] = (lower_limit[i] +
upper_limit[i]) / 2;
sum = sum + mid[i] * freq[i];
freqSum = freqSum + freq[i];
}
return sum / freqSum;
}
let lower_limit = [ 1, 6, 11, 16, 21 ];
let upper_limit = [ 5, 10, 15, 20, 25 ];
let freq = [ 10, 20, 30, 40, 50 ];
let n = freq.length;
mean(lower_limit, upper_limit, freq, n);
document.write(mean(lower_limit,
upper_limit, freq, n));
</script>
|
Approach#2: Using list comprehension and zip
This approach calculates the class interval arithmetic mean given the lower and upper limits of each class interval and their corresponding frequencies.
Algorithm
1. Calculate the number of observations by summing the frequencies: n = sum(freq)
2. Calculate the midpoint of each class interval by taking the average of the lower and upper limits: xi_list = [(a + b) / 2 for a, b in zip(lower_limit, upper_limit)]
3. Calculate the product of each midpoint and its corresponding frequency: fi_xi_list = [a * b for a, b in zip(freq, xi_list)]
4. Calculate the sum of all products: sum_fi_xi = sum(fi_xi_list)
5. Calculate the arithmetic mean by dividing the sum of all products by the total number of observations: arithmetic_mean = sum_fi_xi / n
6. Output the result: print(“Class interval arithmetic mean is:”, arithmetic_mean)
Python3
lower_limit = [ 10 , 20 , 30 , 40 , 50 ]
upper_limit = [ 19 , 29 , 39 , 49 , 59 ]
freq = [ 15 , 20 , 30 , 35 , 40 ]
n = sum (freq)
xi_list = [(a + b) / 2 for a, b in zip (lower_limit, upper_limit)]
fi_xi_list = [a * b for a, b in zip (freq, xi_list)]
arithmetic_mean = sum (fi_xi_list) / n
print ( "Class interval arithmetic mean is:" , arithmetic_mean)
|
Output
Class interval arithmetic mean is: 39.142857142857146
Time complexity: O(n), where n is the number of class intervals. This is because the code performs a single loop over the class intervals to calculate the midpoints, and a second loop to calculate the products.
Space complexity: O(n), as it uses three lists to store the lower limits, upper limits, and frequencies of each class interval, as well as two additional lists for the midpoints and products. However, these additional lists are relatively small compared to the original input lists, so the overall space complexity is still O(n).
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
27 Apr, 2023
Like Article
Save Article