How to write a recursion function correctly?

edited December 2015 in How To...

Hi everyone,

I tried to write a recursion function to calc a number. There seems to have no syntax or runtime error, but I just could not get the result right (result I got are all zeros). I need your help to inspect my codes here. Thanks a lot!

    FloatList RSVs = new FloatList();
    void setup() {

      for (int i = 20; i < 100; i++) {
        float ram = random(-20, 120);
        RSVs.append(ram);
      }
      println(calcK(3));

      getKs();

      printArray(Ks);
    }


    FloatList Ks = new FloatList();

    void getKs() {

      for (int i = 0; i < RSVs.size(); i++) {

        Ks.append(calcK(i));
      }
    }


    float calcK(int RSVIndex) {
      float K;

      if (RSVIndex >= 0) {
        K = (6-1)/6*calcK(RSVIndex-1)+1/6*RSVs.get(RSVIndex);
      } else {
        K = 50;
      }
      return K;
    }

Answers

Sign In or Register to comment.