编写程序计算一个包含10个整数的数组中所有整数的平均值(平均值计算为双精度浮点数)。 在main函数中利用循环输入10个整数,保存在一个数组中,计算均值,然后输出计算结果。 ***输入提示信息:"Please input ten integers:\n" ***每个整数的输入格式要求:"%d" ***输出格式要求:"The mean value is %.2f\n"
时间: 2023-08-09 20:09:36 浏览: 128
好的,以下是您想要的程序代码:
```c++
#include <iostream>
using namespace std;
int main() {
int nums[10];
double sum = 0;
cout << "Please input ten integers:" << endl;
for(int i=0; i<10; i++) {
scanf("%d", &nums[i]);
sum += nums[i];
}
double mean = sum / 10;
printf("The mean value is %.2f\n", mean);
return 0;
}
```
程序首先定义了一个长度为10的整型数组 `nums`,并定义了一个双精度浮点数 `sum`,用于统计数组中所有元素的总和。然后程序利用循环输入10个整数,并将它们保存在数组中,同时计算所有整数的总和。最后,程序计算平均值,输出计算结果,输出格式要求符合题目要求。
相关问题
C语言编写程序计算一个包含10个整数的数组中所有整数的平均值,平均值计算为双精度浮点数,在main函数中利用循环输入十个整数,保存在一个数组中,计算均值,然后输出计算结果
好的,这里是用C语言编写程序计算一个包含10个整数的数组中所有整数的平均值,平均值计算为双精度浮点数的代码:
```c
#include <stdio.h>
int main() {
int arr[10];
double sum = 0.0, avg;
printf("Please enter 10 integers: ");
for (int i = 0; i < 10; i++) {
scanf("%d", &arr[i]);
}
for (int i = 0; i < 10; i++) {
sum += arr[i];
}
avg = sum / 10;
printf("The average is: %.2lf", avg);
return 0;
}
```
这个程序中,我们首先定义了一个包含10个整数的数组`arr`,并定义了一个双精度浮点数变量`sum`和`avg`。然后,我们使用一个for循环来遍历数组,利用`scanf`函数从控制台读入10个整数并保存到数组中。接着,我们再使用一个for循环来遍历数组,将数组中所有整数的和累加到变量`sum`中。最后,我们将累加的和除以数组的长度10,得到平均值,并将其赋值给变量`avg`。最后,我们使用`printf`函数将平均值输出到控制台。注意,我们使用了`%.2lf`格式化字符串来指定输出双精度浮点数时保留两位小数。
2.(10分)编写程序计算一个包含10个整数的数组中所有整数的平均值(平均值计算为双精度浮点数)。 在main函数中利用循环输入10个整数,保存在一个数组中,计算均值,然后输出计算结果。 ***输入提示信息:Please input ten
个整数:***
以下是C++代码实现:
```cpp
#include <iostream>
using namespace std;
int main()
{
int nums[10];
double sum = 0.0, avg;
cout << "Please input ten integers: ";
for (int i = 0; i < 10; i++) {
cin >> nums[i];
sum += nums[i];
}
avg = sum / 10;
cout << "The average is: " << avg << endl;
return 0;
}
```
输出示例:
```
Please input ten integers: 1 2 3 4 5 6 7 8 9 10
The average is: 5.5
```
阅读全文
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![c](https://img-home.csdnimg.cn/images/20250102104920.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://img-home.csdnimg.cn/images/20241231045053.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)