我正在尝试创建一个程序,通过调用一个名为sum的函数来查找单个二维数组的总和。但是我遇到了一个错误的错误{但我不确定这是怎么回事?
#include <stdio.h>
int sum(int s[3][4])
int main(int argc, char *argv[])
{
int s[3][4], c, r, sum = 0;
for (r = 0; r <= 2; r++)
{
for (c = 0; c <= 3; c++)
{
printf("Enter value for s[%d][%d]: ", r, c);
scanf("%d", &s[r][c]);
}
}
printf("The sum is: %d\n", sum(s[3][4]);
return 0;
}
int sum (int s[3][4])
{
for (r = 0; r <= 2; r++)
{
for (c = 0; c <= 3; c++)
{
sum = s[r][c] + sum;
}
}
}
答案 0 :(得分:0)
您在这里错过了;
:
int sum(int s[3][4])
答案 1 :(得分:0)
首先,您不能将sum
用作函数名,因为它是预定义的函数。而且你的代码有一些小的语法错误。这是一些固定的代码。
#include <stdio.h>
int sumx(int s[3][4]);
int main(void)
{
int c, r, sum = 0;
int s[3][4]; //declared inside the main
for (r = 0; r <= 2; r++)
{
for (c = 0; c <= 3; c++)
{
printf("Enter value for s[%d][%d]: ", r, c);
scanf("%d", &s[r][c]);
}
}
printf("The sum is: %d\n", sumx(s)); //pass to the function because now it is not global;
return 0;
}
int sumx(int s[3][4])
{
int sum = 0; //sum is initialized to zero otherwise it will take garbage value
for (int r = 0; r <= 2; r++)
{
for (int c = 0; c <= 3; c++)
{
sum = s[r][c] + sum;
}
}
return sum;
}