Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 编写一个递归函数,该函数接受数组并按相反顺序显示元素,而不在末尾启动数组的索引_C++_Arrays_Recursion - Fatal编程技术网

C++ 编写一个递归函数,该函数接受数组并按相反顺序显示元素,而不在末尾启动数组的索引

C++ 编写一个递归函数,该函数接受数组并按相反顺序显示元素,而不在末尾启动数组的索引,c++,arrays,recursion,C++,Arrays,Recursion,我的问题是:我做得对吗? 因为没有人能确切地告诉我 编写一个递归函数,该函数接受数组并按相反顺序显示元素 不在末尾启动数组的索引。(换句话说,不要写等同于 从阵列末尾开始打印的循环。) int-bleah(int-arr[],int-number) { cout你完全是在做你不想做的事情。你说你被要求写一个递归函数,它不是从最后开始的,而是你发送最后一个单元格 请尝试像这样使用递归函数: void bleah(int arr[], int number) { if (number !=

我的问题是:我做得对吗? 因为没有人能确切地告诉我

编写一个递归函数,该函数接受数组并按相反顺序显示元素 不在末尾启动数组的索引。(换句话说,不要写等同于 从阵列末尾开始打印的循环。)

int-bleah(int-arr[],int-number)
{

cout你完全是在做你不想做的事情。你说你被要求写一个递归函数,它不是从最后开始的,而是你发送最后一个单元格

请尝试像这样使用递归函数:

void bleah(int arr[], int number)
{
    if (number != arr.length()-1)
        bleah(arr, number + 1);
    cout << arr[number] << ' ';
}
一个完整的运行代码(以及main方法)。我理解初学者在开始时有点困难。不要在这里为苛刻的评论而烦恼。 我使用了向量而不是传统的数组,只是为了让事情变得更简单和可读:

#include <iostream>
#include <vector>

using namespace std;
//Typedef the vector to a simple name, array
typedef vector<int> array;

//The actual, recursive function
void printReverse(const array arr, const unsigned int index){
    //The base condition of recursion
    if(index != arr.size()){
        printReverse(arr, index+1);
        cout << arr[index] <<" ";
    }
}

int main()
{
    int size;
    cout << "Enter size of the array: ";
    cin >> size;
    array arr;

    cout << "Enter elements: ";
    for(int i=0; i<size; i++){
        int a;
        cin >> a;
        arr.push_back(a); //add the input to the array(or vector)
    }
    //The recursive function call(starting with index 0)
    printReverse(arr, 0);
    return 0;
}
#包括
#包括
使用名称空间std;
//将向量定义为一个简单的名称数组
typedef矢量阵列;
//实际的递归函数
void printReverse(常量数组arr,常量无符号整数索引){
//递归的基本条件
如果(索引!=arr.size()){
反向打印(arr,索引+1);

很抱歉人们浪费了你的时间! 我最终得到了答案,并在Github上查找。 这是正确的答案

void bleah(int arr[], int number)
{
    if (number + 1 < 10)
    {
        bleah(arr, number + 1);
    }
    cout << arr[number] << ' ';
}

int main()
{
    int arr[11] = { 34,5,8,3,6,678,8,87,67,8 };
    for (int i = 0; i < 10; i++)
    {
        cout << arr[i] << ' ';
    }
    cout << endl;
    bleah(arr, 0);

    cin.get();
    cin.ignore();
    return 0;
}
void bleah(整数arr[],整数编号)
{
如果(数字+1<10)
{
布莱亚(arr,数字+1);
}

不欢迎来到Stack Overflow。请花点时间阅读并参考您可以在此处询问的内容和方式。“因为没有人能准确地告诉我…”你的测试用例怎么说?没关系。没有错误或类似堆栈溢出的问题。输出是通过30到0。我按照任务做得对吗?问你的教授,我怎么知道?没有教授。我是为自己做的。如果你帮不上忙,那你为什么要回答我的问题?有什么意义?谢谢你的回答ate!我是初学者,不要对我太苛刻。@Nick虽然你应该在这里学习如何提出好的问题。你的问题很不确定(@LionP表明你只是错过了任务要求)你没有陈述任何你的代码例子出了什么问题。得到它。下次我会考虑这个问题。“Nick,也许你最好及时考虑这个问题,并改进你的问题。任何其他事情都会导致你被封锁在这里问任何问题。这是可以的。但是我需要用一个数组来解决这个问题。”Nick然后简单地替换了VE。带数组的ctor.Vector在内部只是一个数组。如果查看printReverse()方法,语法上没有区别。只需接收数组引用作为参数,而不是Vector.:)
#include <iostream>
#include <vector>

using namespace std;
//Typedef the vector to a simple name, array
typedef vector<int> array;

//The actual, recursive function
void printReverse(const array arr, const unsigned int index){
    //The base condition of recursion
    if(index != arr.size()){
        printReverse(arr, index+1);
        cout << arr[index] <<" ";
    }
}

int main()
{
    int size;
    cout << "Enter size of the array: ";
    cin >> size;
    array arr;

    cout << "Enter elements: ";
    for(int i=0; i<size; i++){
        int a;
        cin >> a;
        arr.push_back(a); //add the input to the array(or vector)
    }
    //The recursive function call(starting with index 0)
    printReverse(arr, 0);
    return 0;
}
void bleah(int arr[], int number)
{
    if (number + 1 < 10)
    {
        bleah(arr, number + 1);
    }
    cout << arr[number] << ' ';
}

int main()
{
    int arr[11] = { 34,5,8,3,6,678,8,87,67,8 };
    for (int i = 0; i < 10; i++)
    {
        cout << arr[i] << ' ';
    }
    cout << endl;
    bleah(arr, 0);

    cin.get();
    cin.ignore();
    return 0;
}