Reputation: 223
I'm trying to implement a linear search function to search for a particular number in which the user "inputs" however, say for example the user wants to search for the number 3 in the given array. The function should return an index value of 2. But my code returns 6 no matter what input I enter. I suspect there is something wrong in my main function (maybe while I am using a for loop, the value of i gets fixed to 6?). Any ideas?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define numberOfElements 6
#define NOT_FOUND -1
int linearSearch (int *myArray, int key, int i);
int main (int argc, char *argv[]){
int i, key, myArray[] = {1, 2, 3, 4, 5, 6};
printf("Input: ");
for (i = 0; i < numberOfElements; i++){
printf("%d ", myArray[i]);
}
printf("\n");
printf("Please enter a number you wish to search for: ");
scanf("%d", &key);
linearSearch (myArray, key, i);
printf("The number %d is at index %d\n", key, i);
return 0;
}
int linearSearch (int *myArray, int key, int i) {
for (i = 0; i < numberOfElements; i++){
printf("Checking index %d\n", i);
if (myArray[i] == key){
printf("%d\n", i);
return i;
break;
}
printf("It's certainly not here!\n");
}
return NOT_FOUND;
}
Upvotes: 0
Views: 156
Reputation: 319
After you first do the for
loop to list your input array, the i
value is 6
.
Then you reuse the i
as a parameter of linearSearch
.
You should know that when you call linearSearch
with parameter i
, you just put the value of i
to the stack, and in the dinner of the function, any change to i
will not affect the value of outside i
. So you always get a 6
.
To correct this, you can change function call from
linearSearch (myArray, key, i);
to
i = linearSearch (myArray, key, i);
Then i
will be changed to the return value.
Upvotes: 0
Reputation: 27549
You are not capturing the returned value of linearSearch
.
You are passing it the value of i
, but not a reference to i
. Therefore, it doesn't matter what value you assign to it, it will not be available in linearSearch
's calling context.
As a result, you are ignoring anything linearSearch
does.
I'd suggest removing the third parameter from linearSearch
and just capturing its return value in a new variable and printing that.
Make the i
inside linearSearch
a local variable instead of an input parameter.
int linearSearch (int *myArray, int key) {
for (int i = 0; i < numberOfElements; i++){
printf("Checking index %d\n", i);
if (myArray[i] == key){
printf("%d\n", i);
return i;
break;
}
printf("It's certainly not here!\n");
}
return NOT_FOUND;
}
/* ... */
int index = linearSearch (myArray, key);
printf("The number %d is at index %d\n", key, index);
Upvotes: 4
Reputation: 1
//It can work well if you correct it like this.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define numberOfElements 6
#define NOT_FOUND -1
int linearSearch (int *myArray, int key, int& i); **//"int i" changed to "int& i"**
int main (int argc, char *argv[]){
int i, key, myArray[] = {1, 2, 3, 4, 5, 6};
printf("Input: ");
for (i = 0; i < numberOfElements; i++){
printf("%d ", myArray[i]);
}
printf("\n");
printf("Please enter a number you wish to search for: ");
scanf("%d", &key);
linearSearch (myArray, key, i);
if(i != numberOfElements)
printf("The number %d is at index %d\n", key, i);
return 0;
}
int linearSearch (int *myArray, int key, int& i) { //**"int i" changed to "int& i"**
for (i = 0; i < numberOfElements; i++){
printf("Checking index %d\n", i);
if (myArray[i] == key){
printf("%d\n", i);
//return i; **//delete this statement**
break;
}
printf("It's certainly not here!\n");
}
return NOT_FOUND;
}
Upvotes: -1
Reputation: 51
You need to take care of the following things.
linearSearch function returns the value i.
k= linearSearch (myArray, key);
then u can print the value like this.
printf("The number %d is at index %d\n", key, k);
Upvotes: 0
Reputation: 29
Make this changes to your code:
int linearSearch (int *myArray, int key);
i = linearSearch (myArray, key);
int linearSearch (int *myArray, int key, int i) {
int i;
Upvotes: 0
Reputation: 3684
You ignore the return value from the linearSearch
function. Hence you don't print the answer.
Note that you should not pass i
into the function but instead declare it within the loop:
for (int i=0; i<... etc
Upvotes: 5