Code:
int nums[7] = {1, 2, 3, 4, 5, 6, 7};
int i = 0;
int used = 7;
for(i = 0; i < used; i++){
if(nums[i] == 11)
break;
}
if(used >= 7)
printf(“Item not found”);
else
printf(“Item found - %d “, nums[i]);<-----Add ;
1. Proper indentation cannot be stressed enough. You should always indent dependent lines of code, it will help you to find potential errors.
2. The first if statement will always evaluate to true in your code (see what is in red). Perhaps the if statement was suppose to be:
Code:
if( i >= used)
printf.....
3. Going along with #2, avoid using "magic numbers". Having number literals in code with no rhyme or reason is one of the easiest ways to make code hard to maintain, update, and troubleshoot.
4. You are missing a semicolon on your final statement.
Adak answered how a sequential search worked, to answer your question:
Originally Posted by
Haakon
Could someone tell me how and why this method works?
....it doesn't.