c语言 数组求平均数
时间: 2024-06-13 21:00:30 浏览: 167
以下是两种C语言数组求平均数的例子:
1.数组求均值并输出大于均值的数
```c
#include <stdio.h>
int main() {
int x;
double sum = 0;
int cnt = 0;
int number[100]; // 定义一个100个元素的数组
scanf("%d", &x);
while (x != -1) {
number[cnt] = x;
sum += x;
cnt++;
scanf("%d", &x);
}
if (cnt > 0) {
double average = sum / cnt;
printf("%f\n", average);
int i;
for (i = 0; i < cnt; i++) {
if (number[i] > average) {
printf("%d\n", number[i]);
}
}
}
return 0;
}
```
2.平均值比较容易得到
```c
#include <stdio.h>
#include <stdlib.h>
#define NUM 10
float Mean(int a[], int n) {
int i;
int sum = 0;
for (i = 0; i < n; i++) {
sum = sum + a[i]; }
return ((float)sum / n);
}
int main() {
int i;
int feedback[NUM] = {0};
for (i = 0; i < NUM; i++) {
printf("请输入第%d个数:", i + 1);
scanf("%d", &feedback[i]);
}
printf("Mean =%3f\n", Mean(feedback, (sizeof(feedback) / sizeof(feedback[0]))));
return 0;
}
```
阅读全文