This program must calculate permutations but it gives me a result with smiley ,
Can someone help me, Thanks!!

#include <conio.h>
#include <stdio.h>
#include <math.h>

const n = 10,num=4;
int i,a[n];

void tipareste(){
	int i;
	for(i=0;i<num;i++)
	{
		printf(" %d \n", a[i]);
	}
}
void permuta(int k)
{
	int i,x;
	if(k==1) tipareste()   ;
	else{
		for(i=0;i<k;i++){
			x=a[i];
			a[i]=a[k];
			a[k]=x;
			permuta(k-1);
			x=a[i];
			a[i]=a[k];
			a[k]=x;
		}
	}
}

int main()
{
	for(i=0;i<n;i++){
		a[i]=i;
	}
	printf("\n");
	permuta(n);
	return 0;
}

This program

Props for making an honest attempt at converting that Pascal program, but your translation is a bit lacking. Compare and contrast:

#include <stdio.h>

void swap(int *a, int *b)
{
    int temp = *a;
    *a = *b;
    *b = temp;
}

void tipareste(int a[], int n)
{
    int i;
    
    for (i = 0; i < n; i++)
        printf("%d ", a[i]);
        
    putchar('\n');
}

void permuta(int a[], int n, int k)
{
    if (k == 1)
        tipareste(a, n);
    else {
        int i;
        
        for (i = 0; i < k; i++) {
            swap(&a[i], &a[k - 1]);
            permuta(a, n, k - 1);
            swap(&a[i], &a[k - 1]);
        }
    }
}

#define N 3

int main(void)
{
    int a[N];
    int i;
    
    for (i = 0; i < N; i++)
        a[i] = i;
        
    permuta(a, N, N);
    
    return 0;
}

As far as the algorithm goes, notice how a[k - 1] is used in the swap rather than a[k] . This is a difference between the 0-based indexing of C and the 1-based indexing in the Pascal program.

I need combination of 0,1,2,3,4,5,6,7,8,9 with 4-length-digit

I need combination of 0,1,2,3,4,5,6,7,8,9 with 4-length-digit

I'm sensing lack of effort. Don't expect me to do your homework for you.

This article has been dead for over six months. Start a new discussion instead.