Thread: Searching In C

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    5

    Post Searching In C

    The code below all works fine when it is to be compiled.
    i can enter the data but the problem lies when i have to search it.
    can anyone of you fine programmers take a quick look and help me solve the problem.
    i try to search but when i do, the program just closes on me.
    I really appreciate it, spent hours on it and achieved nothing.

    Code:
    #include <stdio.h>
    #define MAX 10
    
    	void lsearch(int list[],int n,int element)
       	{
            int i, flag = 0;
            for(i=0;i<n;i++)
            	if(  list[i] == element)
               {
                 printf(" The element whose value is %d is present at position %d in list\n",element,i);
                 flag =1;
                 break;
               }
             if( flag == 0)
              printf("The element whose value is %d is not present in the list\n",element);
          }
    
      void readlist(int list[],int n)
    	{
         int i;
         printf("Enter the elements\n");
         for(i=0;i<n;i++)
             scanf("%d",&list[i]);
        }
    
      void printlist(int list[],int n)
    	{
         int i;
         printf("The elements of the list are: \n");
         for(i=0;i<n;i++)
             printf("%d\t",list[i]);
         }
    
     void main()
     {
       int list[MAX], n, element;
       printf("Enter the number of elements in the list max = 10\n");
       scanf("%d",&n);
       readlist(list,n);
       printf("\nThe list before sorting is:\n");
       printlist(list,n);
       printf("\nEnter the element to be searched\n");
       scanf("%d",&element);
       lsearch(list,n,element);
      }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    main should return a value
    Code:
    int main()
     {
       int list[MAX], n, element;
       int ch;
       
       printf("Enter the number of elements in the list max = 10\n");
       scanf("%d",&n);
       readlist(list,n);
       printf("\nThe list before sorting is:\n");
       printlist(list,n);
       printf("\nEnter the element to be searched\n");
       scanf("%d",&element);
       lsearch(list,n,element);
       while((ch=getchar())!='\n' && ch!=EOF);
       
       getchar();
       return 0;
      }
    ssharish2005

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    5

    Thumbs up

    Thanks a lot my fellow programmers.

    its finally working thank god, but i had to delete the "return = 0;" from the code due to there being an error.

    but never the less, thanks a lot

    really appreciate it

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > but i had to delete the "return = 0;" from the code due to there being an error.
    Lemme guess, you didn't change void main into int main, like everyone told you to.

    Why is that?

  6. #6
    Registered User
    Join Date
    Dec 2005
    Posts
    5

    Lightbulb

    well let me intrege you salem.
    i just copied the main part of the code and pasted into my work to see after spending hours on it, if it works.

    but it did not work. soexperementally i deleted a line and allayluyah it works.

    so im sorry for not carring about ur oppinion. coz to be honest, ur bit of help was as good to me as a kick on the bol*ox.

    and also, i asked for you to look at the code and correct it for me if it was possible. but noooo, u gave me a link about opening the work using command prompt. i never asked for that.

    lemme guess. you never read the question properly like i asked you to.

    why is that?

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Try this:
    Code:
    int main(void) {
        return 0;
    }
    and this:
    Code:
    void main(void) {
        return 0;
    }
    and maybe you'll see what Salem means.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Dec 2005
    Posts
    5
    brother, i did try the int and the void, that wasn't a problem.

    i just dont like to be intimidated or made to look like a fool, when the person whos intimidated gave useless advide in the first place.

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    How exactly was it useless? If you had actually read the information that Salem pointed you to you would have seen this:
    Try reading and implementing the ideas in FAQ "How do I get my program to wait for a keypress?"
    And that's exactly what you ended up doing. Had Salem pointed you directly to that other FAQ you might not have realized what the root of the problem was since you never hinted at it. Next time, try reading information that people post for you. And even better, try searching the FAQ or previous posts for similar problems to the one you're having. Otherwise you just look like a lazy jackass and you'll get corresponding replies.
    If you understand what you're doing, you're not learning anything.

  10. #10
    Registered User
    Join Date
    Dec 2005
    Posts
    5
    LOL
    some people are so pathetic.

    i did not ask for an autobiagraphy.

    i clearly stated that if someone can look at the code and fix it for me so that it works.
    if i wanted a manual on it i could of done that myself.
    just because salem tells you when to open and close your mouth, does not mean i need help like that.

    so get ur facts right sunny jim, and next time, ask salem to check what you are saying before you open your mouth

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So to sum up this thread:
    1 - You're an idiot, who doesn't listen to suggestions.
    2 - You pretend you want to learn C, but you don't care about what the standard states.
    3 - You pretend you want help from people, get mad when people correct your code.
    4 - You don't know what the word 'autobiography' means.
    5 - You type 'ur' because you either:
    5a - Think it saves time.
    5b - Are just lazy.
    5c - Can't spell big words such as "you" and "are".
    6 - Don't really want to learn a damn thing, because if you did, you "could have done that". What you really want is someone to fix your problem for you, so you don't have to do anything. See 5b.

    Ah well, one more idiot for 'The List(TM)'.


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Logic Junkie
    Join Date
    Nov 2005
    Posts
    31
    Quote Originally Posted by shahsoldier
    its finally working thank god, but i had to delete the "return = 0;" from the code due to there being an error.
    Well, if he actually wrote "return = 0;" in his code, I can understand that it didn't work
    -S

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > brother, i did try the int and the void, that wasn't a problem.
    Obviously, you have a different idea of 'problem', one where "It works for me" is good enough.
    C has many traps, way more than you could possibly know, so we point out things which on the face of it make no difference to you today, but will sure enough bite you in the ass if you continue to ignore them.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Visual C++ 2005 linking and file sizes
    By Rune Hunter in forum C++ Programming
    Replies: 2
    Last Post: 11-12-2005, 10:41 PM
  4. Searching and matching strings: segmentation fault
    By Smola in forum C Programming
    Replies: 18
    Last Post: 07-11-2005, 12:25 AM