Binary Search in array C++

Using Visual Studio 2015

===============================================================

#include<iostream>
using namespace std;
#include<conio.h>
void main ()
{
       int arr [10], as = 10, start = 0, end = 9, mid = (start + end) / 2, input;
       bool check = false;
       for (int i = 0; i < as; i++)
       {
       cout<<"Enter the number "<<(i+1) <<"=";
       cin>>arr[i];
       }
       cout << "Enter the value to search= ";
       cin >>input;
       while (start <= end)
       {
              mid = (start + end) / 2;
              if (input == arr[mid])
              {
                     check = true;
                     break;
              }
              else if (input>arr[mid])
              {
                     start = mid + 1;
              }
              else if (input<arr[mid])
              {
                     end = mid - 1;
              }

       }
       if (check == false)
       {
              cout << "Element Not Found";
       }
       else
              cout << "Element Found";
       _getche ();
}

==============================================================

Comments

Popular posts from this blog

Suppose you are given a square matrix of size n x n, write a program to determine if this is an identity matrix. 2D array

Program to create a multiplication table of a given number using while loop.

Program to find equal values using if statement in C++