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; The program is working now but for reason will only recognise a,b,c. Anything after c it will not work out. ...

  1. #16
    SDH
    SDH is offline
    Registered User
    Join Date
    Dec 2012
    Posts
    67
    The program is working now but for reason will only recognise a,b,c. Anything after c it will not work out. Any ideas? Something to do with the array again?
    Thanks for all your help.

  2. #17
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    Well, I hope you read my last post. Post the updated code
    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

  3. #18
    SDH
    SDH is offline
    Registered User
    Join Date
    Dec 2012
    Posts
    67
    I am pretty sure that I have got the code right and made the amendments that you suggested.

    Code:
     #include<stdio.h>  
    void main(void)
    {
      
    
    
       int search, c, n, count = 0, number, count1[100]={0}, i;
       char array[100];
      
       printf("Enter the character of elements in array\n");
       n = 5; 
      
       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;
      }

  4. #19
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    Partially yes

    As said, main has to return int.

    Now, I asked before, what this line does?
    Code:
    count1[array[i]]++;
    and I say again! array is of char type, so you are indexing an array (the count1 array) with a letter. In C, this is not good.
    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

  5. #20
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    But you know what? I actually don't like this approach.

    Think about the problem. You need for every piece of data a corresponding counter. It's a one to one assignment (I don't know if that makes sense as I said, but in math we have some set that every element corresponds to another. Here we have sets of equal size with elements that match to only one. Well, enough with math :P)

    Use a struct, which has as members the piece of data ( a char ) and an integer counter. Then create one array only and then... well I' ll leave that to you. You found your way here, which means that you have understood how things go
    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

  6. #21
    SDH
    SDH is offline
    Registered User
    Join Date
    Dec 2012
    Posts
    67
    Quote Originally Posted by std10093 View Post
    Partially yes

    As said, main has to return int.

    Now, I asked before, what this line does?
    Code:
    count1[array[i]]++;
    This line means if array[i] is N, then count1[N]++ I believe.

  7. #22
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    But array is of char type!!!! In C, you can not index an array with a letter!

    I said my opinion about the approach.
    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

  8. #23
    SDH
    SDH is offline
    Registered User
    Join Date
    Dec 2012
    Posts
    67
    Ok, that was what another user suggested. I am new to programming.

  9. #24
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    Oh, ok then
    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

  10. #25
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your initial post said this was for numbers. Do you need just letters, now? Or letters and numbers both?

  11. #26
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by std10093 View Post
    But array is of char type!!!! In C, you can not index an array with a letter!
    Actually you can because char is just an integer type.

    Quote Originally Posted by SDH View Post
    The program is working now but for reason will only recognise a,b,c. Anything after c it will not work out. Any ideas?
    And here you see how it works (assuming ASCII encoding for the characters):
    The character 'a' has the ASCII value 97, 'b' is 98, 'c' is 99, 'd' is 100, ...

    Now when you do
    Code:
    count1[array[i]]++
    where array[i] holds a character, you get back the ASCII value for it. But since "count1" is declared having 100 elements (with indices 0 to 99) you are writing outside the legal memory for any lowercase character except 'a', 'b' and 'c'.

    If you want to be able to count all characters you have to either increase "count1" (e.g. declaring it as count1[128] for all ASCII characters) or map the ASCII values to a smaller range.

    Bye, Andreas
    Last edited by AndiPersti; 01-14-2013 at 02:14 PM. Reason: typo
    Matticus and SDH like this.

  12. #27
    SDH
    SDH is offline
    Registered User
    Join Date
    Dec 2012
    Posts
    67
    Thank you very much AndiPersti. I have now got it work properly. Hopefully this will be my last post.

  13. #28
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    Oh, that's why it didn't feel right when I was typing it.. Thanks for the correction! And SHD thanks too I guess
    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

  14. #29
    SDH
    SDH is offline
    Registered User
    Join Date
    Dec 2012
    Posts
    67
    Nearly finished on this program! Cant wait to say its actually done! Been my life for the last week ( and yours too ).

  15. #30
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    Don't forget to inform us
    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 2 of 2 FirstFirst 12
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