- 1
How can write a program that accept 10 number from usage &display their standard deviation?
3 Antworten
+ 5
haileyesus geremew try this. You can use modules but this is coded from scratch just using built-in functions:
# Enter numbers separated by spaces.
values = [float(i) for i in input().split()]
n = len(values)
mean = sum(values)/n
std_deviation = (sum((i - mean)**2 for i in values)/n)**.5
print(std_deviation)
https://code.sololearn.com/cOzNY46FH0y0
+ 2
Hope you weren't wanting it in C++ 🙃
+ 2
OK Here it is in C++. Quick and dirty and probably terrible style but it works. ......
#include <iostream>
#include <cmath>
using namespace std;
int main() {
// Enter number of values.
int n;
cin >> n;
// Enter values.
float values[n];
int count;
for (count = 0; count < n; count++)
cin >> values[count];
float sum = 0;
float mean;
float variance = 0;
float stdDeviation;
// Calculate mean.
for (count = 0; count < n; ++count)
sum += values[count];
mean = sum / n;
// Calculate variance.
for (count = 0; count < n; ++count)
variance += pow(values[count] - mean, 2);
// Caculate standard deviation.
stdDeviation = sqrt(variance);
cout << stdDeviation;
}
https://code.sololearn.com/c848rkyvyf6C