Like Tree2Likes
  • 2 Post By quzah

What is sequential search?

This is a discussion on What is sequential search? within the C Programming forums, part of the General Programming Boards category; I have attempted to read my teacher's explanation but I can't figure out what or why.. Could someone tell me ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    9

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

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.
    Last edited by Adak; 07-19-2011 at 09:21 PM.

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    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:
    Quote Originally Posted by Haakon
    Could someone tell me how and why this method works?
    ....it doesn't.
    Last edited by AndrewHunter; 07-19-2011 at 10:31 PM.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,269
    Quote 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.)
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    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.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,269
    Quote 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".
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by laserlight View Post
    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.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  8. #8
    Registered User
    Join Date
    Jul 2011
    Posts
    9
    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. #9
    Registered User
    Join Date
    Jul 2011
    Posts
    1
    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);
     
      }
    Last edited by laserlight; 07-20-2011 at 10:21 AM.

  10. #10
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    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.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,269
    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.

    Quote Originally Posted by AndrewHunter
    Implicit integer returns in main are deprecated
    That is not true.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Jul 2011
    Posts
    9
    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. #13
    Registered User
    Join Date
    May 2009
    Posts
    2,767
    Quote Originally Posted by Haakon View Post
    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.
    Last edited by stahta01; 07-20-2011 at 03:59 PM.

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Haakon View Post
    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. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. please help (sequential search)
    By thestrap in forum C Programming
    Replies: 2
    Last Post: 11-02-2007, 02:17 AM
  2. Storing random number in array(sequential search & bubble sort)
    By dukethacore in forum C++ Programming
    Replies: 5
    Last Post: 07-20-2005, 09:28 AM
  3. Storing random number in array(sequential search & bubble sort)
    By dukethacore in forum C++ Programming
    Replies: 1
    Last Post: 07-20-2005, 08:23 AM
  4. Recursive sequential search
    By supaben34 in forum C++ Programming
    Replies: 2
    Last Post: 10-10-2002, 12:06 PM
  5. Sequential Filenames?
    By Confused in forum C Programming
    Replies: 5
    Last Post: 11-17-2001, 12:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21