【问题标题】:Class Recursive Array类递归数组
【发布时间】:2018-05-11 15:00:28
【问题描述】:

我的问题是我不明白为什么我无法获得所需的数组随机数总和。任何人都可以帮我找出错误吗?

#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!

【问题讨论】:

    标签: c++ arrays function class recursion


    【解决方案1】:

    使用对象字段进行递归是最不寻常的。像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;
        // ...
    }
    

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

    【讨论】:

      【解决方案2】:

      这次通话后:

      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】:

        当你调用sum_array索引等于max_size时,你应该在fill_array方法中清除它。

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

        【讨论】:

          【解决方案4】:

          fill_array() 后索引设置为 max_size。您必须在调用 sum_array() 之前将索引重置为 0

          【讨论】:

            猜你喜欢
            • 2020-04-14
            • 1970-01-01
            • 2011-02-25
            • 1970-01-01
            • 2016-12-13
            • 2021-11-24
            • 2017-10-08
            • 1970-01-01
            相关资源
            最近更新 更多