Like Tree5Likes

Help with Linear Search

This is a discussion on Help with Linear Search within the C Programming forums, part of the General Programming Boards category; Thanks for looking at my thread. I have successfully made a code that with search for mutliplies. But I am ...

  1. #1
    SDH
    SDH is offline
    Registered User
    Join Date
    Dec 2012
    Posts
    67

    Help with Linear Search

    Thanks for looking at my thread. I have successfully made a code that with search for mutliplies.

    But I am now trying to modify it so that I dont have to tell it which numbers to search for. I would like it to just output how many of each number there is.

    Would changing the code so it says that it tries out every single number away, so would using a nested loop with a counter to add one to the value of the number searched for work?

    Here is my code for the linear search for number to help anyone out. Really appreciate anyone who has even looked at my thread let alone replied. Thanks.

    Code:
    #include<stdio.h> 
    int main(void)
    {
     
     
       int array[100], search, c, n, count = 0;
     
       printf("Enter the number of elements in array\n");
       scanf("%d",&n);
     
       printf("Enter %d numbers\n", n);
     
       for ( c = 0 ; c < n ; c++ )
          scanf("%d",&array[c]);
     
       printf("Enter the number to search\n");
       scanf("%d",&search);
     
       for ( c = 0 ; c < n ; c++ )
       {
          if ( array[c] == search )    
          {
             printf("%d is present at location %d.\n", search, c+1);
        count++;
          }
       }
       if ( count == 0 )
          printf("%d is not present in array.\n", search);
       else
          printf("%d is present %d times in array.\n", search, count);
     
        return 0;
      }

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,060
    Quote Originally Posted by SDH View Post
    Would changing the code so it says that it tries out every single number away, so would using a nested loop with a counter to add one to the value of the number searched for work?
    Yes. Make your count an array count[0]...count[100], each member starting at 0. Each time you find the element with value i in array, increment count[i]. Then you have a tally of each value 0...100 that occured in array.

  3. #3
    SDH
    SDH is offline
    Registered User
    Join Date
    Dec 2012
    Posts
    67
    I have done that using
    Code:
     for ( nu = 0; nu <10 ; nu++)
    because I am only inputting values of one to ten into the program. I have go it succesfully find which number are in the input but when I would like it say how many times each one occurs. I am using the statement
    Code:
     printf("%d is present %d times in array.\n", nu, count);
    however this doesnt work. It just counts from 1 to 5. I think it has got something to do with the %d assigned to count but I am not totally sure.
    thanks

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,060
    Make `count' an array rather than a scalar. And post more of the context because it's not clear by the for loop with `nu' that you posted.

  5. #5
    Registered User
    Join Date
    Dec 2012
    Posts
    289
    Quote Originally Posted by SDH View Post
    I have done that using
    Code:
     for ( nu = 0; nu <10 ; nu++)
    because I am only inputting values of one to ten into the program. I have go it succesfully find which number are in the input but when I would like it say how many times each one occurs. I am using the statement
    Code:
     printf("%d is present %d times in array.\n", nu, count);
    however this doesnt work. It just counts from 1 to 5. I think it has got something to do with the %d assigned to count but I am not totally sure.
    thanks
    actually the i have done that part, isnt even in the code you posted!!! so it isnt like we could tell you WHY it dont work, IF it isnt in your code!

  6. #6
    SDH
    SDH is offline
    Registered User
    Join Date
    Dec 2012
    Posts
    67
    Here is the code that I am trying to work on.
    Code:
     #include<stdio.h> 
    int main(void)
    {
     
     
       int array[100], search, c, n, count = 0, number, count1[100];
     
       printf("Enter the number of elements in array\n");
       scanf("%d",&n);
     
       printf("Enter %d numbers\n", n);
     
       for ( c = 0 ; c < n ; c++ )
       
       
          scanf("%d",&array[c]);
     
     
       
       for ( number = 0; number <10 ; number++)
       
       for ( c = 0 ; c < n ; c++ )
       {
          if ( array[c] == number )    
          {
             printf("%d is present at location %d.\n", number, c+1);
                 count++;
                
             
          }
      
    
    
       }
    
    
       if ( count == 0 )
          printf("%d is not present in array.\n", number);
       else
          printf("%d is present %d times in array.\n", number, count);
     
        return 0;
      }
    I have changed nu to number just so it is easier to read.
    The input is -
    5
    1 1 1 7 2

    The output is -
    Enter the number of elements in array
    Enter 5 numbers
    1 is present at location 1.
    1 is present at location 2.
    1 is present at location 3.
    2 is present at location 5.
    7 is present at location 4.
    10 is present 5 times in array.

    But I would like the output to read, "1 is present 3 times in array"
    "2 is present 1 times in array"
    7 is present 1 times in array"




  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by SDH View Post
    Here is the code that I am trying to work on.

    I have changed nu to number just so it is easier to read.
    The input is -
    5
    1 1 1 7 2

    The output is -
    Enter the number of elements in array
    Enter 5 numbers
    1 is present at location 1.
    1 is present at location 2.
    1 is present at location 3.
    2 is present at location 5.
    7 is present at location 4.
    10 is present 5 times in array.

    But I would like the output to read, "1 is present 3 times in array"
    "2 is present 1 times in array"
    7 is present 1 times in array"



    C99Tutorial already stated how to do this. You glossed over that post, without really understanding it!

    Code:
    #include<stdio.h> 
    
    int main(void)
    {
     
       //added i to the list of int variables, and initialize count1 to all zero's
       int array[100], i, search, c, n, count = 0, number, count1[100]={0};
     
       printf("Enter the number of elements in array\n");
       scanf("%d",&n);
     
       printf("Enter %d numbers\n", n);
     
       for ( c = 0 ; c < n ; c++ )
          scanf("%d",&array[c]);
     
       //new: tricky counting
       for(i=0;i<n;i++)   { //for every number
          count1[array[i]]++; //if array[i] is N, then count1[N]++
       }
    
       //new: reporting
       for(i=0;i<100;i++) { //scan the whole range of numbers
          if(count1[i])      //there was at least one with this value
             printf("%d is present %d times in array.\n",i,count[i]);
          else
             printf("%d was not in the array\n",i);      
       }
    
        return 0;
      }
    Don't feel badly though - it's tricky!

  8. #8
    SDH
    SDH is offline
    Registered User
    Join Date
    Dec 2012
    Posts
    67
    Adak - I have tried to compile that but for some reason it wont work :s.

    Says there is an error on line 25. : In function 'main': Line 25 ERROR: subscripted value is neither array nor pointer
    compilation terminated due to -Wfatal-errors.

    I have tried to solve this issue myself but with no luck, any ideas?

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    2,282
    Code:
    printf("%d is present %d times in array.\n",i,count[i]);
    @SDH: It appears to be a minor typo - "count[i]" should be "count1[i]". Notice that, at the top of "main()", "count" is declared as an int, and "count1" is declared as an array of int.

  10. #10
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Quote Originally Posted by c99tutorial View Post
    Yes. Make your count an array count[0]...count[100],..
    while the code is
    Quote Originally Posted by SDH View Post
    Code:
     ...
       int array[100]; 
    ...
    shouldn't we say count[0] to count[99]?
    Or maybe I am mistaken

    //SHD didn't do fortunately the same mistake
    laserlight and Crossfire like this.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Matticus nailed it - yes, count is not an array, it should be count1[i] in that printf() statement.

  12. #12
    SDH
    SDH is offline
    Registered User
    Join Date
    Dec 2012
    Posts
    67
    Thank you very much, I knew it was something simply just could not spot it myself. Thanks

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by SDH View Post
    Thank you very much, I knew it was something simply just could not spot it myself. Thanks
    As you < ahem! > noticed, that code was just "off the cuff", and has not been run (let alone tested). So be sure to test it thoroughly, yourself.

    And you're welcome.

  14. #14
    SDH
    SDH is offline
    Registered User
    Join Date
    Dec 2012
    Posts
    67
    I have no tried to get it to work but with characters. I am once again having problems.
    Here is my code,
    Code:
     #include<stdio.h>  
    void main(void)
    {
      
    
    
       int search, c, n, count = 0, number, count1[100]={0};
       char i, array[100];
      
       printf("Enter the number of elements in array\n");
       scanf("%d",&n);
      
       printf("Enter %d numbers\n", n);
      
       for ( c = 0 ; c < n ; c++ )
          scanf("%c ",&array[c]);
      
          for(i=0;i<n;i++)  
       { 
          count1[array[i]]++;                                                              //if array[i] is N, then count1[N]++//
       }
     
       
       for(i=0;i<100;i++)
        { 
          if(count1[i])     
             printf("%c is present %d times in array.\n",i,count1[i]);
          else
             printf("");      
       }
     
        return 0;
      }
    The input I am testing it with is
    5
    a s a b d

    With the output being -
    Enter the number of elements in array
    Enter 5 numbers


    is present 1 times in array.
    a is present 2 times in array.
    b is present 1 times in array.

    So I am assuming it is something to do with the loops again.

  15. #15
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Change main to return int as you nicely had before

    i is a counter, thus it has to be an integer

    this
    Code:
    printf("Enter %d numbers\n", n);
    should be this
    Code:
    printf("Enter %d characters\n", n);
    :P

    %c will not eat the newline character (..because it is a character!). %d does this.
    So, leave a space before %c, like this
    Code:
    scanf(" %c ",&array[c]);
    Now tell me what does this code do
    Code:
     for(i=0;i<n;i++) 
     {
          count1[array[i]]++;                                                             
     }
    Hint : array is of type char!
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Page 1 of 2 12 LastLast
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. Linear Search Problem
    By Taka in forum C++ Programming
    Replies: 2
    Last Post: 04-30-2009, 04:11 AM
  5. binary search or linear search
    By vajira in forum C Programming
    Replies: 0
    Last Post: 06-05-2003, 12:42 PM

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