3

I do understand I have mistakes in this code

#include <stdio.h>  
#include <stdlib.h>  
#include <string.h>  
  
int diziyi_yazdır(int dizi[], int dizi_uzunluğu)  
{  
    for (int i = 0; i < dizi_uzunluğu; i++)  
    {  
        printf("%d", dizi[i]);  
    }  
    printf("\n");  
}  
  
int kombnsyon_yazdır(int dizi[], int dizi_uzunluğu, int index)  
{  
    if (index == 4)  
    {  
      return 0;  
    }  
    for (int i = 0; i < dizi_uzunluğu; i++)  
    {  
        int geçici = dizi[index];  
        dizi[index] = dizi[i];  
        dizi[i] = geçici;  
        diziyi_yazdır(dizi, dizi_uzunluğu);  
        kombnsyon_yazdır(dizi, dizi_uzunluğu, index + 1);  
        dizi[i] = dizi[index];  
        dizi[i] = geçici;  
    }  
}  
  
int main()  
{       
    int dizi[4] = { 1, 2, 3, 4 };  
    kombnsyon_yazdır(dizi, 4, 0);  
} 

I was just expecting same sequences more than one and thats it but instead I got this output how? You can see there are duplicates in the program's output (only partly shown):

1234  
2134  
3124  
4123  
4321  
4312  
4312  
3132  
2133  
2331  
2313  
2313  
...
1111
1111
1111
1111
1111

For English readers, here is a translation:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int print_array(int arr[], int arr_length)
{
    for (int i = 0; i < arr_length; i++)
    {
        printf("%d", arr[i]);
    }
    printf("\n");
}

int print_combinations(int arr[], int arr_length, int index)
{
    if (index == 4)
    {
        return 0;
    }
    for (int i = 0; i < arr_length; i++)
    {
        int temp = arr[index];
        arr[index] = arr[i];
        arr[i] = temp;
        print_array(arr, arr_length);
        print_combinations(arr, arr_length, index + 1);
        arr[i] = arr[index];
        arr[i] = temp;
    }
}

int main()
{
    int arr[4] = { 1, 2, 3, 4 };
    print_combinations(arr, 4, 0);
}
5
  • Sory for confuguration Commented Nov 8 at 15:57
  • 4
    Don't be sorry, edit the question and clean it up. Commented Nov 8 at 16:01
  • 1
    You can use the "Edit" button to reformat your question so it's clear what you are asking. Commented Nov 8 at 16:01
  • Use code block for the code. Commented Nov 8 at 16:12
  • Using a debugger you can clearly see what is happening. Did you debug, setting breakpoints, inspecting variables? What were your findings? For sure, assigning twice to dizi[i] a different value, makes the first assignment useless. Also, if you swap two elements, be aware that if you visit the other element later, your code is making the same swap again. Commented Nov 8 at 18:42

2 Answers 2

6

The problem in your code is in the swap code to restore the original order after the recursive call:

        arr[i] = arr[index];
        arr[i] = temp;

You should change the second statement to arr[index] = temp; or in your code in Turkish: dizi[index] = geçici;

Also note these other mistakes:

  • print_array should have a void return type and its arr argument should be declared as a const array or pointer.
  • print_combinations too should be defined with a void return type and simply use return.
  • the termination test index == 4 works for an array length of 4 but will cause incorrect output or even undefined behavior for other array lengths. The correct test is if (index == arr_length), or even better, as Chris suggests, if (index >= arr_length).
  • index variables should be unsigned, the type size_t is idiomatic for such variables.
  • for good style, you should return 0 for success in the main function. This is implicit since c99 but it cannot hurt to make it explicit.
  • there is no need for <stdlib.h> or <string.h> for this program.

Here is a modified version:

#include <stdio.h>

void print_array(const int arr[], size_t arr_length)
{
    for (size_t i = 0; i < arr_length; i++) {
        printf("%d", arr[i]);
    }
    printf("\n");
}

void print_combinations(int arr[], size_t arr_length, size_t index)
{
    if (index >= arr_length) {
        return;
    }
    for (size_t i = 0; i < arr_length; i++) {
        int temp = arr[index];
        arr[index] = arr[i];
        arr[i] = temp;
        print_array(arr, arr_length);
        print_combinations(arr, arr_length, index + 1);
        arr[i] = arr[index];
        arr[index] = temp;
    }
}

int main(void)
{
    int arr[] = { 1, 2, 3, 4 };
    print_combinations(arr, sizeof(arr) / sizeof(arr[0]), 0);
    return 0;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Looking forward: with C23, instead of int temp = arr[index];, we could use typeof_unqual(arr[index]) temp = arr[index]; as a step towards maintenance reduction. Advanced: perhaps use the return value of print...(), when < 0, to form the immediate return value of print_...().
Thank you for these suggestions, but I shall keep the code portable. C2y should go one step further and allow auto temp = arr[index]; which is less awkward that the typeof_qual variant, albeit I would prefer let temp = arr[index]; over the new semantics of auto inherited from C++ (along with constexpr and nullptr).
thank you ı have more questions about the variotions of this code but since ı couldnt figured how to edit ı gave up but thank you.I just need to see how recursion works visually do you know any tool? you know for observing algorithms?
3

While the core problem has been identified, a few additional notes:

  • Make sure you compile with warnings on. E.g. -Wall -Wextra and if you're really determined, -Werror.
  • Your compiler would warn you that you reach the end of a non void function without returning anything. In fact, your function can return void. The same is true of print_array. That main can return int without an explicit return value is an exception to the rule.
  • You likely want to return when index equals arr_length rather than hard-coding in 4. In fact, you probably actually want to check that index is greater than or equal to arr_length and exit on that condition.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.