如何将该顺序搜索代码更改为递归? [英] How can I change this sequential search code to being recursive?

查看:79
本文介绍了如何将该顺序搜索代码更改为递归?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<iostream>
using namespace std;
int array1[5];
int i, search;
bool found= false;
void main ()
{
    cout<<"Enter the five elements of an integer array \n";
    for (i=0 ; i<5 ; i++)
    {
        cin>>array1[i];
    }
    cout<<endl<<"Your integer array is:"<<endl;
    for (i=0 ; i<5 ; i++)
    {
        cout<<array1[i];
        cout<<"\t";
    }
    cout<<endl<<endl;
    cout<<"Enter a number to search for: \n";
    cin>>search;
    
    for (i=0 ; i<5 ; i++)
    {
        if (search==array1[i])
        {
            cout << "The number " << search << " was found and its index is " << i << endl;
            found= true;
            break;
        }
     }
     if (!found)
         cout << "The number " << search << " was not found!! " << endl;
}



谢谢大家的帮助.



Thanks for anyone''s help.

推荐答案

这闻起来像作业,所以我给您一个简单的英语描述,而不是为您编码.

添加一个接受两个整数(索引和搜索)并返回布尔值的函数.首先,该函数应检查索引,如果它大于或等于数组的大小,则返回false(表示未找到该项目).否则,它应该将指定索引处的数组项与搜索值进行比较.如果值相等,则该函数可以返回true,否则它将返回调用自身并将值(当前索引+ 1)作为索引参数传递的结果.要开始搜索,请从main调用参数为0的函数.
This smells like homework so I''ll give you a plain English description instead of coding it up for you.

Add a function that takes in two integers (index and search) and returns a boolean. First the function should check the index, if it''s greater than or equal to the size of the array then return false (meaning the item is not found). Otherwise it should compare the array item at the index specified to the search value. If the value is equal then the function can return true, otherwise it should return the result of calling itself and passing the value (current index + 1) as the index parameter. To start the search call the function from main with a parameter of 0.


我将为您提供一般的逻辑,您应该尝试自己实现:

输入具有5个整数的数组的函数;如果找到目标,请打印结果并退出功能.如果不是正确的函数,请从函数内部调用相同的函数,但使用4个整数组成的数组(提示:++ array1).
当您到达数组末尾时,无论如何都要退出...
I''ll give you the general logic, you should try implementing it yourself:

enter the function with an array of 5 integers; If you''ve found your target, print your result, and exit the function. If it''s not the right one, call the same function from within the function, but with an array of 4 integers (hint: ++array1).
When you''ve reached the end of array, exit anyway...


Grag.
似乎是学校的作业,而答案是:

int find(int * pArray,int nLength,int nStart,int nTarget)
{
如果(nStart == nLength)
返回-1;

if(nTarget == pArray [nStart])
返回nStart;
其他
return find(pArray,nLength,nStart ++,nTarget);
}

希望对您有帮助:)
祝你好运!
Hi, Grag.
It seems an school homework and here is the answer:

int find( int *pArray, int nLength, int nStart, int nTarget )
{
if ( nStart == nLength )
return -1;

if ( nTarget == pArray[nStart] )
return nStart;
else
return find( pArray, nLength, nStart++, nTarget );
}

Hope it will help you:)
Good luck!


这篇关于如何将该顺序搜索代码更改为递归?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆