write a binary search algorithm to look for the presence of a search key in a single dimensional array of 10 elements. prompt the user to input 10 elements and the search key. display meaningful messages after the search operation.

#include<iostream.h>
#include<conio.h>
void main()
{
int p[10];
int search_key;
cout<<"\n enter 10 elements:"<<"\t";
for(int i=0; i<10; i++)
{
cin>>p[i];
}
cout<<"\n elements sorted:";
for(i=0; i<10; i++)
{
cout<<p[i]<<"\t";
}
cout<<"\n enter the search_key:";
cin>>search_key;
int middle, first=0, last=9;
while(first<=last)
{
middle=(first+last)/2;
middle+1;
cin>>search_key;
for(int index=0; index<10; index++)
{
if (p[index] == search_key)
{
cout<<"\n"<<search_key<<"found at location"<<index;break;
}
}
if(index>9)
cout<<"\n"<<search_key <<"not found";
getch();
}
}

i want to know if my program code is correct for this question or there are mistakes?

Do you seriously work with your code unindented like that? So far all of your posts have zero indentation before some kind mod fixes it.

pardon? can u tell in simple words what is the problem with my codes? i did this work myself but couldn't get some part so wanted to ask if u can help.

pardon?

You don't know what indentation is?

can u tell in simple words what is the problem with my codes?

No. If you're not willing to put any effort into formatting your code so that it's easy to read, I'm not willing to put any effort into reading it.

my code is wrong?

formatting is wrong some where? can u highlight the areas in which u want me to work

He's telling you that your standards need fixing or else NO ONE will read your code.. Read some of the comments I put into your code to figure out how to format it..

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

void main()   //needs to be int main..
{
|<-->| = A tab/or 4-space.

    int p[10];   //notice that there is a tab before int p? that's indentation.
    int search_key;
    cout<<"\n enter 10 elements:"<<"\t";

    for(int i=0; i<10; i++) //put some spaces between your operators and numbers... i = 0 not i=0.
    {
//  |<-->| = A tab/4-spaces..
          cin>>p[i];     //notice that cin is indented by one tab from the for-loop brackets.
    }
                                       //Separate different sections using a blank line..
    cout<<"\n elements sorted:";
    for(i = 0; i < 10; i++)
    {
        cout<<p[i]<<"\t";
    }

    cout<<"\n enter the search_key:";
    cin>>search_key;

    int middle, first=0, last=9;

    while(first<=last)
    {
        middle=(first+last)/2;
        middle+1;
        cin>>search_key;
        for(int index=0; index<10; index++)
        {
            if (p[index] == search_key)
            {
                cout<<"\n"<<search_key<<"found at location"<<index;
                break;
            }
        }
        if(index>9)
            cout<<"\n"<<search_key <<"not found";
        getch();
    }
}

can't u do it the right way and show the code Please.

commented: If you act like a lazy leech then you'll be treated like a lazy leech. -3

Don't be so lazy.. If you did it yourself, it'd look like the following Formatted code:

#include <iostream>
#include <conio>

int main()
{
    int p[10] = {0};
    int search_key;
    int middle = 0, first = 0, last = 9;
    cout<<"\n enter 10 elements:"<<"\t";

    for(int i = 0; i < 10; i++)
    {
        cin>> p[i];
        cin.ignore();
    }

    cout<<"\n elements sorted:";
    for(i = 0; i < 10; i++)
    {
        cout<<p[i]<<"\t";
    }

    cout<<"\n enter the search_key:";
    cin>>search_key;
    cin.ignore();

    while(first <= last)
    {
        middle = (first + last) / 2;
        middle + 1;
        cin>> search_key;
        cin.ignore();

        for(int index = 0; index < 10; index++)
        {
            if (p[index] == search_key)
            {
                cout<<"\n"<< search_key <<"found at location"<<index;
                break;
            }
        }

        if(index > 9)
            cout<<"\n"<<search_key <<"not found";

        getch();
    }
}

thank you so much i dont have words to thank you :'(

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.