当我放置偶数个元素时,为什么我的程序不起作用

时间:2016-03-08 06:11:41

标签: c++ c arrays binary-search

我正在尝试制作一个二进制搜索程序,我的编码在下面,但是当我给出偶数个元素时,它不会给出任何输出,当我给出奇数个元素时,程序运行得很好!!程序首先比较中间值,如果为假,则比较小于或大于中间值

int main()
{
    int n,a[50] ;
    int i, j, temp,counter,searchv,f,l,mid;
    cout<<"Enter no. of elements: ";
    cin>>n;
    f=0;
    l=n-1;
    mid=(f+l)/2;
    cout<<"l= "<<l<<" mid= "<<mid<<"\n";
    cout<<"Enter "<<n <<" values \n";
    for(counter=0;counter<n;counter++)
    {
        cin>>a[counter];
    }

    for(j=0; j<n; j++)
    {
        for (int i=(n-1); i>j ;i--)
        {
            if (a[i]<a[i-1])
            {
                int temp=a[i-1];
                a[i-1]=a[i];
                a[i]=temp;
            }
        }
    }
    cout<<"SORTED ARRAY!!\n";
    for(counter=f;counter<n;counter++)
    {
        cout<<"Value at Element "<<counter <<" is "<<a[counter];

        cout<<endl;
    }
    cout<<"Enter number to search: ";
    cin>>searchv;

    if(a[mid]==searchv)
    {
        cout<<"searched value "<<searchv<<" founded at position "<<mid;
    }
    else if(searchv>a[mid])
    {
        for(counter=l;counter>mid;counter--)
        {
            if(a[counter]==searchv)
                cout<<"searched value "<<searchv<<" founded at position "<<counter;
            break;
        }
    }
    else if(searchv<a[mid])
    {
        for(counter=0;counter<mid;counter++)
        {
            if(a[counter]==searchv)
                cout<<"searched value "<<searchv<<" founded at position "<<counter;
            break;
        }
    }
    else
    {
        cout<<"Value not found\n";
    }
    getch();
    return 0;
}

2 个答案:

答案 0 :(得分:0)

您的错误与数组是否具有偶数或奇数元素无关。在你的两个线性搜索循环中,例如:

    for (counter = l; counter > mid; counter--) {
        if (a[counter] == searchv)
            cout << "searched value " << searchv <<
                " founded at position " << counter << '\n';
        break;
    }

break无条件退出循环。这意味着您只测试范围的第一个元素。你想在你找到匹配项时才打破,当然,你应该使用大括号:

    for (counter = l; counter > mid; counter--) {
        if (a[counter] == searchv) {
            cout << "searched value " << searchv <<
                " founded at position " << counter << '\n';
            break;
        }
    }

如果您养成一致地缩进和使用大括号的习惯,这些错误更容易被发现,可能除了最基本的if没有else之外。

通过此修复程序,您的程序将仅报告匹配项,但不报告任何错误,因为lase else在逻辑上永远不会发生,因为前三个if / else涵盖所有可能的案例。 (对于整数,a == ba < ba > b涵盖所有情况。)

您可以通过保留一个额外的变量来修复此问题,该变量告诉您​​是否找到了匹配项,或者将搜索重构为单独的函数,无论如何这都是可行的。

答案 1 :(得分:0)

根据上面的答案,您可以添加此

而不是

if(a[mid]==searchv)
    {
    cout<<"searched value "<<searchv<<" founded at position "<<mid;
    }
    else if(searchv>a[mid])
    {
    for(counter=l;counter>mid;counter--)
    {
        if(a[counter]==searchv)
    cout<<"searched value "<<searchv<<" founded at position "<<counter;
    break;
    }
    }
    else if(searchv<a[mid])
    {
    for(counter=0;counter<mid;counter++)
    {
    if(a[counter]==searchv)
    cout<<"searched value "<<searchv<<" founded at position "<<counter;
    break;
    }
    }

else
{
     cout<<"Value not found\n";
}

你可以试试这个:

    bool isFound = false;

    for(counter=0;counter<l;counter++)
    {
        if(a[counter]==searchv)
        {
            cout<<"searched value "<<searchv<<" founded at position "<< counter << endl;
            isFound = true;

        }
    }

    if (isFound == false)
    {
        cout << "No result found." << endl;
    }