Struggling to identify what is wrong with my code. I want to understand why my intuition here is wrong. The reason I want to produce a bunch of list is to hopefully enumerate at the start. But what am I doing wrong?
#include <stdio.h>
#include <stdlib.h>
int maxI(int n)
{
int result = 1;
for (int i = 1; i <= n; i++)
{
result *= i;
}
return result;
}
void swap(int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
int **pMut(int *arr, int size, int max)
{
int **results = (int **)malloc(sizeof(int *) * max);
// pointer index, outer loop with reset, inner loop with swaps
int index, x, y;
for (index = 0; index < max; index++)
{
results[index] = (int *)malloc(sizeof(int) * size);
for (x = 0; x < size - 1; x++)
{
for (y = 1; y < size; y++)
{
swap(&arr[x], &arr[y]);
for (int z = 0; z < size; z++)
{
results[index][z] = arr[z];
}
}
}
}
swap(&arr[x], &arr[y]);
return results;
}
int main()
{
int target[] = {1, 2, 3};
int size = sizeof(target) / sizeof(target[0]);
int max = maxI(size);
int **result = pMut(target, size, max);
for (int index = 0; index < max; index++)
{
for (int x = 0; x < size; x++)
{
printf("%d", result[index][x]);
}
printf("n");
}
for (int i = 0; i < max; i++)
{
free(result[i]);
}
free(result);
return 0;
}
results show:
321
123
321
123
321
123
I played around where I would but the swap back to reset the loop, but its either 123 6 times or out of bounds and other repeats.
5
In evaluating your solution, for deriving and printing the various combinations/permutations of integers, it appears that you were attempting to apply the functionality of the “C” program example that I found https://www.geeksforgeeks.org/write-a-c-program-to-print-all-permutations-of-a-given-string/ which provides the permutations for a string to an array of integers. If this is just a coincidence, you might want to refer to this example.
In reviewing your code it appears that your program is attempting to derive a two-dimensional integer array to store the various permutations, as opposed to the recursive function calling that occurs in the example I believe is being used as a building block.
Using that example, following is a refactored version of the code to accommodate integer permutations in lieu of character permutations.
#include <stdio.h>
#include <stdlib.h>
// Function to swap two integers - based upon the swapping of characters
void swap(int* x, int* y) {
int temp = *x;
*x = *y;
*y = temp;
}
void permuteRec(int* str, int idx, int n) {
// Base case
if (idx == n - 1) {
for (int i = 0; i < n; i++)
printf("%d ", str[i]);
printf("n");
return;
}
for (int i = idx; i < n; i++) {
// Swapping
swap(&str[idx], &str[i]);
// First idx+1 integers
permuteRec(str, idx + 1, n);
// Backtrack
swap(&str[idx], &str[i]);
}
}
int main() {
int str[] = {1, 2, 3};
int n = sizeof(str) / sizeof(str[0]);
permuteRec(str, 0, n); // Bypass using a wrapper function as the size is being calculated within the main function
return 0;
}
You will see the similarities to the sample program and your program, with the main difference of deriving the number of array elements within the “main” function and not utilizing a wrapper program.
Testing out this refactored code results in the following terminal output.
craig@Vera:~/C_Programs/Console/PermInteger/bin/Release$ ./PermInteger
1 2 3
1 3 2
2 1 3
2 3 1
3 2 1
3 1 2
As a further test, an array size of four elements was tested just to prove out that the number of permutations is derived and printed.
int main() {
int str[] = {1, 2, 3, 4};
int n = sizeof(str) / sizeof(str[0]);
permuteRec(str, 0, n); // Bypass using a wrapper function as the size is being calculated within the main function
return 0;
}
craig@Vera:~/C_Programs/Console/PermInteger/bin/Release$ ./PermInteger
1 2 3 4
1 2 4 3
1 3 2 4
1 3 4 2
1 4 3 2
1 4 2 3
2 1 3 4
2 1 4 3
2 3 1 4
2 3 4 1
2 4 3 1
2 4 1 3
3 2 1 4
3 2 4 1
3 1 2 4
3 1 4 2
3 4 1 2
3 4 2 1
4 2 3 1
4 2 1 3
4 3 2 1
4 3 1 2
4 1 3 2
4 1 2 3
Again, I suggest that you refer to the enclosed page link if that was not your reference and view how recursive calls were used in lieu of attempting to create and store data within a two-dimensional array.