Skip to content

Instantly share code, notes, and snippets.

@ShrootBuck
Created December 16, 2025 22:45
Show Gist options
  • Select an option

  • Save ShrootBuck/cd014ffa813c0c6c373afe04b22f668f to your computer and use it in GitHub Desktop.

Select an option

Save ShrootBuck/cd014ffa813c0c6c373afe04b22f668f to your computer and use it in GitHub Desktop.
a fast standard deviation + mean calculator for my physics labs
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void setIO(string name = "") {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
if (!name.empty()) {
freopen((name + ".in").c_str(), "r", stdin);
freopen((name + ".out").c_str(), "w", stdout);
}
}
int main() {
setIO("f");
vector<double> values;
double val;
while (cin >> val) {
values.push_back(val);
}
if (values.empty())
return 0;
double sum = 0;
for (double x : values) {
sum += x;
}
double mean = sum / values.size();
double sq_sum = 0;
for (double x : values) {
sq_sum += (x - mean) * (x - mean);
}
double std_dev = sqrt(sq_sum / values.size());
cout << "Average: " << mean << "\n";
cout << "Standard Deviation: " << std_dev << "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment