# Thread: What is sequential search?

1. ## What is sequential search?

I have attempted to read my teacher's explanation but I can't figure out what or why.. Could someone tell me how and why this method works?

Code:
```Use a for loop that starts at index 0 and traverses through all
elements of the array

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)
else
printf(“Item found - %d “, nums[i])```
I appreciate your time,
Thank you!

2. Sequential search is simply iterating through the items, one by one, until the search ends. In the for loop, the variable i, starts at 0, and stops when either the value of i is no longer less than 7, or if nums[i] == 11. Note that index is one less than the values that the array[i] is given, in this program.

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

Originally Posted by Haakon
Could someone tell me how and why this method works?
....it doesn't.

4. Originally Posted by AndrewHunter
....it doesn't.
Rather, the implementation of sequential search that you posted does not work. (Although it coincidentally gives the correct answer in this case, assuming that the missing semi-colon is added in, and you fix those weird double quotes that aren't.)

5. That wasn't my implementation Laser, it was the OP. I was using the red highlighting to point out what was wrong, which I pointed out in statement 2.

6. Originally Posted by AndrewHunter
That wasn't my implementation Laser, it was the OP. I was using the red highlighting to point out what was wrong, which I pointed out in statement 2.
Obviously "you" refers to Haakon
It might not even be Haakon's implementation (e.g., it could have been a mistake in the teacher's example), which is why I stated that it was "the implementation of sequential search that you posted".

7. Originally Posted by laserlight
Obviously "you" refers to Haakon
It might not even be Haakon's implementation (e.g., it could have been a mistake in the teacher's example), which is why I stated that it was "the implementation of sequential search that you posted".
My bad Laser. I see what you (meaning you) were saying now.

8. Correct laser, I was extremely confused as I have spent at least an hour just looking through the code hoping that it will click for me..

Andrew, thanks for clarifying. Just changing that if statement to what you showed make sense. I was so confused why she sat used = 7, then later it has an if statement thats only valid if it is greater than or equal to 7.

Thanks again to both of you.

9. Code:
```
#include<stdio.h>

int sequential_search(char *items, int count, char key)
{
register int t;

for(t=0; t < count; ++t)
if(key == items[t]) return t;
return -1; /* no match */
}

int main(void){
char *str = "asdf";

int index = sequential_search(str, 4, 's');

printf("%d",index);

}```

10. Thank you for posting something that really had nothing to do with the topic at hand with the exception of that it is a sequential search. Also....

Register keyword....really? Although it is 'cute' and I love putting random keywords into my programs to 'spice things up', it is not needed here and it is important to note that the compiler will just ignore you and put the variable where it wants to anyway.

Although since you like keywords, may I suggest the real interesting return statement......Implicit integer returns in main are deprecated; you need to positively return a value to the OS.

11. Mallory: I have removed your link because it is just advertisement. In the future, place such personal links in your signature instead. If you do not yet have the permission to set a signature, hang around first and contribute sensibly, then such an option will become available to you.

Originally Posted by AndrewHunter
Implicit integer returns in main are deprecated
That is not true.

12. So I understand the point of sequential search now, however with the task I've given I'm not sure if my code still is sequential search.. Yes or no?

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
int fives[10] = {0};
int i = 0;
int used = 10;
srand(time(NULL));

//Generate & Assign Values to array
for(i=0; i<=used; i++)
{
fives[i] = rand()%10+1;

if(fives[i] == 5)
{
printf("*");
}

printf("%d\n", fives[i]);
}

return 0;
}```
In many explenations it seems as if the for loop does a search for the desired value, and if it finds it, it breaks and send the info to an if statement showing if the value was found. If I need to complete 10 values, I cant break the for loop. Is it still considered a sequential search if I move the if statement inside the for loop?

13. Originally Posted by Haakon
So I understand the point of sequential search now, however with the task I've given I'm not sure if my code still is sequential search.. Yes or no?
No, it needs to be a search before it can be an sequential search.

You have to initialize the array before you can/should search it. I would mark off for this; I would bet you are supposed to write a search function not a comparison test. Edit: Technically it might count as a search; but, I would not think most graders will agree.

See Mallory post for a search function example.

Edit: My post assumes you have learned to write functions; if you have not, then the assignment (as described by you) makes little sense.

Edit: If you declare a array "int fives[10]" then you CAN NOT access "fives[10]" because the good indexes go from 0 to 9.
You code will do that because you use " i<=used" in the for loop.

Edit: It is good programming to avoid the use of "magic constants" or "magic numbers"; this is done to make the code easier to understand and modify.
http://en.wikipedia.org/wiki/Magic_n...ical_constants
Note: I would ignore this issue (magic constants) for now; but, realize some graders might mark off for using them.

Tim S.

14. Originally Posted by Haakon
So I understand the point of sequential search now, however with the task I've given I'm not sure if my code still is sequential search.. Yes or no?

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
int fives[10] = {0};
int i = 0;
int used = 10;
srand(time(NULL));

//Generate & Assign Values to array
for(i=0; i<=used; i++)
{
fives[i] = rand()%10+1;

if(fives[i] == 5)
{
printf("*");
break;
}

printf("%d\n", fives[i]);
}

return 0;
}```
In many explenations it seems as if the for loop does a search for the desired value, and if it finds it, it breaks and send the info to an if statement showing if the value was found. If I need to complete 10 values, I cant break the for loop. Is it still considered a sequential search if I move the if statement inside the for loop?
First, a sequential search may not mean a full sequential search, of the entire search array space. If you start the search at index 0 of the array, and you find the answer you are searching for at index 1, then you have used sequential search successfully. Searching after you have found the answer(s) you need, is less than clever, surely.

You want to remove the = char colored red, in your code, above. You are searching from 0-10, which is eleven array elements, and your array only has 10 elements (0-9).

If you want to break out of the for loop when you find a certain value, you can use the break code I added, also in red.

A while loop is typically used to loop until a certain condition is met, but the number of times needed to get to that certain test condition, is not known:

Code:
```i = 0;
while(array[i] != 5)
++i;```
Is a simple example of looping until array[i] is equal to 5. Note that if array[0] is equal to 5, then no further looping is done here.

15. Still confused by what a sequential search is? If you read this far, you used a sequential search of the posts in this thread to arrive here.

Quzah.

Popular pages Recent additions