I am a beginner in c++ and now i reached in array processing binary search. my program is being compiled but it is not showing any result. Please help me out.

#include<iostream.h>
  #include<conio.h>

  const
 int n=10;

  void main(){

  	int a[n] , l , initial, final, mid,data;

   	for(l=0; l<n; l++)
      	      cin >> a[l] ;

      cout << "Enter the data to be searched.";
        	cin >> data;

      initial=0;
      final=n-1;
      mid=(initial + final) /2 ;

      while((initial<=final)  && (a[mid] !=  data ))
       {
       if(data < a[mid])
      final = mid - 1;
      else
      initial = mid + 1;
      }

        if(data == a[mid])
        cout << "Data present in the list.";
        else
        cout << "Data is not in the list.";

   getch();
  }

if i write the no. from 1-10 and when i write 5 for the data to be searched it shows the correct result but except 5 it stucks there with no result.

You are not updating the mid variable..

please show me the way or tell where can i add som missing codes

i did it. thanks vidit :)

if(data < a[mid])
      final = mid - 1;
      else
      initial = mid + 1;
      mid=(initial + final) /2 ;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.