C语言输入一组数求总体标准差,平均数,中位数,总体方差,样本方差,样本标准差,以表格形式输出
时间: 2023-08-09 11:11:21 浏览: 101
以下是一个示例代码,可以根据需要进行修改:
```c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX_SIZE 100
void sort(int arr[], int n) {
int i, j, temp;
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (arr[i] > arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
double mean(int arr[], int n) {
int i;
double sum = 0;
for (i = 0; i < n; i++) {
sum += arr[i];
}
return sum / n;
}
double median(int arr[], int n) {
if (n % 2 == 0) {
return (arr[n / 2 - 1] + arr[n / 2]) / 2.0;
} else {
return arr[n / 2];
}
}
double variance(int arr[], int n) {
int i;
double m = mean(arr, n);
double sum = 0;
for (i = 0; i < n; i++) {
sum += pow(arr[i] - m, 2);
}
return sum / n;
}
double sample_variance(int arr[], int n) {
int i;
double m = mean(arr, n);
double sum = 0;
for (i = 0; i < n; i++) {
sum += pow(arr[i] - m, 2);
}
return sum / (n - 1);
}
double standard_deviation(double variance) {
return sqrt(variance);
}
void print_table(int arr[], int n) {
sort(arr, n);
double m = mean(arr, n);
double med = median(arr, n);
double var = variance(arr, n);
double s_var = sample_variance(arr, n);
double s_dev = standard_deviation(s_var);
double dev = standard_deviation(var);
printf("Number\tValue\n");
printf("------\t-----\n");
printf("N\t%d\n", n);
printf("Mean\t%.2f\n", m);
printf("Median\t%.2f\n", med);
printf("Variance\t%.2f\n", var);
printf("Sample Variance\t%.2f\n", s_var);
printf("Standard Deviation\t%.2f\n", dev);
printf("Sample Standard Deviation\t%.2f\n", s_dev);
}
int main() {
int arr[MAX_SIZE];
int n, i;
printf("Enter the number of values: ");
scanf("%d", &n);
if (n > MAX_SIZE) {
fprintf(stderr, "Error: too many values.\n");
exit(EXIT_FAILURE);
}
printf("Enter the values: ");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
print_table(arr, n);
return 0;
}
```
示例输出:
```
Enter the number of values: 10
Enter the values: 3 6 7 8 9 1 2 5 4 0
Number Value
------ -----
N 10
Mean 4.50
Median 4.50
Variance 8.25
Sample Variance 9.17
Standard Deviation 2.87
Sample Standard Deviation 3.03
```
阅读全文