Skip to content

Instantly share code, notes, and snippets.

@juanfal
Created November 14, 2023 18:31
Show Gist options
  • Save juanfal/656e0030070e0a61c3c544bc059879c0 to your computer and use it in GitHub Desktop.
Save juanfal/656e0030070e0a61c3c544bc059879c0 to your computer and use it in GitHub Desktop.
malaga stats
// t9e13.malagastats.cpp
// juanfc 2023-11-14
// https://gist.github.com/juanfal/656e0030070e0a61c3c544bc059879c0
#include <iostream>
#include <cmath>
#include <array>
using namespace std;
// consts
const int N=100;
// types
typedef array<float,N> TVec;
// prototypes
void writeArr(TVec a);
void stats(TVec a,
float& mean, float& min,
float& max, float& stdev);
int main()
{
float mean, min, max, stdev;
TVec datos =
{{
9.3,15.2,12.3,12.8,12.5,14.9,15.8,12.2,10.6,14.1,14.5,14.5,
13.3,14.4,11.4,11.3,13.5,12.1,12.2,12.0,12.1, 9.1,12.2, 9.8,
8.7,14.1,13.0,15.6,13.4,12.7,11.7,14.3,11.7,14.5,10.4,11.2,
13.1,14.9,14.0,15.2,13.8, 9.4,15.4,14.4,19.7,15.4,17.3,16.5,
17.3,15.7,11.9,14.8,14.6,13.3,14.7,12.9,12.9,13.7,10.2,13.2,
15.0,16.2,12.7, 7.4,13.6,12.4,14.2,19.5,13.3,12.8,14.9,10.7,
13.3,12.3,12.0,12.8,10.3,15.6,14.9,13.6,11.2,11.3,12.7,16.8,
9.1,12.0,17.7,15.5,15.8,15.3,15.2,15.9,11.1,16.7,13.6,12.9,
17.0,17.2,14.2,11.9
}};
writeArr(datos); cout << endl;
stats(datos, mean, min, max, stdev);
cout << "mean: " << mean << endl; // 13.483
cout << "min: " << min << endl; // 7.4
cout << "max: " << max << endl; // 19.7
cout << "std dev: " << stdev << endl; // 2.29475
return 0;
}
void stats(TVec a,
float& mean, float& min,
float& max, float& stdev)
{
mean = 0;
float sa2 = 0, sa = 0;
min = max = a[0];
for (int i = 0; i < N; ++i) {
mean += a[i];
sa += a[i];
sa2 += a[i]*a[i];
if (a[i] < min) min = a[i];
else if (a[i] > max) max = a[i];
}
mean /= N;
stdev = sqrt((sa2-sa*sa/N)/(N-1));
}
void writeArr(TVec a)
{
for (int i = 0; i < N; ++i)
cout << a[i] << " ";
}
void swap(int& a, int& b)
{
int t = a;
a = b; b = t;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment