类递归数组

时间:2017-11-28 08:09:14

标签: c++ arrays function class recursion

我的问题在于我无法理解为什么我无法获得数组随机数的所需总和。有人可以帮我弄清楚错误吗?

#include <iostream>
using namespace std;
class Recursion{
   int max_size;
   double sum;
   int index;
   double* arr;
public:
   Recursion(int);
   void fill_array();
   void sum_array();
};
Recursion::Recursion(int size){//from main
   max_size = size;
   sum = 0;
   index = 0;
   arr = new double[max_size];
}
void Recursion::fill_array(){
   if (index == max_size){
      cout << "Array is Full." << endl;
      //stop array
   }
   else{
      arr[index] = rand() % 10+1;
      cout << arr[index] << endl;
      index++; 
      fill_array();
   }
}
void Recursion::sum_array(){
   if (index == max_size){
      cout << "Sum is: "<< sum << "!"<< endl;
   }
   else{
      sum = sum + arr[index];
      index++;
      sum_array();
   }
}
int main(){
   Recursion connect(5);
   connect.fill_array();
   connect.sum_array();
   return 0;
}

输出结果为:

8
10
4
9
1
Array is Full.
Sum is: 0!

4 个答案:

答案 0 :(得分:2)

使用对象字段进行递归是最常见的。像index这样的变量通常作为参数传递:

double Recursion::sum_array(int index) {
    if (index >= max_size) {
        return 0;
    } else {
        return arr[index] + sum_array(index + 1);
    }
}

int main() {
    // ...
    cout << "Sum is: "<< sum_array(0) << "!"<< endl;
    // ...
}

否则,就像其他答案一样,在您的原始代码中,您忘记重置索引(这就是为什么将它存储在课堂中的原因很奇怪)。

答案 1 :(得分:1)

当您致电sum_array索引等于max_size时,您应该使用fill_array方法清除它。

void Recursion::fill_array(){
if (index == max_size){
  cout << "Array is Full." << endl;
  //stop array
  index = 0;
}

答案 2 :(得分:1)

此次电话会议后:

connect.fill_array();

index等于max_size。当你完成填充数组(以便它可用于其他函数)时,你想将它重新初始化为0,如下所示:

   if (index == max_size){
      cout << "Array is Full." << endl;
      index =0;
      //stop array
   }

现在输出是:

4
7
8
6
4
Array is Full.
Sum is: 29!

个人意见:

使索引成为类的数据memebr,以便在两个函数之间共享它,但不需要共享(我的意思是它不是在另一个的中间步骤中使用当前值的那个) ),有点奇怪,并且可能导致错误,正如你已经体验过的那样。

索引,即循环遍历数组的计数器应该是局部作用于当时循环数组的函数,因此我建议从类中丢弃index作为数据成员,并且将其作为函数中的参数传递。此外,您可以为该参数设置默认值,因为您希望从数组的开头循环。

把所有东西放在一起,我们得到:

#include <iostream>
using namespace std;
class Recursion{
   int max_size;
   double sum;
   double* arr;
public:
   Recursion(int);
   void fill_array(int index);
   void sum_array(int index);
};
Recursion::Recursion(int size){//from main
   max_size = size;
   sum = 0;
   arr = new double[max_size];
}
void Recursion::fill_array(int index = 0){
   if (index == max_size){
      cout << "Array is Full." << endl;
      //stop array
   }
   else{
      arr[index] = rand() % 10+1;
      cout << arr[index] << endl;
      index++; 
      fill_array(index);
   }
}
void Recursion::sum_array(int index = 0){
   if (index == max_size){
      cout << "Sum is: "<< sum << "!"<< endl;
   }
   else{
      sum = sum + arr[index];
      index++;
      sum_array(index);
   }
}
int main(){
   Recursion connect(5);
   connect.fill_array();
   connect.sum_array();
   return 0;
}

最终打印件中的!让我感到有些害怕,你可能想要删除它(例如用一个点替换它),因为它可能会让用户感到困惑,而且是因子。

答案 3 :(得分:0)

在fill_array()之后将index设置为max_size。在调用sum_array()

之前,必须将index重置为0