I've got some conditions that i forgot to mention: the largest size is a 2d array of size 10 and there are the same amount of columns as there are rows.
I'm not sure how, but I've gotten it to print out every permutation with some duplicates. here's the code:
Code:
int permute(int mmin[10][10], int mdiff[10][10], int size, int realsize){
int q, total=0, sum=0, i, j;
for(q=size;q>=0;q--){
mmin=swap(mmin, size, q, realsize);
sum=mmin[q][q];
sum+=permute(mmin, mdiff, size-1, realsize);
for(i=0;i<=realsize;i++){
for(j=0;j<=realsize;j++){
printf("%d", mmin[i][j]);
}
printf("\n");
}
if(sum>total)
total=sum;
//printf("sum is %d\n", sum);
mmin=swapback(mmin, q, size, realsize);
printf("\n");
}
return total;
}
int swap(int mmin[10][10], int j, int k, int realsize){
int i, temp;
for(i=0;i<=realsize;i++){
printf("swapping [%d][%d]=%d with [%d][%d]=%d\n", i, j, mmin[i][j], i, k, mmin[i][k]);
temp=mmin[i][j];
mmin[i][j]=mmin[i][k];
mmin[i][k]=temp;
}
return mmin;
}
int swapback(int mmin[10][10], int j, int k, int realsize){
int i, temp;
for(i=0;i<=realsize;i++){
printf("swapping back [%d][%d]=%d with [%d][%d]=%d\n", i, j, mmin[i][j], i, k, mmin[i][k]);
temp=mmin[i][j];
mmin[i][j]=mmin[i][k];
mmin[i][k]=temp;
}
return mmin;
}
ignore mdiff. swapback is different only because i wanted it to print "swapping back..." instead of just "swapping" for tracing purposes. The sum doesn't seem to work. i'm guessing it has to do with "sum+=permute(...)" and all of the swapping.