# Straight sequential search algorithm

## Homework Statement

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 ?

## Homework Equations

emp # emp name

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

## The Attempt at a Solution

Code:
#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

return 0;
}

thanx alot

rcgldr
Homework Helper
I didn't know what the underline means ?
You mean the fact it was underlined, or what the underlined phrase means? The problem statement wants you to write 3 different programs.

Is it required to use the hash function or my solution for part a is right?
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:
typedef struct
{
char name[arraySize];
int number;
}employee;

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:
typedef struct
{
char name[arraySize];
int number;
}employee;

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:
#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

return 0;
}

c) a probability search algorithm.

Code:
#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)

return 0;
}

i do have wrong output with part c
where is my mistake ?
again thank you

Last edited:
rcgldr
Homework Helper
i do have wrong output with part c
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?

Where is your sentinel value in part b?