2014-04-16 68 views
0

我必須寫,檢查如果一個值位於與使用用於loop.I N個元素的數組寫的代碼的函數C程序陣列和用於循環鍛鍊

#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

您需要用括號'{printf();}'將'printf'括起來。您還必須關閉'for'循環的括號。 「main」的括號在哪裏? – AntonH

+0

圍繞'printf'放置括號不會解決這個問題。首先,你的整個'main'函數需要括號;語法就像'int main(){... code goes here ...}'。 – larsks

+3

您需要花一些時間閱讀關於C語法的基本書籍。 – larsks

回答

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; 
} 

這就是說,你在這裏有邏輯問題:

  1. v不被任何聲明。
  2. 你永遠不會填充你的數組(N)。
  3. n是由用戶輸入的值,而不是數組的上限。如果我輸入101,該怎麼辦?

這些不僅僅是語法問題,你需要修正你的邏輯。