In this page we’ll link and show different algorithm implementations of all the possible online mean/AVG that could be found on interent, by also giving the source of the code.
Arithmetic mean in VB
Function mean(arr) size = UBound(arr) + 1 mean = 0 For i = 0 To UBound(arr) mean = mean + arr(i) Next mean = mean/size End Function 'Example WScript.Echo mean(Array(3,1,4,1,5,9))
- Output:
3.83333333333333
Source: Rosetta code
By clicking the links it’s also possible to see the C# and C++ version.
Geometric mean in VB
Function arithmetic_mean(arr) sum = 0 For i = 0 To UBound(arr) sum = sum + arr(i) Next arithmetic_mean = sum / (UBound(arr)+1) End Function Function geometric_mean(arr) product = 1 For i = 0 To UBound(arr) product = product * arr(i) Next geometric_mean = product ^ (1/(UBound(arr)+1)) End Function Function harmonic_mean(arr) sum = 0 For i = 0 To UBound(arr) sum = sum + (1/arr(i)) Next harmonic_mean = (UBound(arr)+1) / sum End Function WScript.StdOut.WriteLine arithmetic_mean(Array(1,2,3,4,5,6,7,8,9,10)) WScript.StdOut.WriteLine geometric_mean(Array(1,2,3,4,5,6,7,8,9,10)) WScript.StdOut.WriteLine harmonic_mean(Array(1,2,3,4,5,6,7,8,9,10))
- Output:
5.5 4.52872868811677 3.41417152147406
Source: Rosetta code
By clicking the links it’s also possible to see the C# and C++ version.
Quadratic mean in VB
Function rms(iLow As Integer, iHigh As Integer) Dim i As Integer If iLow > iHigh Then i = iLow iLow = iHigh iHigh = i End If For i = iLow To iHigh rms = rms + i ^ 2 Next i rms = Sqr(rms / (iHigh - iLow + 1)) End Function Sub foo() Debug.Print rms(1, 10) End Sub
Output:
6.20483682299543
Source: Rosetta code
By clicking the links it’s also possible to see the C# and C++ version.
Median in C#
using System; using System.Linq; namespace Test { class Program { static void Main() { double[] myArr = new double[] { 1, 5, 3, 6, 4, 2 }; myArr = myArr.OrderBy(i => i).ToArray(); // or Array.Sort(myArr) for in-place sort int mid = myArr.Length / 2; double median; if (myArr.Length % 2 == 0) { //we know its even median = (myArr[mid] + myArr[mid - 1]) / 2.0; } else { //we know its odd median = myArr[mid]; } Console.WriteLine(median); Console.ReadLine(); } } }
Source: Rosetta code
By clicking the links it’s also possible to see the C++ version.
Mode in C#
using System; using System.Collections; using System.Collections.Generic; using System.Linq; namespace Test { class Program { static void Main(string[] args) { /* * We Use Linq To Determine The Mode */ List<int> myList = new List<int>() { 1, 1, 2, 4, 4 }; var query = from numbers in myList //select the numbers group numbers by numbers //group them together so we can get the count into groupedNumbers select new { Number = groupedNumbers.Key, Count = groupedNumbers.Count() }; //so we got a query //find the max of the occurence of the mode int max = query.Max(g => g.Count); IEnumerable<int> modes = query.Where(x => x.Count == max).Select(x => x.Number);//match the frequence and select the number foreach (var item in modes) { Console.WriteLine(item); } Console.ReadLine(); } } }
Source: Rosetta code
By clicking the links it’s also possible to see C++ version.
Standard deviation in C#
using System; using MathNet.Numerics.Statistics; class Program { static void Run(int sampleSize) { double[] X = new double[sampleSize]; var r = new Random(); for (int i = 0; i < sampleSize; i++) X[i] = r.NextDouble(); const int numBuckets = 10; var histogram = new Histogram(X, numBuckets); Console.WriteLine("Sample size: {0:N0}", sampleSize); for (int i = 0; i < numBuckets; i++) { string bar = new String('#', (int)(histogram[i].Count * 360 / sampleSize)); Console.WriteLine(" {0:0.00} : {1}", histogram[i].LowerBound, bar); } var statistics = new DescriptiveStatistics(X); Console.WriteLine(" Mean: " + statistics.Mean); Console.WriteLine("StdDev: " + statistics.StandardDeviation); Console.WriteLine(); } static void Main(string[] args) { Run(100); Run(1000); Run(10000); } }
Source: Rosetta code
By clicking the links it’s also possible to see the C++ version.
Implementation of K-means algorithm in C++
#include<iostream.h>#include<conio.h>void main(){ int i1,i2,i3,t1,t2; int k0[10]; int k1[10]; int k2[10]; cout<<"\nEnter 10 numbers:\n"; for(i1=0;i1<10;i1++) { cin>>k0[i1]; } //initial means int m1; int m2; cout<<"\n Enter initial mean 1:"; cin>>m1; cout<<"\n Enter initial mean 2:"; cin>>m2; int om1,om2; //old means do { //saving old means om1=m1; om2=m2; //creating clusters i1=i2=i3=0; for(i1=0;i1<10;i1++) { //calculating distance to means t1=k0[i1]-m1; if(t1<0){t1=-t1;} t2=k0[i1]-m2; if(t2<0){t2=-t2;} if(t1<t2) { //near to first mean k1[i2]=k0[i1]; i2++; } else { //near to second mean k2[i3]=k0[i1]; i3++; } } t2=0; //calculating new mean for(t1=0;t1<i2;t1++) { t2=t2+k1[t1]; } m1=t2/i2; t2=0; for(t1=0;t1<i3;t1++) { t2=t2+k2[t1]; } m2=t2/i3; //printing clusters cout<<"\nCluster 1:"; for(t1=0;t1<i2;t1++) { cout<<k1[t1]<<" "; } cout<<"\nm1="<<m1; cout<<"\nCluster 2:"; for(t1=0;t1<i3;t1++) { cout<<k2[t1]<<" "; } cout<<"\nm2="<<m2; cout<<"\n ----"; }while(m1!=om1&&m2!=om2); cout<<"\n Clusters created"; //ending getch();}/* OUTPUTEnter 10 numbers:2 4 10 12 3 20 30 11 25 23 Enter initial mean 1:2 Enter initial mean 2:16Cluster 1:2 4 3m1=3Cluster 2:10 12 20 30 11 25 23m2=18 ----Cluster 1:2 4 10 3m1=4Cluster 2:12 20 30 11 25 23m2=20 ----Cluster 1:2 4 10 3 11m1=6Cluster 2:12 20 30 25 23m2=22 ----Cluster 1:2 4 10 12 3 11m1=7Cluster 2:20 30 25 23m2=24 ----Cluster 1:2 4 10 12 3 11m1=7Cluster 2:20 30 25 23m2=24 ---- Clusters created*/Source: ankurm
