1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Straight sequential search algorithm

  1. Apr 7, 2012 #1
    1. The problem statement, all variables and given/known data

    A company has a number of employees whose records, for simplicity, contain:

    Employee number Employee name

    Write separate programs, each of which searches for a key whose value is to be input, using the sequential search variations given below. Let each program initialize the data to the sample given.

    a) a straight sequential search algorithm.
    b) a sentinel search algorithm.
    c) a probability search algorithm.


    i didn't know what the underline means ?
    is it required to use the hash function or my solution for part a is right ?

    2. Relevant equations

    emp # emp name

    121267 Ali Mohammed
    045128 Samir Hasan
    379452 Majid Sabri
    160252 Tawfiq Faris
    378845 Basil Ali
    070918 Saddiq mahmod
    166702 Husain Khalid
    572556 Jamil Yasir
    167010 Zaid Amr



    3. The attempt at a solution

    Code (Text):
    #include<iostream>
    using namespace std;

    const int arraySize = 1000;
    struct employee
    {
        char name[arraySize];
        int number;
    };

    int main()
    {


        employee arr[9]={"Ali Mohammed",121267,"Samir Hasan",45128,"Majid Sabri",379452,"Tawfiq Faris",160252,"Basil Ali",378845,"Saddiq mahmod",70918,"Husain Khalid",166702,"Jamil Yasir",572556,"Zaid Amr",167010};
        int size=17;
        int target,flag,n=9,i;
            cin>>target;
            i=0;

        while(i<n)
        {
            if(target==arr[i].number)
            {
                flag=1;
                break;
            }
            i++;
        }
        if(flag)
            cout<<"found"<<"   "<<arr[i].name<<endl;
        else
            cout<<"not found "<<i<<endl;

        return 0;
    }

    thanx alot
     
  2. jcsd
  3. Apr 7, 2012 #2

    rcgldr

    User Avatar
    Homework Helper

    You mean the fact it was underlined, or what the underlined phrase means? The problem statement wants you to write 3 different programs.

    A sequential search does not use a hash so part a looks OK, but you should initialize flag = 0 when declaring it. Also you may want to use typedef for the structure:

    Code (Text):

    typedef struct
    {
        char name[arraySize];
        int number;
    }employee;
     
     
  4. Apr 7, 2012 #3
    thanks for your notes

    thinking ... if i want to use hash function is it possible, that i find an element by using given index to go directly to element arr


    again ... thnx alot


    this is my try for part b) a sentinel search algorithm.


    Code (Text):


    #include "stdafx.h"
    #include<iostream>
    using namespace std;
    const int arraySize = 1000;
    typedef struct
    {
        char name[arraySize];
        int number;
    }employee;

    int main()
    {


        employee arr[10]={"Ali Mohammed",121267,"Samir Hasan",45128,"Majid Sabri",379452,"Tawfiq Faris",160252,"Basil Ali",378845,"Saddiq mahmod",70918,"Husain Khalid",166702,"Jamil Yasir",572556,"Zaid Amr",167010};
       
        int target,flag=0,n=10,i;
            cin>>target;
            arr[9].number=target;
            for(i=0;target!=arr[i].number;i++)
                ;
            if(i<9)
                cout<<"found   "<<arr[i].name<<endl;
            else
                cout<<"not found"<<endl;


        return 0;
    }

     

    c) a probability search algorithm.

    Code (Text):
    #include<iostream>
    using namespace std;
    //int hashfunction(char[],int)
    const int arraySize = 1000;
    typedef struct
    {
        char name[arraySize];
        int number;
    }employee;

    int main()
    {

    const int n=9;
        employee arr[9]={"Ali Mohammed",121267,"Samir Hasan",45128,"Majid Sabri",379452,"Tawfiq Faris",160252,"Basil Ali",378845,"Saddiq mahmod",70918,"Husain Khalid",166702,"Jamil Yasir",572556,"Zaid Amr",167010};
       
        int target,flag=0,i=0;
            cin>>target;
            if(target<=arr[n-1].number)
            {
                while(target>=arr[i].number)
                {
                    if (target==arr[i].number)
                    {
                        cout<<"found   "<<arr[i].name<<endl;
                        flag=1;
                        break;
                    }
                    i++;
                }
            }

    if(flag==0)
    cout<<"not found"<<endl;

    return 0;
    }


     

    i do have wrong output with part c
    where is my mistake ?
    again thank you
     
    Last edited: Apr 7, 2012
  5. Apr 7, 2012 #4

    rcgldr

    User Avatar
    Homework Helper

    For part c, you need code to handle the case where target < arr.number. Are the employee structures supposed to be sorted by number before you use the method in part c?
     
  6. Apr 7, 2012 #5
    Where is your sentinel value in part b?
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Straight sequential search algorithm
Loading...