0

I have to write a function which checks if a value is located in an array with N elements using for loop.I wrote the code

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

When I compile it,it says syntax error before printf.What does this mean?What have I done wrong?

6
  • You need to enclose the printf with brackets { printf();}. You also have to close the bracket for the for loop. And where are the brackets for the main? Commented Apr 16, 2014 at 21:27
  • Putting brackets around printf isn't going to solve this problem. To start with, your entire main function needs brackets; the syntax is something like int main () { ...code goes here... }. Commented Apr 16, 2014 at 21:28
  • 3
    You need to spend some time with a basic book on C syntax. Commented Apr 16, 2014 at 21:28
  • Formatting code is over rated Commented Apr 16, 2014 at 21:30
  • "You need to enclose the printf with brackets { printf();}" -- please don't write ignorant nonsense. Commented Apr 16, 2014 at 21:34

1 Answer 1

2

Basic syntax issues. Try:

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

That said, you have logical problems here:

  1. v is not declared anywhere.
  2. You never populate your array (N).
  3. n is a value entered by the user, not the upper bound of the array. What if I enter 101?

Those are more than syntax issues, you'll need to fix your logic.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.