1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Afina-wow [57]
3 years ago
14

Write a program that has an array of at least 20 integers. It should call a function that uses the linear search algorithm to lo

cate one of the values. The function should keep a count of the number of comparisons it makes until it finds the value. The program then should call a function that uses the binary search algorithm to locate the same value. It should also keep count of the number of comparisons it makes. Display these values on the screen. In order for us to have the same answers let's initialize an array with these 20 integer values: 101, 142, 147, 189, 199, 207, 222, 234, 289, 296, 310, 319, 388, 394, 417, 429, 447, 521, 536, 600 Let's search for value 521. These are the parameters used for a successful run of the program.
Computers and Technology
1 answer:
zheka24 [161]3 years ago
3 0

Answer:

Following are the program in the C++ programming Language.

#include<iostream> // header file

using namespace std; // using namespace  

int binary_Searching(int arr[], int n, int key1) // function definition of binary search

{

int l1 = 0, h1 = n - 1, mid; // variable declaration

int r = 0; // holding result

while (l1 <= h1)  

{

r++; // increment of r

mid =(l1 + h1) / 2; // calculating the mid value

if(arr[mid] == key1) // checking the condition if the searching value is mid value

{

return r; // return r

}

else if (arr[mid]<key1) // checking the condition

{

l1 = mid + 1;  

}  

else

{

h1 = mid - 1;

}

}

return r; // reurn r

}

 

int linear_Searching(int arr[], int n, int key1) // function definition

{

int k,index = -1; // variable declaration

for(k= 0;k<n;k++) // iterating over the loop

{

if(arr[k] == key1) // checking the condition

{

if(index == -1)

{

index = k;

break; // break the program

}

}

}

return index+1; // return the index

}

int main() // main method

{

int arr[20] = {101, 142, 147, 189, 199, 207, 222, 234, 289, 296, 310, 319, 388, 394, 417, 429, 447, 521, 536, 600 };// declaring an array  

int result=binary_Searching(arr,20,521); // calling the function binary serach

cout<<"Total number of Binary search comparisons: "<<result<<endl;

int reult1=linear_Searching(arr,20,521); // calling the function linear serach

cout<<"Total number of Linear search comparisons: "<<reult1<<endl;

return 0;

}

<u>Output:</u>

Total number of Binary search comparisons: 3

Total number of Linear search comparisons:  18

Explanation:

Here, we define the integer type function " binary_Searching()" and pass three integer type arguments in its parameter i.e., "arr[]" which is array type, "n", and "key1" and inside the function.

  • we set three integer type variable and assign values in it "l1" to 0, "h1" to n-1, and "r" to 0.
  • we set the while loop and pass the condition when the variable l1 is less than equal to the variable h1 then increment in the variable r by 1 after that, the addition of l1 or h1 is store in the integer variable "mid" for calculating the mid value.
  • inside the while loop, we set the if statement and pass condition the variable arr[mid] is equal to the key1 then, return 1 otherwise is the arr[mid ] is less than key1 then, increment at mid and store in l1 otherwise decrement at mid and store in the variable h1.

Here, we define the integer type function "linear_Searching()" and pass three integer type arguments in its parameter i.e., "arr[]" which is array type, "n", and "key1" and inside the function.

  • inside it, we set the for loop and pass condition if k is less than n then, we set the if statement and check if arr[k] is equal to the key1 then, again we set if conditional statement and check if index is equal to the -1 then, we initialize the value of k in index then, we break the loop .
  • Then, we return index by increment of 1.

Finally, we set the main method "main()" inside it, we set an integer type array variable and pass elements init then, we call bothe the variable and passing the values in its parameter and store them in other integer variable and print the following results.

You might be interested in
Computer World sells laptops separately from accessory products like docking stations, anti-virus software, and external hard dr
uysha [10]

Answer:

Optional product pricing

Explanation:

<em>Optional product pricing</em> occurs when a product is sold for a much lower price but complementary products or accessories are sold separately to generate profit.

A typical example is in the printer category, printer cartridges are sold separately from the printer when the one in the newly purchased printer runs out. The customer is forced to purchase new cartridges that the company benefits from as profits.

Optional product pricing is a strategy to provide less expensive technology while exploiting the frequent use of accessories to make a substantial profit.

6 0
3 years ago
What does obsolete mean?
Alex
No longer produced or used; out of date.
8 0
3 years ago
Read 2 more answers
What is the MINIMUM number of paths that would need to be broken to prevent Computing Device A from connecting with Computing De
Sveta_85 [38]

Answer:

The answer is "Option c".

Explanation:

In the given question the device A is connected by 3 wires, contributing all of them, which also includes several connector paths. When all the wires of A are broken down, and if all of this leaves no routes that can be used. Even so, if it is done to E, it's also linked to four different routes. Its solution would've been C because its value will be the MINIMUM.

4 0
3 years ago
Read 2 more answers
Which of the following is the path to the zoom button? select one:
kupik [55]
The correct answer is a. (view tab | zoom group).
8 0
3 years ago
Read 2 more answers
Lime is using social media to ask consumers about the qualities they desire in an electric scooter. The firm hopes to develop a
tresset_1 [31]

Answer:

The correct answer to the following question will be "Product".

Explanation:

  • Product marketing is often a mechanism by which a brand is marketed and delivered to a client. It's also known as the intermediate feature between product innovation and brand recognition or awareness.
  • It's something to which comparison is made when selling a product on the market to the consumer.

Therefore, the Product is the right answer for the above question.

8 0
3 years ago
Other questions:
  • When creating a professional presentation, how many typefaces are recommended at the most?
    5·1 answer
  • By default, which of these traffic source dimensions does google analytics capture for each user that visits your website?
    8·1 answer
  • What contains the computer brain the central processing unit
    8·2 answers
  • What is one problem you should keep in mind when researching information on the Internet?
    6·2 answers
  • Why would a designer choose to use an isometric sketch rather than a perspective sketch to share their idea with a design team m
    14·1 answer
  • Write a program that prompts the user to enter the month and year and displays the number of days in the month. For example, if
    13·1 answer
  • Select the correct answer.
    15·1 answer
  • In linear​ programming, what are restrictions that limit the degree to which a manager can pursue an​ objective?
    15·1 answer
  • Which CIDR network has the smallest possible number of hosts? a. 123.0.0.0/8 b. 123.45.0.0/16 c. 123.45.67.0/24 d. All have the
    10·1 answer
  • Suppose that you have declared a numeric array named numbers, and two of its elements are numbers[1] and numbers[4]. You know th
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!