# Riddle me this....

This is a discussion on Riddle me this.... within the C Programming forums, part of the General Programming Boards category; I can't understand why user_input isn't being reassigned every time the function is recalled... Code: int search(int population[], int i) ...

1. ## 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. 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.

3. 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. 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. > 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.

6. 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. 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. 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. > else if(cell_number_main=-1)
Now you're using = where you should be using ==

10. Thank you everyone. It works perfectly now.