Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Computer Engineering Expert

I have code for a sequential search and a binary search. I have to "add a counter for every key comparison in each search function and print out the number of key comparisons for each search."

Here is my code:

#include
using namespace std;

int SequentialSearch(const int _list[], int _length, const int & _item);
int BinarySearch(const int _list[], int _length, const int & _item);

int main()
{
int temp;
int numbers[] = {5,10,15,20,25,30,35,40,45,50};
cout << SequentialSearch(numbers, 10, 30) << endl;
cout << BinarySearch(numbers, 10, 30) << endl;

cin >> temp;
return 0;
}
int SequentialSearch(const int _list[], int _length, const int & _item)
{
for (int x = 0; x< _length; x++)
{
if (_list[x] == _item)
return x;
}
return -1;
}
int BinarySearch(const int _list[], int _length, const int & _item)
{
int first = 0;
int last = _length -1;
int mid = 0;
bool found = false;

while (first <= last && !found)
{
mid = (first + last) / 2;
if (_list[mid] == _item)
found = true;
else if (_list[mid] > _item)
last = mid - 1;
else
first = mid + 1;
}
if (found)
return mid;
else
return -1;
}

Computer Engineering, Engineering

  • Category:- Computer Engineering
  • Reference No.:- M91615172
  • Price:- $20

Priced at Now at $20, Verified Solution

Have any Question?


Related Questions in Computer Engineering

Calculate the probability of selecting a random sample of

Calculate the probability of selecting a random sample of 225 observations with a sample proportion of 0.54 or greater, if the sample population has a population proportion of 0.50.

The diet doctor claims that the average north american is

The diet doctor claims that the average North American is more than 20 pounds overweight. To test his claim, a random sample 30 Of north Americans was weighted, the difference between their actual weight and their ideal ...

Identify economic decision that is driven by a behavioral

Identify economic decision that is driven by a behavioral bias rather than by pure rational behavior. why are they differ today?

Suppose a life insurance company sells a 230000 one-year

Suppose a life insurance company sells a $230,000 ?one-year term life insurance policy to a 20?-year-old female for ?$330. The probability that the female survives the year is 0.999642. Compute and interpret the expected ...

When using todays digital cameras file sizes are often

When using today's digital cameras file sizes are often saved in a format that is well over 1 Megabyte. This may be great for high definition photo reproductions but is a disaster for uploading to the web. Images on your ...

Question suppose now that after each turn a single six-face

Question : Suppose now that after each turn, a single six-face die is tossed. If it comes out to one, a single chip is removed from the fourth pile; otherwise, the board is left as is. Describe an efficient algorithm tha ...

Hoping to lore more shoppers downtown he said he built a

Hoping to lore more shoppers downtown. He said he built a new public parking garage in central business district. The city plans to pay for the structure through parking fees. For a random sample of 44 weekdays, daily fe ...

Please help with anbspfunctionnbspcodesymbol to convert

Please help with a function  codeSymbol , to convert each mark to a symbol (A, B, C, D, E, F) and a code (7,6,5,4,3,2,1) according to the table below. And call it in the main function. Use the table below to determine th ...

Solve the following one 1 self-check problems on paper and

Solve the following one (1) Self-Check problems on paper and bring your sheet of paper to your section on Thursday: Write a function called grades that takes a list of tuples as a parameter. Each tuple in this list conta ...

You have an opportunity to buy a bond with a face value of

You have an opportunity to buy a bond with a face value of $10,000 and coupon rate of 14%, payable semi-annually. NOTE: Interest per 6-month period is 7% of Face Value (i.e. $10,000x0.07 = $700 per 6-month period).  (i) ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As