Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Problem in binary search using C++

Problem in binary search using C++

Scheduled Pinned Locked Moved C / C++ / MFC
c++help
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member_14482930
    wrote on last edited by
    #1

    I have copied a program from CrazyGeeks website and modified some of its statements. Anyone please tell me whats wrong in below code:

    #include
    using namespace std;

    int main()
    {
    int count, j, elements[50], search_num, first, last, mid;
    cout<<"Enter the total number of elements :";
    cin>>count;

    cout<<"\nEnter ">>count>>" numbers:\n";
    for (j=0; j>elements[j];
    }

    cout<<"\nWhich number that you want to search: ";
    cin>>search_num;
    first = 0;

    last = count-1;
    mid = (first+last)/2;

    // binary search
    while (first <= last)
    {
    if(elements[mid] > search_num){
    first = mid + 1;
    }

    else if(elements[mid] == search_num){
    cout< last){
    cout<

    Code link

    CPalliniC V 2 Replies Last reply
    0
    • M Member_14482930

      I have copied a program from CrazyGeeks website and modified some of its statements. Anyone please tell me whats wrong in below code:

      #include
      using namespace std;

      int main()
      {
      int count, j, elements[50], search_num, first, last, mid;
      cout<<"Enter the total number of elements :";
      cin>>count;

      cout<<"\nEnter ">>count>>" numbers:\n";
      for (j=0; j>elements[j];
      }

      cout<<"\nWhich number that you want to search: ";
      cin>>search_num;
      first = 0;

      last = count-1;
      mid = (first+last)/2;

      // binary search
      while (first <= last)
      {
      if(elements[mid] > search_num){
      first = mid + 1;
      }

      else if(elements[mid] == search_num){
      cout< last){
      cout<

      Code link

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      Quote:

      cout<<"\nEnter ">>count>>" numbers:\n";

      Should be

      out << "\nEnter " << count << " numbers:\n";

      You should ask the user for an ordered sequence, in order to make the binary search work. Your code is very C-like, you didn't take advantange of the wonderful features of modern C++. :-)

      In testa che avete, signor di Ceprano?

      M 1 Reply Last reply
      0
      • CPalliniC CPallini

        Quote:

        cout<<"\nEnter ">>count>>" numbers:\n";

        Should be

        out << "\nEnter " << count << " numbers:\n";

        You should ask the user for an ordered sequence, in order to make the binary search work. Your code is very C-like, you didn't take advantange of the wonderful features of modern C++. :-)

        M Offline
        M Offline
        Member_14482930
        wrote on last edited by
        #3

        Thank you very much

        CPalliniC 1 Reply Last reply
        0
        • M Member_14482930

          Thank you very much

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          You are welcome.

          In testa che avete, signor di Ceprano?

          1 Reply Last reply
          0
          • M Member_14482930

            I have copied a program from CrazyGeeks website and modified some of its statements. Anyone please tell me whats wrong in below code:

            #include
            using namespace std;

            int main()
            {
            int count, j, elements[50], search_num, first, last, mid;
            cout<<"Enter the total number of elements :";
            cin>>count;

            cout<<"\nEnter ">>count>>" numbers:\n";
            for (j=0; j>elements[j];
            }

            cout<<"\nWhich number that you want to search: ";
            cin>>search_num;
            first = 0;

            last = count-1;
            mid = (first+last)/2;

            // binary search
            while (first <= last)
            {
            if(elements[mid] > search_num){
            first = mid + 1;
            }

            else if(elements[mid] == search_num){
            cout< last){
            cout<

            Code link

            V Offline
            V Offline
            Victor Nijegorodov
            wrote on last edited by
            #5

            You should format your code snippet with the proper indentations.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups