下列程序的输出结果是________。 #include <iostream> using namespace std; int fun (int a, int b) { static int m, i=2; i+=m+1; m=i+a+b; return (m); } int main( ) { int k=4, m=1, p; p=fun(k, m); cout<<p<<','; p=fun(k, m); cout<<p<<endl; return 0; }
时间: 2024-02-05 17:02:29 浏览: 119
这段程序的输出结果是:10,18。
这是因为函数fun中定义了静态变量m和i,它们在每次调用fun函数时都不会被重新初始化。在第一次调用fun函数时,i的值会被加上m+1,m的值会被更新为i+a+b即2+4+1+1=8,最终函数返回m的值8+4+1=13。在第二次调用fun函数时,i的值会再次加上m+1,即i=2+8+1=11,m的值会被更新为i+a+b即11+4+1=16,最终函数返回m的值16+4+1=21。因此程序的输出结果为10,18。
相关问题
功能:求10个数中大于平均值的数的个数。 C++函数。 例如:给定10个数为1,3,6,9,4,23,35,67,12,88时,函数值为3。 #include <iostream> #include <fstream>using namespace std;int fun(int a[l,int n)
以下是求10个数中大于平均值的数的个数的C++函数实现:
```
#include <iostream>
using namespace std;
int fun(int a[], int n) {
int sum = 0;
double avg;
for (int i = 0; i < n; i++) {
sum += a[i];
}
avg = (double)sum / n;
int count = 0;
for (int i = 0; i < n; i++) {
if (a[i] > avg) {
count++;
}
}
return count;
}
int main() {
int a[10] = {1, 3, 6, 9, 4, 23, 35, 67, 12, 88};
int res = fun(a, 10);
cout << "大于平均值的数的个数为:" << res << endl;
return 0;
}
```
输出结果为:
```
大于平均值的数的个数为:3
```
功能:有若干个数放入数组中,计算其中正数的平均值aver. C++函数。 #include <iostream> #include <fstream>using namespace std;double fun(int a[10]);void mainO) int a[10]; double av; for(int i=0;i<10;i++) cin>>a[i]; av=fun(a); cout<<av<<endl; double fun(int a[10])
代码如下:
```c++
#include <iostream>
using namespace std;
double fun(int a[10])
{
int count = 0; // 统计正数个数
int sum = 0; // 正数和
for (int i = 0; i < 10; i++) {
if (a[i] > 0) {
count++;
sum += a[i];
}
}
return (double)sum / count;
}
int main()
{
int a[10];
double av;
for(int i=0;i<10;i++)
cin>>a[i];
av = fun(a);
cout<<av<<endl;
return 0;
}
```
函数 `fun` 的作用是计算数组中正数的平均值。首先定义变量 `count` 和 `sum` 分别用于统计正数个数和正数和。然后遍历数组,如果当前元素是正数,就将 `count` 加 1,将 `sum` 加上当前元素的值。最后返回 `sum` 除以 `count` 的结果即可得到正数的平均值。
在主函数中,先读入数组的值,然后调用 `fun` 函数计算平均值,并将结果输出。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)