sorting and searching

This is a discussion on sorting and searching within the C Programming forums, part of the General Programming Boards category; i wana search an array, then sort it, and then want output to display where in the array that element ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    4

    sorting and searching

    i wana search an array, then sort it, and then want output to display where in the array that element was found. this is what i've come up with so far, but something's wrong. can anyone point me in the right direction? i'm not getting any syntax errors, so it's gota be the logic itself.
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    
    main()
    {
    	char name[6][10] =
        {"Annabelle", "BoDidly", "Joseph", "Leona", "Molly", "Osie"};
        int count = 0,
            i, j;
        char str_temp;
    
        for(i = 0; i <= 5; i = i + 1)
    
            printf("%s\n", name[i]);
    
            printf("Enter the Name you are Searching for:  ", name[i]);
            scanf("%s", &name[i]);
    
        if("%s" = &name[i])
        printf("The Name you Entered was Found. It was in position %d of the array.", &name[i]);
        else
        printf("Sorry, but the name you entered was not Found.");
    
     	getch();
    
    }
    Any replies to this thread greatly appreciated. Thanks

  2. #2
    Registered User
    Join Date
    Aug 2002
    Posts
    34

    Arrow

    There are a fair few errors in this from what i can see. You are mainly using the one variable name - that being name[i]

    Code:
    printf("Enter the Name you are Searching for:  ", name[i]);
            scanf("%s", &name[i]);
    
        if("%s" = &name[i])
    If I can understand what you are trying to do you can't really say if(name[i] == name[i]) because it will always be true, so what i suggest is that as a start you change
    Code:
    printf("Enter the Name you are Searching for:  ", name[i]);
            scanf("%s", &name[i]);
    to
    Code:
    printf("Enter the Name you are Searching for:\n");
            scanf("%s", searchkey);
    You might need to use strcmp() to compare the strings.

    Good Luck

  3. #3
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Here's another error :

    >> scanf("%s", &name[i]);

    Drop the ampersand '&', should be scanf("%s", name[i]);

    >> char str_temp;

    Should be char str_temp[SOME_VALUE];, which causes the above statement to change to scanf("%s", str_temp);

    Also, you'll have to test each string with the user's input, like this :
    Code:
    int found = 0;
    
    for( i = 0; i < 6; i++ )
    {
          if( strcmp( name[i], str_temp ) == 0 )
          {
               printf( "The Name you Entered was Found. It was in position %d of the array.", i + 1 );
               return getch();
          }
    }
    
    printf("Sorry, but the name you entered was not Found.");
    return getch();

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. *Help*in sorting and searching algorithm
    By yuentong in forum C++ Programming
    Replies: 1
    Last Post: 03-07-2009, 09:43 AM
  2. Doxygen failing
    By Elysia in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 04-16-2008, 01:24 PM
  3. who has made simple searching engine?
    By oshindeler in forum C++ Programming
    Replies: 2
    Last Post: 08-03-2006, 10:41 AM
  4. understanding searching and sorting
    By volk in forum C++ Programming
    Replies: 3
    Last Post: 04-30-2003, 09:44 AM
  5. problem in sorting and searching
    By ygfperson in forum C++ Programming
    Replies: 3
    Last Post: 04-16-2002, 07:47 PM

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