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.
In C++ write a function to find a product of two matrices using arrays. The function...
In C++ write a function to find a product of two matrices using arrays. The function should be general and should accept any size matrices.
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 in c++ using only while and for loops . Use of arrays and...
Write a program in c++ using only while and for loops . Use of arrays and functions is not allowed. Given the first value, generate the next ten terms of the sequence like 1, 2, 4, 8, 16, 22, 26, 38, 62, 74, 102, 104, … Explaination with code is required.
Write a program in C# for a Cricket match using Jagged Arrays. The name of the...
Write a program in C# for a Cricket match using Jagged Arrays. The name of the project will be on your name. It has the following modules: Create two functions in class Cricket_Match for random numbers generation. The first function generates 1-6 numbers which is known as balls played by each player. The second function generates a 0-6 number which is known as the score produced against each ball by an individual player. In the main show the individual player...
In C language Write a program that includes a function search() that finds the index of...
In C language Write a program that includes a function search() that finds the index of the first element of an input array that contains the value specified. n is the size of the array. If no element of the array contains the value, then the function should return -1. The program takes an int array, the number of elements in the array, and the value that it searches for. The main function takes input, calls the search()function, and displays...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT