Linear Sequence Search

This is a discussion on Linear Sequence Search within the C Programming forums, part of the General Programming Boards category; Hi everybody, I just registered to the forum, I will be asking questions so much during those months, hope I ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    5

    Linear Sequence Search

    Hi everybody, I just registered to the forum, I will be asking questions so much during those months, hope I won't be too annoying

    The first thing I want to ask to you is this:
    My professor just gave me this simple program that finds a key in an array..
    Code:
    
    #include <stdio.h>
    #define V 5
    
    
    int seqS(int v[], int l, int r, int k);
    
    
    main()
    {
    	int i, k, a[V];
    
    
        for (i=0; i<V; i++)
        {
            printf("Input a[%d]:", i);
            scanf("%d", &a[i]);
        }
        printf("Input key:  ");
        scanf("%d", &k);
    printf("-1 not found, else index: %d\n", seqS(a, 0, V-1, k)); 
        system("pause");
    }
    
    
    int seqS(int v[], int l, int r, int k) 
    { 
         int i=l;
    
         while (i<r && k!=v[i])
              i++;
         if (i==r)
              return -1;
         return i;
     }
    I tried to change the while part with a for loop but it doesn't seem to work..
    Here's my changes


    Code:
    int seqS(int v[], int l, int r, int k) 
    { 
         int i;
      
    for (i=l; i<r; i++)
     if (v[i]==k)
      return (i);
    else return (-1);
    }
    


    it just keeps giving me -1 in return...
    what did I do wrong?

    Thanks

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Your for loop will only execute at l, that is one time, since the else statement is attached to your if() statement...

    Effectively... "Is this it? No. Oh Darn. Send -1"

    If you construct your loop like this...
    Code:
    int seqs(int v[], int l, int r, int k)
      { int i;
    
        for (i = l; i < r; i++)
          if (v[i] == k)
           return i;
    
        return -1; }
    ... Now each element of the array from l to r-1 will be tested before it returns -1.

    One might quip that you need to be careful or ELSE.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    5
    Oh my god thank you!
    i know it was just a stupid problem... The way I did it, the program would have just searched for k at the first position! In fact if i had put k== v[0] it would have worked!
    Thanks again

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    No worries... it's always the silly stuff that trips me up too...

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    Using braces ALL the time forces you to think a bit more about what is really going on.

    As would a consistent and meaningful approach to indentation.

    I hope you can see how your code would fail just from looking at it, with these ideas in place.
    Code:
    int seqS(int v[], int l, int r, int k) 
    { 
        int i;
    
        for (i=l; i<r; i++) {
            if (v[i]==k) {
                return (i);
            } else {
                return (-1);
            }
        }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linear Search
    By tsmith94 in forum C Programming
    Replies: 5
    Last Post: 10-16-2011, 09:33 PM
  2. Difference Between A Linear Search And Binary Search
    By ImBack92 in forum C Programming
    Replies: 4
    Last Post: 05-12-2011, 08:47 AM
  3. Linear search
    By kingkobra in forum C++ Programming
    Replies: 0
    Last Post: 12-03-2009, 01:42 PM
  4. binary search or linear search
    By vajira in forum C Programming
    Replies: 0
    Last Post: 06-05-2003, 12:42 PM
  5. Linear Search: the recursive way.
    By Nutshell in forum C Programming
    Replies: 7
    Last Post: 01-15-2002, 02:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21