简体   繁体   English

数组递归 无法获得正确的值来返回

[英]Recursion with an Array; can't get the right value to return

Solution found - in under 5 minutes, thanks folks! 找到解决方案-不到5分钟,谢谢大家!

Clarification: The contents of my array are the values 0-29. 澄清:我数组的内容是值0-29。 So array[0][0] = 0, while array[29][0] = 29 --- they're just test values. 所以array [0] [0] = 0,而array [29] [0] = 29 ---它们只是测试值。 Also, I have a potential solution that's been posted multiple times, going to try that. 另外,我已经发布了一个潜在的解决方案,请尝试一下。

Recursive Solution: Not working! 递归解决方案:不起作用! Explanation: An integer, time, is passed into the function. 说明:一个整数,时间,被传递给该函数。 It's then used to provide an end to the FOR statement ( counter<time ). 然后用于结束FOR语句( counter<time )。 The IF section ( time == 0 ) provides a base case where the recursion should terminate, returning 0. The ELSE section is where the recursive call occurs: total is a private variable defined in the header file, elsewhere. IF部分( time == 0 )提供了一种基本情况,递归应该终止,返回0。ELSE部分是递归调用发生的地方:total是在头文件中定义的私有变量。 It's initialized to 0 in a constructor, elsewhere. 在其他地方的构造函数中将其初始化为0。 The function calls itself, recursively, adding productsAndSales[time-1][0] to total, again, and again, until the base call. 该函数递归地调用自身,一次又一次地将productsAndSales[time-1][0]相加,直到基本调用为止。 Then the total is returned, and printed out later. 然后将总数返回,并在以后打印出来。 Well, that's what I hoped for anyway. 好吧,那是我一直希望的。

What I imagined would happen is that I would add up all the values in this one column of the array and the value would get returned, and printed out. 我想象会发生的事情是,我将把该数组的这一列中的所有值加起来,然后该值将被返回并打印出来。 Instead if returns 0. If I set the IF section to "return 1", I noticed that it returns powers of 2, for whatever value time is. 相反,如果if返回0。如果我将IF部分设置为“ return 1”,我注意到无论返回值是多少,它都会返回2的幂。 EG: Time = 3, it returns 2*2 + 1. If time = 5, it returns 2*2*2*2 + 1. EG:时间= 3,则返回2 * 2 +1。如果时间= 5,则返回2 * 2 * 2 * 2 + 1。

I don't understand why it's not returning the value I'm expecting. 我不明白为什么它没有返回我期望的值。 One thing I thought of is that I'm attempting to use private variable total in the return section, along with the recursive call...maybe that's a no-no somehow? 我想到的一件事是,我试图在return部分中使用私有变量total以及递归调用……也许这是不行吗?

int CompanySales::calcTotals( int time )
{
  cout << setw( 4 );
  if ( time == 0 )
   {
    return 0;
   }
  else
   {
    return total += calcTotals( productsAndSales[ time-1 ][ 0 ]);
   }
}

Iterative Solution: Working! 迭代解决方案:有效! Explanation: An integer, time, is passed into the function. 说明:一个整数,时间,被传递给该函数。 It's then used to provide an end to the FOR statement ( counter<time ). 然后用于结束FOR语句( counter<time )。 The FOR statement cycles through an array, adding all of the values in one column together. FOR语句遍历数组,将一列中的所有值加在一起。 The value is then returned (and elsewhere in the program, printed out). 然后返回该值(并打印出程序的其他位置)。 Works perfectly. 完美运作。

int CompanySales::calcTotals( int time )
{
 int total = 0;
 cout << setw( 4 );

 for ( int counter = 0; counter < time; counter++ )
 {
  total += productsAndSales[counter][0];
 }
 return total0;
}

Don't use the global total , make it an argument. 不要使用全局total ,而应将其作为参数。

int totals = calcTotals(time-1, 0); // Call it starting at the end, 
                                    // so we don't have to pass along the `time`


int CompanySales::calcTotals( int counter, int total )
{
  if ( counter == 0 ) {
    return total;
  }
  else {
    return calcTotals(counter - 1, total + productsAndSales[counter][ 0 ]);
  }
}

Now it's tail recursive too. 现在它也是尾递归的。

Well, in your recursive function you're expecting time as a parameter to your function, but when you make the recursive call, its passing the value of your productsAndSales array, not the (time - 1) that I would have expected. 好吧,在您的递归函数中,您希望时间作为函数的参数,但是当您进行递归调用时,它传递的是productAndSales数组的值,而不是我期望的(time-1)。

So assuming that the contents of your productAndSales array does not contain zero, the time == 0 termination check will never occur 因此,假设productAndSales数组的内容不包含零,则永远不会发生time == 0终止检查

Wrong argument being passed around: 传递了错误的论点:

total += calcTotals( productsAndSales[ time-1 ][ 0 ]);

Should be: 应该:

total +=  productsAndSales[ time ][ 0 ]  + calcTotals(time - 1);

应该

return total += productsAndSales[time - 1][0] + calcTotals(time - 1);

This should produce the same result as the iterative function. 这将产生与迭代函数相同的结果。

int CompanySales::calcTotals( int time )
{
  cout << setw( 4 );
  if ( time == 0 ){
    return 0;
  }
  else{
    return productsAndSales[time-1][ 0 ] + calcTotals( time - 1 );
  }
}

Nowhere in your recursive solution are you actually adding a productsAndSales value -- you are passing in those values as the time parameter to your calcTotals() function. 无处在你的递归解决方案,你实际上添加productsAndSales值-你在这些值传递的时间参数的calcTotals()函数。

So if total starts as zero, you are simply calling this function a number of times and never adding anything other than zero to it. 因此,如果total从零开始,您只是简单地多次调用此函数,并且从不添加零以外的任何东西。

Shouldn't this be 这不应该是

int CompanySales::calcTotals( int time )
{
  int total = 0;
  cout << setw( 4 );
  if ( time == 0 )
   {
    return 0;
   }
  else
   {
    return total += productsAndSales[ time ][ 0 ] + calcTotals( time - 1);
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM