Alex
Alex

Reputation: 47

Class Recursive Array

My problem here is that I do not get why I cannot get the desired sum of the array's random numbers. Could anyone help me to figure out the error, please?

#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;
}

The output is:

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

Upvotes: 1

Views: 67

Answers (4)

gsamaras
gsamaras

Reputation: 73394

After this call:

connect.fill_array();

index is equal to max_size. You want to re-initialize it to 0, when you are done filling the array (so that it's available for the other function), like this:

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

Now the output is:

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

Personal opinion:

Making an index a data memebr of class, in order to share it between two functions, but with no need of sharing (I mean it's not that the one uses the current value in an intermediate step of the other), is kind of odd, and can lead to mistakes, as you experienced already.

The index, i.e. the counter for looping over the array should be local-scoped to the function that loops the array at that time, so I propose to discard index from your class, as a data member, and pass it as a parameter in your functions. Moreover, you could prodive a default value for that parameter, since you want to loop from the start of the array.

Putting everything together, we get:

#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;
}

That ! in the final print scared me a bit, you might want to remove it (replace it with a dot for example), since it might confuse the user, with factorials.

Upvotes: 1

xs0
xs0

Reputation: 2897

It is most unusual to use object fields for recursion.. variables like index are usually passed as parameters:

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;
    // ...
}

Otherwise, like other answers say, in your original code you forgot to reset the index (that's exactly why it's weird to have it stored in the class).

Upvotes: 2

rafix07
rafix07

Reputation: 20934

When you call sum_array index is equal to max_size, you should clear it in fill_array method.

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

Upvotes: 1

ufok
ufok

Reputation: 193

After fill_array() index is set to max_size. You have to reset index to 0 before calling sum_array()

Upvotes: 0

Related Questions