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
Join my discord server! CODE IS (CebjBXN)​
11Alexandr11 [23.1K]

Answer:

sure

Explanation:

7 0
3 years ago
Read 2 more answers
A(n) _____ can replace many applications with one unified set of programs, making the system easier to use and more effective.
umka2103 [35]
<span>A(n) ERP can replace many applications with one unified set of programs, making the system easier to use and more effective.

ERP stands for Enterprise Resource Planning system. It is a </span><span>set of integrated programs that manages vital business operations for an entire multi-site, global organization. It supports manufacturing & finance aspects of the organization. </span>
4 0
3 years ago
SHORT ANSWERS:
dem82 [27]

Answer:

Hard drive Microsoft have the highest priority actions

7 0
3 years ago
Name the two types of control systems.<br> PLEASE HELP THIS WAS DUE HOURS AGOOOO
vodomira [7]

Answer:

Open loop control systems.

Closed loop control systems.

Explanation:

5 0
3 years ago
Read 2 more answers
To prevent replay attacks, authentication protocols create a number that will be used only once. The number is called a _____.
fgiga [73]

Authentication protocols are usually designed and configured to create a number that will be used only once, so as to prevent replay attacks in a computer network. Thus, this number is called a <u>nonce</u>.

Authentication work based on the principle of strategically matching an incoming request from an end user or electronic device to a set of uniquely defined credentials.

In an access control list (ACL), authentication and authorization is used to ensure an end user is truly who he or she claims to be, as well as confirming that an electronic device is valid through the process of verification.

In Cybersecurity, authentication protocols are usually designed and configured to create a nonce, which can be used only once, so as to prevent replay attacks in a computer network.

In conclusion, a nonce is used prevent replay attacks in a computer network because the number created by an authentication protocol can be used only once.

Read more: brainly.com/question/17307459

7 0
2 years ago
Other questions:
  • Which of the following terms means that the system changes based on
    14·1 answer
  • What commands would return all the rows and columns in the ProductCategory table?
    7·1 answer
  • _____ is a systems development technique that produces a graphical representation of a concept or process that systems developer
    11·1 answer
  • If you are a member of a security penetration testing team, and you identify vulnerabilities and exploits, what should you obtai
    11·1 answer
  • The moon has less mass than the earth, so what happens to objects on the moon?
    11·1 answer
  • who will follow me on tiktok and like all my videos? if you do ill give branlist and give u a shoutout and you can enter my big
    15·1 answer
  • What are some things all boomers say.
    12·2 answers
  • Pls explain the meaning of the word"INTERNET"​
    5·2 answers
  • Write a pseudocode statement that declares the variable total so it can hold integers. Initialize the variable with the value 0
    9·1 answer
  • How do people and computers approach problems differently
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!