Thread: Riddle me this....

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    26

    Riddle me this....

    I can't understand why user_input isn't being reassigned every time the function is recalled...

    Code:
    int search(int population[], int i)
        {
            int cell_number;
            int user_input;
            char continue_function[5];
            printf("Enter some integer:");
            scanf("%d", &user_input);
            int j;
    
    
                while (j<i)
                    {
    
    
                        if(user_input == population[j])
                            {
    
    
    
    
                                cell_number = j;
                                printf("the user input is %d.\n", user_input);
                                printf("j is %d\n", j);
                                printf("The number that you entered is in cell number %d\n", cell_number);
                                printf("Would you like to continue? ");
                                scanf("%s", continue_function);
                                    if ( strcmp(continue_function, "yes") == 0)
                                        {
                                            user_input = 0;
                                            printf("Trigger.\n");
                                            int j = 0;
                                            printf("j is %d\n", j);
                                            search(population, i);
    
    
                                        }
                                    if (strcmp(continue_function, "no") == 0)
                                        {
                                            j=i;
                                            printf("j is %d\n", j);
                                            search(population, i);
    
    
                                        }
    /*
    I figured out the problem. It's because we initialized j in the beginning of the program.
    Every time we recall the function, it resets j to zero. So, anything that we had previously is erased...
    
    
    But if we don't initialize it, the first value that we put in is stuck...
    */
    
    
    
    
    
    
                            }
                        if (user_input != population[j])
                            {
                                j++;
                            }
    
    
    
    
                        if(j==(i))
                            {
                                printf("That number is not in the array.\n");
                                printf("Would you like to continue? ");
                                scanf("%s", continue_function);
                                    if (strcmp(continue_function, "yes"))
                                        search(population, i);
                            if(strcmp(continue_function, "no"))
                                j=i;
    
    
                            }
                    }
    
    
                    return 0;
    
    
    
    
        }
    Thanks for the help.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
            int j;
                while (j<i)
    What initial value of j do you have here?

    You should try and write the code as
    Code:
    int done = 0;
    while ( ! done ) {
        // prompt for search value
        // do a search, perhaps using another function which JUST 
        // does a search, without any of the user interaction.
        // prompt for yes/no.  If the answer is "no", then done = 1;
    }
    Because using recursion just to implement a while loop is a really bad idea.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    26
    Considering what you said, I have made these changes. However, I'm not getting the results that I want.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int search(int *, int, int);
    
    
    int main()
        {
            int cell_number;
            int cell_number_main;
            int user_input;
            char continue_function[5];
            int population[10000];
            int i;
            int done=0;
            for (i=0; i<10000; i++)
                {
                    population[i] = rand()%100;
                    //printf("The number added to the array is %d.\n", population[i]);
    
    
                }
    
    
            while(done!=1)
                {
                    printf("Enter some integer: ");
                    scanf("&d", &user_input);
                    search(population, 10000, user_input);
                    printf("\nWould you like to continue? \n");
                    scanf("%s", continue_function);
                    if(strcmp(continue_function, "no")==0)
                        done=1;
                }
    
    
            return 0;
    
    
        }
    
    
    int search(int population[], int i, int user_input)
        {
            int cell_number;
            int j =0;
    
    
            while(j<i)
                {
                    if(user_input==population[j])
                        {
                            cell_number=j;
                            printf("The number that you entered is in cell number %d.", cell_number);
                        }
                    else if(user_input!=population[j])
                        j++;
                    else if(j==i)
                        {
                            printf("That number is not in the array.\n");
                        }
    
    
                }
    
    
    
    
        }

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Your search leaves some to be desired...

    If you only want the first occurance of your user_input value you can simply do this...
    Code:
    int search (int population[], int size, int user_input)
      { int i;
         for (i = 0; i < size; i++)
            if (population[i] == user_input)
              return i;
         return -1; }
    ... which will return the cell number or -1 if the number is not found.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > if(user_input==population[j])
    If this is true, you DON'T increment j, and your loop never exits.

    > else if(j==i)
    This can NEVER be true inside the loop. Or rather it becomes true (once) in another else if branch, but not in this else if branch.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Sep 2011
    Posts
    26
    Okay, I literally see nothing wrong with this code now, and I implemented that for loop because it made more sense to me. But here's what I have now, will someone compile it to see what I'm seeing...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int search(int *, int, int);
    
    
    int main()
        {
            int cell_number;
            int cell_number_main;
            int user_input;
            char continue_function[5];
            int population[10000];
            int i;
            int done=0;
            for (i=0; i<10000; i++)
                {
                    population[i] = rand()%100;
                    //printf("The number added to the array is %d.\n", population[i]);
    
    
                }
    
    
            while(done!=1)
                {
                    printf("Enter some integer: ");
                    scanf("&d", &user_input);
                    cell_number_main=search(population, 10000, user_input);
                    if (cell_number_main!=-1)
                        printf("That number is in cell number %d.\n", cell_number_main);
                    else if(cell_number_main=-1)
                        printf("That number is not in the array.\n");
                    printf("Would you like to continue? \n");
                    scanf("%s", continue_function);
                    if(strcmp(continue_function, "no")==0)
                        done=1;
                }
    
    
            return 0;
    
    
        }
    
    
    int search(int population[], int i, int user_input)
        {
            int j;
            for(j=0;j<i;j++)
                if(population[j]==user_input)
                    return j;
            return -1;
        }

  7. #7
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    Check out this line and you'll find your problem.

    Code:
    while(done!=1)             
    {
    printf("Enter some integer: "); scanf("&d", &user_input); cell_number_main=search(population, 10000, user_input); if (cell_number_main!=-1) printf("That number is in cell number %d.\n", cell_number_main); else if(cell_number_main=-1) printf("That number is not in the array.\n"); printf("Would you like to continue? \n"); scanf("%s", continue_function); if(strcmp(continue_function, "no")==0) done=1;
    }

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    What you literally see is irrelevant, when compared to what the compiler sees. Compile with your warnings turned all the way up (-Wall option for gcc), then fix them all:
    Code:
    $ gcc -Wall search.csearch.c: In function ‘main’:
    search.c:24: warning: too many arguments for format
    search.c:28: warning: suggest parentheses around assignment used as truth value
    search.c:32: warning: implicit declaration of function ‘strcmp’
    search.c:8: warning: unused variable ‘cell_number’
    Line 24: "&d" is not a valid format specifier, try "%d". Also, check the return value of scanf. It returns the number of successful conversions. If this number is not 1 in your program, provide an error message and ask again.
    Line 28: = is for assignment, == is for comparison. Put some space around your operators, and such mistakes become more clear: cell_number_main=-1 vs. cell_number_main = -1
    Line 32: You need to #include <string.h>
    Line 8: Ditch that unused variable.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > else if(cell_number_main=-1)
    Now you're using = where you should be using ==
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Sep 2011
    Posts
    26
    Thank you everyone. It works perfectly now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A riddle : )
    By RoD in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-04-2002, 11:36 AM
  2. riddle me this, batman
    By Aran in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 05-25-2002, 04:19 PM
  3. Riddle me this...
    By QuestionC in forum C Programming
    Replies: 2
    Last Post: 10-26-2001, 10:57 AM
  4. Riddle
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 09-30-2001, 12:13 PM
  5. Another riddle tread!
    By minime6696 in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 08-24-2001, 06:37 AM