Listing all permutations as a List in C

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Listing all permutations as a List in C

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Listing all permutations as a List in C

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật