在程序编辑区编写程序,程序功能如下: 求一维数组a中所有元素的平均值,结果保留两位小数。 例如 输入 10 4 2 7 3 12 5 34 5 9 输出 The aver is:9.10 程序中有两处错误,错误都在提示行: /found/的下面一行,请考生注意。 请改正程序中的错误,使它能得出正确的结果。#include <stdio.h> int main() { int a[10],i; /************found************/ int aver,s; for(i=0;i<10;i++) scanf("%d",&a[i]); /************found************/ s = 0; for ( i=1; i<10; i++) s += a[i]; aver = s / i; printf("The aver is:%.2f\n", aver); }
时间: 2023-07-18 07:40:46 浏览: 849
修改后的程序如下:
```
#include <stdio.h>
int main() {
int a[10], i;
float aver, s; // 修改1:将aver和s的类型改为float
for(i=0;i<10;i++)
scanf("%d",&a[i]);
s = 0;
for (i=0; i<10; i++) // 修改2:将初始值改为0
s += a[i];
aver = s / i;
printf("The aver is:%.2f\n", aver);
return 0; // 添加了return 0
}
```
主要修改如下:
1. 将aver和s的类型改为float,以便计算平均值时得到浮点数结果。
2. 将第二个for循环中i的初始值改为0,以便正确计算数组a的所有元素之和。
3. 添加了return 0,以便在程序正常结束时返回0,表示程序成功完成。
相关问题
用C语言写在程序编辑区编写程序,程序功能如下: 求一维数组a中所有元素的平均值,结果保留两位小数。 例如 输入 10 4 2 7 3 12 5 34 5 9 输出 The aver is:9.10
好的,以下是用C语言编写的程序:
```c
#include <stdio.h>
int main() {
int n;
float sum = 0, aver;
printf("请输入数组的长度:");
scanf("%d", &n);
int a[n];
printf("请输入%d个整数作为数组元素:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
sum += a[i];
}
aver = sum / n;
printf("The aver is:%.2f", aver);
return 0;
}
```
你可以在C语言编译器中运行这个程序,输入数组长度和数组元素,程序会输出平均值,结果保留两位小数。
用C语言写程序:求一维数组a[10]={10,4,2,7,3,12,5,34,5,9}中所有元素的平均值,结果保留两位小数
下面是用 C 语言编写的程序,可以求一维数组 a[10]={10,4,2,7,3,12,5,34,5,9} 中所有元素的平均值,结果保留两位小数:
```c
#include <stdio.h>
int main() {
int a[10] = {10, 4, 2, 7, 3, 12, 5, 34, 5, 9};
int sum = 0;
float avg;
for (int i = 0; i < 10; i++) {
sum += a[i];
}
avg = (float)sum / 10;
printf("平均值为:%.2f\n", avg);
return 0;
}
```
程序的逻辑很简单,就是先遍历一遍数组,求出所有元素的和,然后除以元素个数,即可得到平均值。需要注意的是,为了保留两位小数,我们使用了 %.2f 的格式化输出方式。
阅读全文
相关推荐
![c](https://img-home.csdnimg.cn/images/20250102104920.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)