题目:程序定义了N×N的二维数组,并在主函数中赋值。 请编写函数fun,函数的功能是:求出数组周边元素的平均值 并作为函数值返给主函数中的s。 -------------------------------------------------------*/ #include <stdio.h> #include <stdlib.h> #define N 5 double fun ( int w[][N] ) { /**********Program**********/ /********** End **********/ } main ( ) { int a[N][N]={0,1,2,7,9,1,9,7,4,5,2,3,8,3,1,4,5,6,8,2,5,9,1,4,1}; int i, j; double s ; printf("***** The array *****\n"); for ( i =0; i<N; i++ ) { for ( j =0; j<N; j++ ) { printf( "%4d", a[i][j] ); } printf("\n"); } s = fun ( a ); printf ("***** THE RESULT *****\n"); printf( "The sum is : %lf\n",s ); }
时间: 2023-07-18 22:42:30 浏览: 69
这段代码定义了一个N×N的二维数组a,并调用了函数fun来求出数组周边元素的平均值并作为函数值返回给主函数中的s。最后输出原始的数组a和求得的平均值s。
以下是函数fun的实现:
```
double fun(int w[][N]) {
int i, j;
double sum = 0;
int count = 0;
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
if (i == 0 || i == N - 1 || j == 0 || j == N - 1) {
sum += w[i][j];
count++;
}
}
}
return sum / count;
}
```
该函数使用两层循环遍历二维数组w,对于每一个边界元素(即第一行、最后一行、第一列、最后一列的元素),将其值累加到sum中,并统计边界元素的个数。最后将sum除以边界元素个数得到平均值,并将其返回。
相关问题
题目:程序定义了N×N的二维数组,并在主函数中赋值。 请编写函数fun,函数的功能是:求出数组周边元素的平均值 并作为函数值返给主函数中的s。
以下是实现该功能的代码:
```c
#include <stdio.h>
#define N 3
float fun(int arr[][N]) {
float sum = 0;
int count = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (i == 0 || i == N-1 || j == 0 || j == N-1) {
sum += arr[i][j];
count++;
}
}
}
return sum / count;
}
int main() {
int arr[N][N] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
float s = fun(arr);
printf("The average of the elements around the array is: %.2f\n", s);
return 0;
}
```
在函数`fun`中,我们遍历了数组的周边元素,并计算它们的和。同时,我们也记录了周边元素的个数。最后,我们通过除以周边元素的个数,计算出了平均值,并将其作为函数的返回值。在主函数中,我们调用`fun`函数,并将其返回值打印出来。
程序定义了N×N的二维数组,并在主函数中赋值。请编写函数fun,函数的功能是:求出数组周边元素的平均值并作为函数值返给主函数中的s
好的,这个问题我可以回答。请看下面的代码:
```c
#include <stdio.h>
double fun(int a[][N], int n) {
double sum = ;
int count = ;
for (int i = ; i < n; i++) {
for (int j = ; j < n; j++) {
if (i == || i == n - 1 || j == || j == n - 1) {
sum += a[i][j];
count++;
}
}
}
return sum / count;
}
int main() {
int a[N][N] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
double s = fun(a, N);
printf("The average value of the elements around the array is: %f\n", s);
return ;
}
```
这个程序定义了一个N×N的二维数组a,并在主函数中赋值。函数fun的功能是求出数组周边元素的平均值并作为函数值返回给主函数中的s。具体实现是,遍历数组a的所有元素,如果该元素在数组的周边,则将其加入sum中,并将count加1。最后返回sum/count即可。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://img-home.csdnimg.cn/images/20210720083646.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)