Thread: Linear Search

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    20

    Linear Search

    Hi, I'm pretty new to C programming. Writing a program to search through an array and print the number and its position. The program compiles and runs and if the value entered is not present in the array i get the correct result , however if the value is present it prints 0 in position 0. If possible could someone please help me find the error.
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #define SIZE 100
    
    
    int Search(const int set[] , int key, int size);/*Linear search function*/
    
    
    int main(void)
    {
        int num[SIZE];/*Array of numbers*/
        int i;/*counter*/
        int search;/*Integer to be searched for*/
        int element;/*Stores value returned from function*/
        
        for ( i = 0; i < SIZE ; i++)
        {
            num[i] = i * 2;/*Get values for array*/
        }
    
    
        printf("Enter a number to search for\n");
        scanf("%d" , &search);
        
        element = Search(num , search , SIZE);/*Call function*/
    
    
        if (element != -1 )
        {
            printf("The number %d was found at position %d" , num[element] , element);
        }
        else
        {
            printf("Not found\n");
        }
        system("PAUSE");
        return 0;
    }
    
    
    int Search(const int set[] , int key , int size)
    {
        int n = 0;
    
    
        while( (set[n] != key) && (n < size))
        {
            if( set[n] == key)
            {
                return n;
            }
            else
            {
                n++;
            }
        }
       
        if (n >= size)
        {
           return -1;
        }
        
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    I'm thinkin' you're over thinkin'...

    Try this...
    Code:
    int Search(const int set[] , int key , int size)
    {
        int n = 0;
        while(n < size)
        {
            if( set[n] == key)
              return n;
            n++;
        }
        return -1;
    }
    You correctly return the index when you find it...
    The only other thing to do is increment the counter... which won't happen if it returns.
    If the loop ends, it didn't find it so return -1

    You had the right idea... you just went a little overboard.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Since you apparently already know how to use for loops, use a for loop instead of a while loop here.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    20
    Thank you ,its working now. I understand why the extra bits were unnecessary. Learned something today

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by tsmith94 View Post
    Thank you ,its working now. I understand why the extra bits were unnecessary. Learned something today
    To learn some more... try it Lase's way, with a for loop...

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    20
    Thanks. I was over thinking it , is why i didn't use a for loop. I changed it.

Popular pages Recent additions subscribe to a feed

Similar Threads

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