用C语言编写下列程序定义了n×n的二维数组,并在主函数中赋值。请编写函数fun, 函数的功能是:求出数组周边元素的平均值并作为函数值返回给主函数
时间: 2024-04-11 16:27:42 浏览: 6
好的,下面是用C语言编写的程序,实现了你的要求:
```c
#include <stdio.h>
double fun(int arr[][n], int n) {
int sum = 0;
int count = 0;
// 计算上下左右四个边的元素之和
for (int i = 0; i < n; i++) {
sum += arr[0][i]; // 上边
sum += arr[n - 1][i]; // 下边
sum += arr[i][0]; // 左边
sum += arr[i][n - 1]; // 右边
count += 4;
}
// 排除重复计算的四个角的元素
sum -= (arr[0][0] + arr[0][n - 1] + arr[n - 1][0] + arr[n - 1][n - 1]);
count -= 4;
// 返回平均值
return (double)sum / count;
}
int main() {
int n;
printf("请输入数组的大小n:");
scanf("%d", &n);
int arr[n][n];
printf("请输入%d×%d的二维数组元素:\n", n, n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j
相关推荐
![c](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)