c/c++ permtation file
February 23, 2010 9:49 AM Subscribe
I need to write a program in C/C++ that takes all the permutations of a set of numbers, say {1,2,3,4}, and puts them in a binary file.
I have found quite a few algorithms on the internet that get the permutations and display them, but I need them in a file. For instance there are 24 permutations of {1,2,3,4} and so I would like to set up an array that is 96 (24*4) elements long, with every four being a new permutation, which I then write to a file. The one at the bottom of this
page seems to work, I just don't know how to get them all in a file. Any thoughts?
posted by amsterdam63 to computers & internet (15 answers total) 1 user marked this as a favorite
#include
#include
void print(const int *v, const int size)
{
if (v != 0) {
for (int i = 0; i <> printf("%4d", v[i] );
}
printf("\n");
}
} // print
void visit(int *Value, int N, int k)
{
int static level = -1;
level = level+1; Value[k] = level;
if (level == N)
print(Value, N);
else
for (int i = 0; i <> if (Value[i] == 0)
visit(Value, N, i);
level = level-1; Value[k] = 0;
}
int main()
{
const int N = 4;
int Value[N];
for (int i = 0; i <> Value[i] = 0;
}
visit(Value, N, 0);
system("pause");
}
posted by amsterdam63 at 9:53 AM on February 23, 2010