Straight sequential search algorithm

  • Thread starter Thread starter Sumaya
  • Start date Start date
  • Tags Tags
    Algorithm Search
AI Thread Summary
The discussion revolves around implementing three types of search algorithms for employee records in a programming assignment. Participants clarify that a straight sequential search does not require a hash function, and adjustments are suggested for the code, such as initializing the flag variable and using typedef for the employee structure. The second part discusses the sentinel search algorithm, where a sentinel value is added to facilitate the search process. For the probability search algorithm, it is noted that the employee records should be sorted by number, and additional code is needed to handle cases where the target is less than the current employee number. The conversation highlights the importance of correctly implementing each search method and addressing specific coding issues.
Sumaya
Messages
29
Reaction score
0

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
070918 Saddiq mahmod
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
		cout<<"not found "<<i<<endl;

	return 0;
}


thanx alot
 
Physics news on Phys.org
Sumaya said:
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.

Sumaya said:
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;
 
rcgldr said:
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;

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 arragain ... thnx a lot 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
			cout<<"not found"<<endl;	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)
cout<<"not found"<<endl;

return 0;
}
i do have wrong output with part c
where is my mistake ?
again thank you
 
Last edited:
Sumaya said:
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?
 
Back
Top