Posted by James Durbin on 1999-04-27
Benjamin Johnston wrote:
> (this is Java, but
> it shouldn't be hard to convert to c)....
>
> public class DisplayCombinations {
> private static int[] array = { 2, 7, 9, 4 }; /*the data to sort -- doesn't
> need to be numbers*/
> private static int[] temp = { 0, 0, 0, 0 };
> /* 'temp' must be a zero array of the same size as 'array' */
>
> public static void main(String[] args) {
> combinations(0);
> }
>
> private static void combinations(int depth) {
> int i;
> if (depth==array.length) {
> for (i=0; i<array.length; i++) {
> System.out.print(array[temp[i]-1]);
> }
> System.out.print("\n");
> } else {
> i=0;
> do {
> if (temp[i]==0) {
> temp[i]=depth+1;
> combinations(depth+1);
> temp[i]=0;
> }
> i++;
> } while (i<temp.length);
> }
>
> return;
> }
> }
There's a couple things I want to say no. ! thanks for the reply no. 2
although I can convert this to C you probably should have... it has 1
minor error that I noticed while looking over the code.... it never
switches any elements around... meaning it'll print the same thing over
and over n! times... but it should help me as to looking at this problem
from a slightly different approch... I too was trying to use
recursion... but I was using a for loop, this seems to do the same
thing, but I'm not sure I haven't worked out if it does or not... my
recursion went something like this... (replaceing the do/while loop and
converting it into Java for your original code is in Java, becides
there's not much difference since Java was created to be simmilar to C
(I believe)) :
for(i=depth;i<array.length-1;i++)
{
combanations(depth+1);
switchit(depth);
}
and having an extra function:
private static void switchit(int depth)
{
int t;
t=array[depth];
array[depth]=array[depth+1];
array[depth+1]=t;
return;
}
this was just one variation I used... well... test your algarithm
tomorrow with the positioning of my switchit() function where I think it
should be to see if I understood it... well.. thanks again for finaly
answering my question... I had almost given up on having someone answer
it...
--
/*
From: James H. Durbin
Email: divine-bovine@xxxx.xxx
*/
Previous post | Next post | Timeline | Home