【问题标题】:Ackermann Table Geneation阿克曼表生成
【发布时间】:2015-05-28 16:40:05
【问题描述】:

我正在努力了解有关 Ackermann 函数、递归时间和一般函数学的更多信息,但是,我的代码无法编译。我感觉这与 acktgen() 中的数组有关,但我不是 100% 确定。

#include <stdlib.h>
#include <iostream>
using namespace std;

int ack(int m, int n){
    if(m==0) return n+1;
    else if (n==0) return ack(m,1);
    else return ack(m-1,ack(m,n-1));
}

int acktgen(const int s, const int t){
    int acktable[s+1][t+1];
    for (int i = 1 ; i= t+1; ++i){ //column labels
        acktable[0][i]= i-1 ;
    }
    for (int i = 1 ; i= s+1; ++i){  //row labels
        acktable[i][0]= i-1 ;
    } 
    for (int i = 1; i<=s+1; ++i){
        for (int j = 1; j<=t+1; ++j){
            acktable[i][j]= ack(i-1,j-1);
        }
    }
    return(acktable);
}

int main(){
    for(int i=0;i<5;i++) {
        for(int j=0;j<5;j++) {
            cout<<acktgen(4,4)[i][j]<< "\t";
        }
    }
}

我知道这不是最有效的阿克曼算法,但我只是将其用作示例。

编译器错误:

prog.cpp: In function 'int acktgen(int, int)':
prog.cpp:26:17: error: invalid conversion from 'int (*)[(t + 1)]' to 'int' [-fpermissive]
  return(acktable);
                 ^
prog.cpp:14:6: warning: address of local variable 'acktable' returned [-Wreturn-local-addr]
  int acktable[s+1][t+1];
      ^
prog.cpp: In function 'int main()':
prog.cpp:32:24: error: invalid types 'int[int]' for array subscript
    cout<<acktgen(4,4)[i][j]<< "\t";
                        ^

【问题讨论】:

  • 请发布编译器错误。
  • 完成了。我知道我忘了做某事
  • 顺便说一句,您使用的是什么编译器?我得到的错误是:ideone.com/sZ4OMF 这些错误是我所期望的。
  • 我正在使用 IDEONE C++14。 ideone.com/4CglyM
  • 那些编译器错误与发布的代码无关。

标签: c++ function multidimensional-array ackermann


【解决方案1】:

让我们来看看每个错误和警告:

> prog.cpp: In function 'int acktgen(int, int)': prog.cpp:26:17: error:
> invalid conversion from 'int (*)[(t + 1)]' to 'int' [-fpermissive]  
> return(acktable);

您声明了您的 acktgen 函数以返回一个 int,但您却返回了一个地址。我不知道你的意图是什么,但如果是从数组中返回单个值,那么你返回那个值,即

return acktgen[0][4];

或类似的东西。


> prog.cpp:14:6: warning: address of local variable 'acktable' returned
> [-Wreturn-local-addr]   int acktable[s+1][t+1];

您正在返回一个局部变量的地址。这样做是 C++ 中未定义的行为,所以不要这样做。当函数返回时,所有局部变量都消失了。因此,试图返回(和使用)不存在的东西的地址是行不通的(或者它可能会起作用,但只是偶然)。


> prog.cpp: In function 'int main()': prog.cpp:32:24: error: invalid
> types 'int[int]' for array subscript
>     cout<<acktgen(4,4)[i][j]<< "\t";

这是不正确的,因为 acktgen 返回一个 int,而不是一个数组或类似数组的对象。

基本上,您需要向我们提供更多关于您希望在 acktgen 函数中返回的信息。它真的应该是一个数组吗?它应该只是一个值吗?


您的代码中的一些内容:

1) 用非常量表达式声明数组在 ANSI 中是不合法的 C++:

int acktable[s+1][t+1];

那行代码不是合法的 C++。要模拟一个数组,可以使用std::vector&lt;std::vector&lt;int&gt;&gt;

std::vector<std::vector<int>> acktable(s+1, std::vector<int>(t+1));

2) 你的循环条件写错了:

   for (int i = 1 ; i = t+1; ++i){ //column labels

看看中间条件——这就是你想要的,只有在 i == t+1 时循环才会继续?之后你在循环中犯了同样的错误。

其次,您的循环越界访问数组。 C++ 中的数组是从 0 开始的,但如果你仔细观察你的循环,你就会发现它越界了:

    for (int i = 1; i<=s+1; ++i){
        for (int j = 1; j<=t+1; ++j){
            acktable[i][j]= ack(i-1,j-1);
        }
    }

最后一次迭代会发生什么?您访问acktable[s+1][t+1],这是超出范围的。该数组的最高索引是 st,因为我们从 0 开始计数。

【讨论】:

  • 我希望 acktgen (ack table gen) 返回二维数组,我意识到我在这些循环中的错误,它们应该小于所有循环。最后一点,当我将 acktable 定义为 (s+1)x(t+1) 数组时,不应该定义 acktable[s+1][t+1] 吗?我知道我们从零开始计数,但这对我的索引的工作方式有何影响?此外,代码已根据您的一些建议进行了编辑,并且出现了更多错误,特别是涉及您的 std::vector 调用。
  • 如果你想返回一个数组,你不能按照你的方式去做。返回 std::vector&lt;std::vector&lt;int&gt;&gt;int** 动态创建数组的位置。不,它对您访问数组的方式无效,如果您声明:int array[10],则有效索引为 0、1、2、3、4、5、6、7、8 和 9。访问数组[10]超出范围。
猜你喜欢
  • 2010-11-28
  • 2012-01-31
  • 1970-01-01
  • 2015-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多