Question

In: Computer Science

Write a program of Binary Search in C++ by using function and arrays with the explanation.

Write a program of Binary Search in C++ by using function and arrays with the explanation.

Solutions

Expert Solution

#include<iostream>
using namespace std;
int main()
{
int BSearch(int [],int,int); //declaring a BSearch()_to search position of given element
int N,i,A[50],E,X;
cout<<"Enter number of elements:"; //input size of array
cin>>N;
cout<<"\nEnter elements\n"; //input elements of array
for(i=0;i<N;++i)
{
cin>>A[i];
}
cout<<"\nElement to search:"; //input element to earch
cin>>E;
  
X=BSearch(A,N,E); // initialize the value of function to variable X
if(X!=-1) //IF VALUE OF x IS NOT -1 THEN PRINT THIS STATEMENT
cout<<"\n Position of element is "<<X+1;
else
cout<<"\nNo element found!";

return 0;
}

int BSearch(int A[],int N,int E) // Function to search element ina array
{
int Fst,Lst,Mid; // Taking variable Fst(First position ),Lst(Last position) and Mid(Middle of array)
Fst=0; Lst=N-1;
  
while(Fst<=Lst) // execute the loop when value of First is lessthen last
{
Mid=(Fst+Lst)/2; // Finding the middle of array
if(E==A[Mid]) //if element is equal to value of element at position Mid
return(Mid); // return the value to X
else
if(E>A[Mid]) // if element is greater element at positon mid
Fst=Mid+1; // then the value of First will be Middle +1 and last will be same and search again between this position
else
Lst=Mid-1;
}
return -1;
}


Related Solutions

Write a program in C++ to implement Binary Search Algorithm. Assume the data given in an...
Write a program in C++ to implement Binary Search Algorithm. Assume the data given in an array. Use number of data N at least more than 10. The function Binary Search should return the index of the search key V.
Make a Binary search program for C# and write algorithm and explain it in easy words...
Make a Binary search program for C# and write algorithm and explain it in easy words also show output and input
Write a program in C language that uses a binary search algorithm to guess a number...
Write a program in C language that uses a binary search algorithm to guess a number from 1 to 100. The computer will keep guessing until they get the users number correct.
Correct this Binary Search (C++) // This program demostrates linear search algorithm #include <iostream> using namespace...
Correct this Binary Search (C++) // This program demostrates linear search algorithm #include <iostream> using namespace std; // Binary search algorith // f is the first , l is the last , t is the target int binarySearch(int stgrade[], int f, int l, int t) { while (f <= l) { int m = f + (l - l) / 2; // Check if x is present at mid if (stgrade[m] == t) return m; // If x greater, ignore...
Write a program to show the difference between linear search and binary search. Show the input...
Write a program to show the difference between linear search and binary search. Show the input test data for your program and the output produced by your program which clearly show that binary search is faster than linear search
Assume you need to write a Java program that uses a binary search algorithm to search...
Assume you need to write a Java program that uses a binary search algorithm to search a sorted array for a given value. 1. Write a Java pseudocode that uses recursion to accomplish the task. Here is a hint. When you are searching for a particular value in an array, there are two possible outcomes. 1) The value is found and the array index of that value is returned. 2) The value is not found and we return -1. (5...
Binary Search. Write a MIPS assembly program to perform a binary search on A[10], which is an array of 10 positive integers.
Binary Search. Write a MIPS assembly program to perform a binary search on A[10], which is an array of 10 positive integers. Your program should have a main routine that does the following:(a) Prompt the user to enter all the 10 integers in the array.(b) Prompt the user to enter the number to be searched.(c) Reads the integer values and makes sure it is a positive integer.(d) Prints the index of the integer. If the input is not available in...
A local instructor wants you to write a c++ program using arrays to calculate the average...
A local instructor wants you to write a c++ program using arrays to calculate the average score made on exams by her students. For simplicity, she always has only 12 students in each course she teaches. She teaches multiple subjects so she would like to enter the name of the exam. She wants the program to also determine the highest and lowest scores and the number of students who passed and failed the exam. A score of 60 or above...
Write a C++ program using dynamic arrays that allows the user to enter the last names...
Write a C++ program using dynamic arrays that allows the user to enter the last names of the candidates in a local election and the number of votes received by each candidate. The program must ask the user for the number of candidates and then create the appropriate arrays to hold the data. The program should then output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should...
C++ DO not use arrays to write this program. Write a program that repeatedly generates three...
C++ DO not use arrays to write this program. Write a program that repeatedly generates three random integers in the range [1, 100] and continues as follows: If the right-most digit of all the three integers is equal, the program displays them in ascending order on the screen and continues. If the generated integers have different right-most digits, they are not displayed and the program continues. The program terminates once the right-most digits of all the three random numbers are...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT