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
Type the correct answer in the box. Spell all words correctly.
ololo11 [35]

foud6gdjyrxyutwrhgcghkiyiyofjxgxhdttenhdjgxjxjgztujxjxufudtduyyd6rdgtsufoud6gdjyrxyutwrhgcghkiyiyofjxgxhdttenhdjgxjxjgztujxjxufudtduyyd6rdgtsufoud6gdjyrxyutwrhgcghkiyiyofjxgxhdttenhdjgxjxjgztujxjxufudtduyyd6rdgtsufoud6gdjyrxyutwrhgcghkiyiyofjxgxhdttenhdjgxjxjgztujxjxufudtduyyd6rdgtsufoud6gdjyrxyutwrhgcghkiyiyofjxgxhdttenhdjgxjxjgztujxjxufudtduyyd6rdgtsufoud6gdjyrxyutwrhgcghkiyiyofjxgxhdttenhdjgxjxjgztujxjxufudtduyyd6rdgtsufoud6gdjyrxyutwrhgcghkiyiyofjxgxhdttenhdjgxjxjgztujxjxufudtduyyd6rdgtsufoud6gdjyrxyutwrhgcghkiyiyofjxgxhdttenhdjgxjxjgztujxjxufudtduyyd6rdgtsufoud6gdjyrxyutwrhgcghkiyiyofjxgxhdttenhdjgxjxjgztujxjxufudtduyyd6rdgtsufoud6gdjyrxyutwrhgcghkiyiyofjxgxhdttenhdjgxjxjgztujxjxufudtduyyd6rdgtsufoud6gdjyrxyutwrhgcghkiyiyofjxgxhdttenhdjgxjxjgztujxjxufudtduyyd6rdgtsufoud6gdjyrxyutwrhgcghkiyiyofjxgxhdttenhdjgxjxjgztujxjxufudtduyyd6rdgtsufoud6gdjyrxyutwrhgcghkiyiyofjxgxhdttenhdjgxjxjgztujxjxufudtduyyd6rdgtsufoud6gdjyrxyutwrhgcghkiyiyofjxgxhdttenhdjgxjxjgztujxjxufudtduyyd6rdgtsufoud6gdjyrxyutwrhgcghkiyiyofjxgxhdttenhdjgxjxjgztujxjxufudtduyyd6rdgtsufoud6gdjyrxyutwrhgcghkiyiyofjxgxhdttenhdjgxjxjgztujxjxufudtduyyd6rdgtsufoud6gdjyrxyutwrhgcghkiyiyofjxgxhdttenhdjgxjxjgztujxjxufudtduyyd6rdgtsufoud6gdjyrxyutwrhgcghkiyiyofjxgxhdttenhdjgxjxjgztujxjxufudtduyyd6rdgtsufoud6gdjyrxyutwrhgcghkiyiyofjxgxhdttenhdjgxjxjgztujxjxufudtduyyd6rdgtsufoud6gdjyrxyutwrhgcghkiyiyofjxgxhdttenhdjgxjxjgztujxjxufudtduyyd6rdgtsu

5 0
2 years ago
When you ____ an exception, you send a message that an error has occurred to another part of the program.
jekas [21]
The correct answer is most definitely B
8 0
2 years ago
Read 2 more answers
The navigation items for a Lightning app are available on mobile: A. In a special mobile configuration in the navigation menu an
mr_godi [17]

Navigation items are used for moving from one part of the part to a destination using actions and also for passing information. The right option is  <em>C. In the navigation menu and the first four items of the </em><em>navigation bar</em><em>, when users are in the app </em>

<em />

There are different methods/ways to implement the Navigation component in a mobile application, we have the following type of Navigation Implementation

  1. Bottom Navigation
  2. Drawer Navigation(Slides from Left to right)
  3. Top Navigation

Learn more about Navigation here:

brainly.com/question/8908486

5 0
2 years ago
A Web site that allows users to enter text, such as a comment or a name, and then stores it and later display it to other users,
balandron [24]

Answer:

Option(c) is the correct answer.

Explanation:

Cross-site scripting is the type of security breach that are usually found in the  software applications.The main objective of cross site scripting it is used by the hackers to exploit the data security.

  • The cross site scripting is the collection of web pages  that enables people to insert the text like comment,  name stores it afterwards it save the data and then  it appears to the other users.
  • Others options are incorrect because they are not related to given scenario.
5 0
2 years ago
Read 2 more answers
One vital component of your professional behavior with regard to computing systems today is the creation of​ _________.
zlopas [31]
<span>One vital component of your professional behavior with regard to computing systems today is the creation of​ strong passwords.
It is very important to have a good password on your computer, especially if you are dealing with sensitive data and information. It would be for the best to create such a password which won't be hacked into easily, in case somebody wants to steal your data.
</span>
3 0
2 years ago
Other questions:
  • A drop-down menu must be contained by
    5·1 answer
  • The ________________ command tests connectivity by sending an echo request to a remote computer.
    14·1 answer
  • How do you double space on microsoft word?
    5·1 answer
  • In C++ write a program that prints out PI as a type double and a type float EXACTLY as shown below. Your program should have ONE
    13·1 answer
  • Assume that strikeCounter has already been declared to be a "pointer to int". Assume further that strikeCounter has been initial
    7·1 answer
  • A computer operating system software manufacturer invests its profits in creating newer versions of its operating system softwar
    7·1 answer
  • List the components of a typical operating system and summarize the role of each in a single phrase.
    6·1 answer
  • 4.5 code practice computer science
    5·1 answer
  • Consider the following class definition.
    7·1 answer
  • Select the three concepts of capital outlay.
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!