我必须编写一个函数来检查一个值是否位于一个包含N个元素的数组中,使用for循环。我写了代码
#include <stdio.h>
int main ()
int N[100],n,i;
printf ("Write the value of n:");
scanf ("%d",&n);
i=0;
printf ("Write the value of the element :");
scanf ("%d",&v[i]);
for (i=0,i<n,i++)
{
if (N[i]==n)
}
printf ("The value is located in the array :");
return 0;
当我编译它时,它会在printf之前说出语法错误。这是什么意思?我做错了什么?
答案 0 :(得分:2)
基本语法问题。尝试:
#include <stdio.h>
int main(void)
{
int N[100],n,i;
printf ("Write the value of n:");
scanf ("%d",&n);
i=0;
printf("Write the value of the element :");
scanf("%d", &v[i]); /* v doesn't exist */
/* you are looping up to n, which could be anything */
for (i=0; i<n; i++)
{
/* you never populate N, so why would you expect n to be found?
the value of any element in N is indeterminate at this point */
if (N[i]==n)
{
printf ("The value is located in the array :");
}
}
return 0;
}
那就是说,你在这里遇到了逻辑问题:
v
未在任何地方声明。N
)。n
是用户输入的值,而不是数组的上限。如果我输入101怎么办?这些不仅仅是语法问题,您还需要修复逻辑。