Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using the CArray class with the SeqSearch method and the BinSearch method, creat

ID: 3798939 • Letter: U

Question

Using the CArray class with the SeqSearch method and the BinSearch method, create an array of 1,000 random integers. Add a new private Integer data member named compCount that is initialized to 0. In each of the search algorithms, add a line of code right after the critical comparison is made that increments compCount by 1. Run both methods, searching for the same number, say 734, with each method. Compare the values of compCount after running both methods. What is the value of compCount for each method? Which method makes the fewest comparisons?

***Please use C#

Explanation / Answer

RandomNumbers.CPP :

#include<iostream.h>

#include<conio.h>

#include<stdlib.h>

class CArray

{

private:

int compCount;

public:

int SeqSearch(int a[],int key){

compCount = 0;

for(int i=0;i<1000;i++){

compCount++;

if(a[i] == key)

return compCount;

}

return 0;

}

int BinSearch(int a[],int key){

compCount = 0;

int i,temp;

for(i=0;i<1000;i++){

for(int j=i+1;j<1000;j++){

if(a[i]>a[j]){

temp = a[i];

a[i] = a[j];

a[j] = temp;

}

}

}

int mid,low = 0,high = 1000-1;

while(low<=high){

mid = (low + high)/2;

compCount++;

if(a[mid] == key){

return compCount;

}

else if(a[mid] > key){

high = mid - 1;

}

else{

low = mid + 1;

}

}

return compCount;

}

};

void main()

{

int arr[1000],i;

clrscr();

for(i=0;i<1000;i++)

arr[i] = rand()%1000 + 1;

int seqcount = 0,binarycount = 0,key;

cout<<"Enter element to be found:";

cin>>key;

CArray ca;

seqcount = ca.SeqSearch(arr,key);

cout<<"Sequential Search Comparisons Count:"<<seqcount<<endl;

binarycount = ca.BinSearch(arr,key);

cout<<"Binary Search Comparisons Count:"<<binarycount<<endl;

char c = (seqcount<binarycount)?'s':'b';

if(c=='s')

cout<<"Sequential Search makes the fewest comparisons";

else

cout<<"Binary Search makes the fewest comparisons";

getch();

}

Sample Input & Output :

Enter element to be found:734

Sequential Search Comparisons Count:107

Binary Search Comparisons Count:9

Binary Search makes the fewest comparisons

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote