recursion finding duplicates, almost working...
in
Programming Questions
•
10 months ago
I tried to code PhiLho's suggestion in this
topic. The goal is to eliminate duplicates numbers (replacing by random numbers tup to a limit) in an array. So I made this code using recursion. As one can see looking at the output, things go well until every duplicate is eliminated, then the function keep running mysteriously changing the numbers until it returns a very crazy array... I'm lost. What is happening? What am i missing?
thanks
here the code:
- int[] numbers = new int[6];
- int range = 8;
- void setup()
- {
- numbers = testRepeated(numbers, range);
- print("in setup: \n");
- println(numbers);
- }
- int [] testRepeated(int[] n, int range)
- {
- //sort
- n = sort(n);
- boolean found = false; // to se if i should call function again
- // to hold new number for testing and getting a different one
- int newNumber = n[0];
- print("@ begin of function sorted \n");
- println(n);
- println(" ");
- //find duplicates
- for (int i=0; i < n.length-1;i++)
- {
- //test against next number
- if (n[i] == n[i+1])
- {
- print("numbet at index = " + i + " is repeated \n");
- println(n);
- println(" ");
- found = true;
- // if equal draw a new and different one
- while (newNumber == n[i])
- {
- newNumber = (int)random(range+1);
- }
- // change number in array
- n[i+1] = newNumber;
- print("new number is = " + n[i+1] +"\n");
- println(n);
- println(" ");
- }
- }
- println("found: " + found);
- if (found)
- {
- println("once more");
- testRepeated(n, range);
- }
- print("returning\n");
- println(n);
- println(" ");
- return n;
- }
and an output to console:
@ begin of function sorted
[0] 0
[1] 0
[2] 0
[3] 0
[4] 0
[5] 0
numbet at index = 0 is repeated
[0] 0
[1] 0
[2] 0
[3] 0
[4] 0
[5] 0
new number is = 1
[0] 0
[1] 1
[2] 0
[3] 0
[4] 0
[5] 0
numbet at index = 2 is repeated
[0] 0
[1] 1
[2] 0
[3] 0
[4] 0
[5] 0
new number is = 1
[0] 0
[1] 1
[2] 0
[3] 1
[4] 0
[5] 0
numbet at index = 4 is repeated
[0] 0
[1] 1
[2] 0
[3] 1
[4] 0
[5] 0
new number is = 1
[0] 0
[1] 1
[2] 0
[3] 1
[4] 0
[5] 1
found: true
once more
@ begin of function sorted
[0] 0
[1] 0
[2] 0
[3] 1
[4] 1
[5] 1
numbet at index = 0 is repeated
[0] 0
[1] 0
[2] 0
[3] 1
[4] 1
[5] 1
new number is = 8
[0] 0
[1] 8
[2] 0
[3] 1
[4] 1
[5] 1
numbet at index = 3 is repeated
[0] 0
[1] 8
[2] 0
[3] 1
[4] 1
[5] 1
new number is = 8
[0] 0
[1] 8
[2] 0
[3] 1
[4] 8
[5] 1
found: true
once more
@ begin of function sorted
[0] 0
[1] 0
[2] 1
[3] 1
[4] 8
[5] 8
numbet at index = 0 is repeated
[0] 0
[1] 0
[2] 1
[3] 1
[4] 8
[5] 8
new number is = 3
[0] 0
[1] 3
[2] 1
[3] 1
[4] 8
[5] 8
numbet at index = 2 is repeated
[0] 0
[1] 3
[2] 1
[3] 1
[4] 8
[5] 8
new number is = 3
[0] 0
[1] 3
[2] 1
[3] 3
[4] 8
[5] 8
numbet at index = 4 is repeated
[0] 0
[1] 3
[2] 1
[3] 3
[4] 8
[5] 8
new number is = 3
[0] 0
[1] 3
[2] 1
[3] 3
[4] 8
[5] 3
found: true
once more
@ begin of function sorted
[0] 0
[1] 1
[2] 3
[3] 3
[4] 3
[5] 8
numbet at index = 2 is repeated
[0] 0
[1] 1
[2] 3
[3] 3
[4] 3
[5] 8
new number is = 0
[0] 0
[1] 1
[2] 3
[3] 0
[4] 3
[5] 8
found: true
once more
@ begin of function sorted
[0] 0
[1] 0
[2] 1
[3] 3
[4] 3
[5] 8
numbet at index = 0 is repeated
[0] 0
[1] 0
[2] 1
[3] 3
[4] 3
[5] 8
new number is = 8
[0] 0
[1] 8
[2] 1
[3] 3
[4] 3
[5] 8
numbet at index = 3 is repeated
[0] 0
[1] 8
[2] 1
[3] 3
[4] 3
[5] 8
new number is = 8
[0] 0
[1] 8
[2] 1
[3] 3
[4] 8
[5] 8
numbet at index = 4 is repeated
[0] 0
[1] 8
[2] 1
[3] 3
[4] 8
[5] 8
new number is = 7
[0] 0
[1] 8
[2] 1
[3] 3
[4] 8
[5] 7
found: true
once more
@ begin of function sorted
[0] 0
[1] 1
[2] 3
[3] 7
[4] 8
[5] 8
numbet at index = 4 is repeated
[0] 0
[1] 1
[2] 3
[3] 7
[4] 8
[5] 8
new number is = 0
[0] 0
[1] 1
[2] 3
[3] 7
[4] 8
[5] 0
found: true
once more
@ begin of function sorted
[0] 0
[1] 0
[2] 1
[3] 3
[4] 7
[5] 8
numbet at index = 0 is repeated
[0] 0
[1] 0
[2] 1
[3] 3
[4] 7
[5] 8
new number is = 3
[0] 0
[1] 3
[2] 1
[3] 3
[4] 7
[5] 8
found: true
once more
@ begin of function sorted
[0] 0
[1] 1
[2] 3
[3] 3
[4] 7
[5] 8
numbet at index = 2 is repeated
[0] 0
[1] 1
[2] 3
[3] 3
[4] 7
[5] 8
new number is = 0
[0] 0
[1] 1
[2] 3
[3] 0
[4] 7
[5] 8
found: true
once more
@ begin of function sorted
[0] 0
[1] 0
[2] 1
[3] 3
[4] 7
[5] 8
numbet at index = 0 is repeated
[0] 0
[1] 0
[2] 1
[3] 3
[4] 7
[5] 8
new number is = 4
[0] 0
[1] 4
[2] 1
[3] 3
[4] 7
[5] 8
found: true
once more
@ begin of function sorted
[0] 0
[1] 1
[2] 3
[3] 4
[4] 7
[5] 8
found: false // here the correct array
returning
[0] 0
[1] 1
[2] 3
[3] 4
[4] 7
[5] 8
but things keep going...
????
returning
[0] 0
[1] 4
[2] 1
[3] 3
[4] 7
[5] 8
returning
[0] 0
[1] 1
[2] 3
[3] 0
[4] 7
[5] 8
returning
[0] 0
[1] 3
[2] 1
[3] 3
[4] 7
[5] 8
returning
[0] 0
[1] 1
[2] 3
[3] 7
[4] 8
[5] 0
returning
[0] 0
[1] 8
[2] 1
[3] 3
[4] 8
[5] 7
returning
[0] 0
[1] 1
[2] 3
[3] 0
[4] 3
[5] 8
returning
[0] 0
[1] 3
[2] 1
[3] 3
[4] 8
[5] 3
returning
[0] 0
[1] 8
[2] 0
[3] 1
[4] 8
[5] 1
returning
[0] 0
[1] 1
[2] 0
[3] 1
[4] 0
[5] 1
in setup:
[0] 0
[1] 1
[2] 0
[3] 1
[4] 0
[5] 1
1