Straight sequential search algorithm

  • Thread starter Thread starter Sumaya
  • Start date Start date
  • Tags Tags
    Algorithm Search
Click For Summary

Discussion Overview

The discussion revolves around implementing different variations of a sequential search algorithm for a homework assignment involving employee records. Participants are tasked with writing programs for a straight sequential search, a sentinel search, and a probability search algorithm.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant questions the meaning of an underlined phrase in the homework statement and whether a hash function is necessary for part a, suggesting that a sequential search does not require it.
  • Another participant points out that the flag variable should be initialized to 0 in the provided code and suggests using typedef for the employee structure.
  • A participant expresses uncertainty about the possibility of using a hash function to directly access elements by index.
  • In part b, a participant provides a sentinel search implementation but is questioned about the absence of a sentinel value in their code.
  • For part c, a participant reports incorrect output and is advised to handle cases where the target is less than the current employee number and to consider whether the employee structures need to be sorted.

Areas of Agreement / Disagreement

Participants generally agree on the structure of the programs but express differing opinions on specific implementations and requirements, particularly regarding the use of sentinel values and handling edge cases in the algorithms. The discussion remains unresolved regarding the correct implementation details for part c.

Contextual Notes

There are unresolved questions about the necessity of sorting the employee records for the probability search algorithm and the proper use of sentinel values in the sentinel search implementation. Additionally, there are assumptions about the initialization of variables that may affect the correctness of the code.

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 a lot
 
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?