+1 vote
2.8k views
in Programming by (98.9k points)
edited
Write A Program in C++ that first initializes an array of given 10- sorted real numbers. The program must verify whether a given element belongs to this array or not, using BINARY SEARCH TECHNIQUE. The element (to be searched) is to be entered at the time of execution. If the number is found, the program should print its position in the array otherwise it should print, “The number is not found”.

1 Answer

0 votes
by (98.9k points)
edited by
 
Best answer

Output::

image

 

Below code is executed on gdb turbo c++ compiler , it takes 10 input of given number then sort by binary search technique

 

 

#include<iostream.h>
int main()
{
 int i, arr[10], num, first, last, middle;
 cout<<"Enter 10 Elements (in ascending order): ";
 for(i=0; i<10; i++)
 cin>>arr[i];
 cout<<"\nEnter Element to be Search: ";
 cin>>num;
 first = 0;
 last = 9;
 middle = (first+last)/2;
 while(first <= last)
 {
 if(arr[middle]<num)
 first = middle+1;
 else if(arr[middle]==num)
 {
 cout<<"\nThe number, "<<num<<" found at Position "<<middle+1;
 break;
 }
 else
 last = middle-1;
 middle = (first+last)/2;
 }
 if(first>last)
 cout<<"\nThe number, "<<num<<" is not found in given Array";
 cout<<endl;
 return 0;
}

 

asked Jan 7 in Programming by anonymous
edited Jan 7 by
logic of the program?

Related questions

Doubtly is an online community for engineering students, offering:

  • Free viva questions PDFs
  • Previous year question papers (PYQs)
  • Academic doubt solutions
  • Expert-guided solutions

Get the pro version for free by logging in!

5.7k questions

5.1k answers

108 comments

558 users

...