请用C语言编写函数 fun,函数的功能是求出二维数组周边元素绝对值之和,作为函数值返回。二维数组中的值在主函数中赋予。
时间: 2024-05-06 08:20:27 浏览: 17
```c
#include <stdio.h>
#include <stdlib.h>
int fun(int **arr, int row, int col) {
int sum = 0;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (i == 0 || i == row - 1 || j == 0 || j == col - 1) {
sum += abs(arr[i][j]);
}
}
}
return sum;
}
int main() {
int row, col;
printf("请输入二维数组的行数和列数:");
scanf("%d %d", &row, &col);
int **arr = (int **)malloc(sizeof(int *) * row);
for (int i = 0; i < row; i++) {
arr[i] = (int *)malloc(sizeof(int) * col);
printf("请输入第%d行的%d个数:", i + 1, col);
for (int j = 0; j < col; j++) {
scanf("%d", &arr[i][j]);
}
}
int result = fun(arr, row, col);
printf("周边元素绝对值之和为:%d\n", result);
for (int i = 0; i < row; i++) {
free(arr[i]);
}
free(arr);
return 0;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![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_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)