这段代码有什么问题 #include<stdio.h> int fun(int n,int a[][100]) { int s=0; int i=0,j=0; for(j=0;j<n;j++) { s+=a[0][j]; s+=a[n-1][j]; } for(i=1;i<n-1;i++) { s=s+a[i][0]; s+=a[i][n-1]; } return s; } int main() { int n,a[100][100],i,j; int s; scanf("%d",&n); for(i=0;i<n;i++) for(j=0;j<n;j++) { scanf("%d",&a[i][j]); } s=fun(n,a); printf("%d",s); return 0; }
时间: 2024-03-07 07:49:48 浏览: 31
这段代码的问题在于函数名和参数中的括号不匹配,应该将函数名中的"fun"改为"func"。此外,在第二个for循环中,第二个语句的末尾缺失了"]",应该将"a[i]"改为"a[i][j]"。
正确的代码应该是这样的:
```
#include <stdio.h>
int func(int n, int a[][100])
{
int s = 0;
int i = 0, j = 0;
for(j = 0; j < n; j++)
{
s += a[0][j];
s += a[n - 1][j];
}
for(i = 1; i < n - 1; i++)
{
s += a[i][0];
s += a[i][n - 1];
}
return s;
}
int main()
{
int n, a[100][100], i, j, s;
scanf("%d", &n);
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
scanf("%d", &a[i][j]);
}
}
s = func(n, a);
printf("%d", s);
return 0;
}
```
相关问题
#include <stdio.h> int fun(int x)
#include <stdio.h> 是C语言中的一个预处理指令,它告诉编译器在编译过程中要包含stdio.h头文件。stdio.h是标准输入输出库的头文件,其中包含了一些常用的输入输出函数的声明,比如printf和scanf。
int fun(int x) 是一个函数的声明,它定义了一个名为fun的函数,该函数接受一个整数参数x,并返回一个整数值。函数声明告诉编译器函数的名称、参数类型和返回类型,但不包含函数的具体实现。
*定义函数实现n个字符串按字典顺序排序 开头: #include<stdio.h> #include<string.h> void fun (char *str[],int n)
好的,以下是一个实现n个字符串按字典顺序排序的示例代码:
```
#include <stdio.h>
#include <string.h>
void fun(char *str[], int n) {
int i, j;
char *temp;
// 使用冒泡排序算法
for (i = 0; i < n-1; i++) {
for (j = 0; j < n-i-1; j++) {
if (strcmp(str[j], str[j+1]) > 0) {
// 交换两个字符串的位置
temp = str[j];
str[j] = str[j+1];
str[j+1] = temp;
}
}
}
}
int main() {
char *str[] = {"apple", "banana", "orange", "grape", "kiwi"};
int n = sizeof(str) / sizeof(str[0]);
fun(str, n);
printf("排序后的字符串数组:\n");
for (int i = 0; i < n; i++) {
printf("%s ", str[i]);
}
printf("\n");
return 0;
}
```
在这个示例代码中,我们使用了冒泡排序算法对字符串数组进行排序。在排序过程中,我们使用了 `strcmp()` 函数来比较两个字符串的大小关系,并使用指针变量 `temp` 来交换两个字符串在数组中的位置。最后,我们输出排好序的字符串数组。
请注意,这个示例代码中的字符串数组是在程序中直接定义的。如果您需要从用户输入中获取字符串数据,请使用 `scanf()` 或其他输入函数来读取数据,并将它们存储在字符串数组中。
相关推荐
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)