Given an array of size n and the task is to find Coefficient of variation . Coefficient of variation is the ratio of standard deviation and mean. The main purpose of coefficient of variation is to find study of quality assurance by measuring the dispersion of the population data of a probability or frequency distribution, or by determining the content or quality of the sample data of substances. The method of measuring the ratio of standard deviation to mean is also known as relative standard deviation often abbreviated as RSD.
Examples :
Input : arr[] = {60.25, 62.38, 65.32, 61.41, 63.23}
Output : 0.0307144
Input : arr[] = {15, 36, 53.67, 25.45, 67.8, 56, 78.09}
Output : 0.48177
Approach:
Coefficient of Variation = Standard deviation / mean
Example:
arr[] = {60.25, 62.38, 65.32, 61.41, 63.23}
mean = 62.518
Standard Deviation = 1.9202
Coefficient of Variation = Standard deviation / mean
= 1.9202 / 62.518
= 0.0307144
Below is the implementation of the above formula :
C++
#include <bits/stdc++.h>
using namespace std;
float mean( float arr[], int n)
{
float sum = 0;
for ( int i = 0; i < n; i++)
sum = sum + arr[i];
return sum / n;
}
float standardDeviation( float arr[], int n)
{
float sum = 0;
for ( int i = 0; i < n; i++)
sum = sum + (arr[i] - mean(arr, n)) *
(arr[i] - mean(arr, n));
return sqrt (sum / (n - 1));
}
float coefficientOfVariation( float arr[], int n)
{
return standardDeviation(arr, n) / mean(arr, n);
}
int main()
{
float arr[] = { 15, 36, 53.67, 25.45,
67.8, 56, 78.09 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << coefficientOfVariation(arr, n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static double mean( double arr[], int n)
{
double sum = 0 ;
for ( int i = 0 ; i < n; i++)
sum = sum + arr[i];
return sum / n;
}
static double standardDeviation( double arr[],
int n)
{
double sum = 0 ;
for ( int i = 0 ; i < n; i++)
sum = sum + (arr[i] - mean(arr, n)) *
(arr[i] - mean(arr, n));
return Math.sqrt(sum / (n - 1 ));
}
static double coefficientOfVariation( double arr[],
int n)
{
return (standardDeviation(arr, n) / mean(arr, n));
}
public static void main (String[] args) {
double arr[] = { 15 , 36 , 53.67 , 25.45 ,
67.8 , 56 , 78.09 };
int n = arr.length;
System.out.println( coefficientOfVariation(arr, n));
}
}
|
Python3
import math
def mean(arr, n):
sum = 0
for i in range ( 0 , n):
sum = sum + arr[i]
return ( sum / n)
def standardDeviation(arr, n):
sum = 0
for i in range ( 0 , n):
sum = ( sum + (arr[i] - mean(arr, n)) *
(arr[i] - mean(arr, n)))
return math.sqrt( sum / (n - 1 ))
def coefficientOfVariation(arr, n):
return (standardDeviation(arr, n) /
mean(arr, n))
arr = [ 15 , 36 , 53.67 , 25.45 ,
67.8 , 56 , 78.09 ]
n = len (arr)
print ( round (coefficientOfVariation(arr, n), 5 ))
|
C#
using System;
class GFG {
static float mean( double []arr, int n)
{
double sum = 0;
for ( int i = 0; i < n; i++)
sum = sum + arr[i];
return ( float )sum / n;
}
static float standardDeviation( double []arr,
int n)
{
double sum = 0;
for ( int i = 0; i < n; i++)
sum = sum + (arr[i] - mean(arr, n)) *
(arr[i] - mean(arr, n));
return ( float )Math.Sqrt(sum / (n - 1));
}
static float coefficientOfVariation( double []arr,
int n)
{
return ( float ) (standardDeviation(arr, n) / mean(arr, n));
}
public static void Main () {
double []arr = { 15, 36, 53.67, 25.45,
67.8, 56, 78.09 };
int n = arr.Length;
Console.WriteLine( coefficientOfVariation(arr, n));
}
}
|
PHP
<?php
function mean( $arr , $n )
{
$sum = 0;
for ( $i = 0; $i < $n ; $i ++)
$sum = $sum + $arr [ $i ];
return $sum / $n ;
}
function standardDeviation( $arr , $n )
{
$sum = 0;
for ( $i = 0; $i < $n ; $i ++)
$sum = $sum + ( $arr [ $i ] -
mean( $arr , $n )) *
( $arr [ $i ] - mean( $arr , $n ));
return sqrt( $sum / ( $n - 1));
}
function coefficientOfVariation( $arr , $n )
{
return standardDeviation( $arr , $n ) /
mean( $arr , $n );
}
$arr = array ( 15, 36, 53.67, 25.45,
67.8, 56, 78.09 );
$n = count ( $arr );
echo coefficientOfVariation( $arr , $n );
?>
|
Javascript
<script>
function mean(arr, n)
{
let sum = 0;
for (let i = 0; i < n; i++)
sum = sum + arr[i];
return sum / n;
}
function standardDeviation(arr, n)
{
let sum = 0;
for (let i = 0; i < n; i++)
sum = sum + (arr[i] - mean(arr, n)) *
(arr[i] - mean(arr, n));
return Math.sqrt(sum / (n - 1));
}
function coefficientOfVariation(arr, n)
{
return (standardDeviation(arr, n) / mean(arr, n));
}
let arr = [ 15, 36, 53.67, 25.45, 67.8, 56, 78.09 ];
let n = arr.length;
document.write( coefficientOfVariation(arr, n).toFixed(5));
</script>
|
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 neveropen!