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
Directions and Analysis
anastassius [24]

Answer:

Sorry this is a typing test, too pysical for online help as for the words and time limit to reinforced learning, if content is the issue grab an article or paper and type that.

8 0
3 years ago
PYTHON:Given the dictionary, d, find the largest key in the dictionary and associate the corresponding value with the variable v
lapo4ka [179]

di = {5: 3, 4: 1, 12: 2}

val_of_max = di[max(di)]

print(val_of_max)

I hope this helps!

4 0
3 years ago
Create the code that will find
frez [133]

Answer:

function findLongestWord(str) {

 var longestWord = str.split(' ').sort(function(a, b) { return b.length - a.length; });

 return longestWord[0].length;

}

findLongestWord(InputHere);

Explanation:

Replace InputHere with the input

7 0
3 years ago
Which of the following color palettes for the BackColor and ForeColor properties contains colors that are guaranteed to be displ
Norma-Jean [14]

Answer: 1)System Palette

Explanation: System palette is kind of palette found in the system which contain the color values that is used by the display.This palette is also  drawing of the devices and applications.But the application don't get the direct access to the system palette rather logical palette permits it.

They work on the basis of the properties carried from the Back color and Fore color.Other palettes mention in the option is incorrect because these palettes don't work for the operating system's display .Thus the correct option is option(1).

8 0
3 years ago
Which of the following statements most accurately describes the strength of different types of accreditation?
tia_tia [17]

Answer:

C I believe but I'm not a hundred percent sure

8 0
2 years ago
Other questions:
  • HELP NOW PLZZ/ Question: Complete the sentence with the correct response.
    11·1 answer
  • When you enter search keywords in the search box of file explorer and the onedrive option is selected?
    15·2 answers
  • Which WAN technology is designed to work with a variety of commonly used layer-2 protocols and is sometimes called a layer-2.5 t
    15·1 answer
  • The term “computer literacy” dates back to what decade? <br> 1960s<br> 1970s<br> 1980s<br> 1990s
    5·1 answer
  • Write a function that, given an array A of N integers, of which represents loads caused by successive processes, the function sh
    15·1 answer
  • You work as an IT Technician for uCertify Inc. David, a user, has recently purchased a laptop computer. He is now complaining th
    14·1 answer
  • The parts of a memo are _____.
    9·2 answers
  • It is an island country; it fought against us in World War II; it is known for sushi.
    14·2 answers
  • Please help ASAP! will mark brianliest! 30 points!
    14·1 answer
  • 2
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!