使用指针,求十个数的均方差
时间: 2023-07-11 16:07:49 浏览: 275
求均值方差
4星 · 用户满意度95%
假设这十个数存储在数组 `arr` 中,可以按照以下步骤求出均方差:
1. 计算平均值 `avg`:
```c++
double sum = 0;
for (int i = 0; i < 10; i++) {
sum += *(arr + i);
}
double avg = sum / 10;
```
2. 计算每个数与平均值的差的平方,并累加到 `sum_diff` 中:
```c++
double sum_diff = 0;
for (int i = 0; i < 10; i++) {
double diff = *(arr + i) - avg;
sum_diff += diff * diff;
}
```
3. 计算均方差 `std_dev`:
```c++
double std_dev = sqrt(sum_diff / 10);
```
完整代码如下:
阅读全文