1

I'm struggling to make this code work properly. The goal is to create fibonacci codes for values 0-127 to be used later in a program intended to compress a text file.

Examples

0: 1

1: 11

2: 011

3: 0011

4: 1011

5: 00011

6: 10011

for {1, 2, 3, 5, 8, 13...} with an extra 1 at the end to make it clear when the specific char is completed.

Any help would be appreciated, I've been playing with it for a while now trying to make it work, so if anything isn't clear ask.

If you have a better algorithm for this situation, that would be great as well if you could lead me in the right direction.

public static void makeFibCodes (String[] fibCodes) {

  int[] fibSeq = {1,2,3,5,8,13,21,34,55,89,144};
  String s = "1";

  for (int i = 1; i <= 127; i++) {
     s = "1";
     s = makeFibRec(fibSeq, i, s, false, 9);
     fibCodes[i] = s;
     System.out.println(s);
  }
}   

public static String makeFibRec (int[] fibSeq, int i, String s, boolean hasValue, int   fibI) {
  if (i == 0) {
     hasValue = true;

     return "0" + s ;
  } else if (i == 1) {

     hasValue = true;
     return "1" + s;
  } else {
     for (int j = fibI; j > 0; j--) {

        if (i > fibSeq[j]) {
           hasValue = true;
           s = "1" + s;
           i = i - fibSeq[j];

           return makeFibRec(fibSeq, i, s, hasValue, j) + s;
        } if (hasValue) {
           s = "0" + s;
        } 
     }

     return s;
  }
}
  • you have to use this way? – Bora Kasap Feb 11 '14 at 01:01
  • I have to write code to determine the values, I can't hard code the values into an array, if thats what you mean. – user3084920 Feb 11 '14 at 01:04
  • 1
    This is a stupid assignment then. Hasn't you professor ever seen a real problem? – jeremyjjbrown Feb 11 '14 at 01:45
  • I don't disagree... Unfortunately I can't just skip the assignment so I'm stuck trying to figure this out :-/ – user3084920 Feb 11 '14 at 01:49
  • Well, one thing I've noticed is you haven't said what problem you're running into. – Danation Feb 11 '14 at 01:55
  • For this version I'm getting long, seemingly random, strings of 1s and 0s: 1 00000100111001111 R 11000100111000100111001111 2 1100001001110000100111001111 O 110000010011100000100111001111 N 000000100111001111 M 1100111001111 L 110010010001110010010001110010001110001111 H 11000100011100010001110001111 4 001000100111000100111001111 C 110010000111001000011100001111 0 1100111001111 ! 110010011100100111001111 P 1100001001110000100111001111 W 1100010010011100010010011100100111001111 A 00001000011100001111 Y 001001001001110010010011100100111001111 11000100111000100111001111 E 110001110001111 – user3084920 Feb 11 '14 at 21:55

0 Answers0