c - Find all possible arrangements of a n numbers in an array -


i have array contains lets [25,15,8,20] want find possible arrangements of numbers possible.

expected output:

25 15 8 20 25 15 20 8 25 20 15 8 25 20 8 15 25 8 20 15 25 8 15 20 15 25 8 20 15 25 20 8 15 20 25 8 15 20 8 25 15 8 20 25 15 8 25 20 20 25 15 8 20 25 8 15 20 8 25 15 20 8 15 25 20 15 25 8 20 15 8 25 8 15 20 25 8 15 25 20 8 25 15 20 8 25 20 15 8 20 15 25 8 20 25 15   void print(int *num, int n) {  int i;  ( = 0 ; < n ; i++)     printf("%d ", num[i]);    printf("\n"); } int main() {  int num[n];  int *ptr;  int temp;  int i, n, j;  printf("\nhow many number want enter: ");     scanf("%d", &n);  printf("\nenter list of numbers see combinations:\n");  (i = 0 ; < n; i++)     scanf("%d", &num[i]);  (j = 1; j <= n; j++) {     (i = 0; < n-1; i++) {         temp = num[i];         num[i] = num[i+1];         num[i+1] = temp;         print(num, n);  } }  return 0; } 

the above program not giving possible outputs. how internal swap , combinations

there several aspect finding permutations , number of permutations. shown comments below, calculate number of permutations k size groups in total on n elements, find factorial on n divided factorial number of k size groups can make n elements. case of finding permutations 4 elements in 4 elements array, there 24 possible permutations.

the permutations available found recursively. on following , let me know if have questions:

#include <stdio.h> #include <stdlib.h>  void swap (int *x, int *y); unsigned long long nfact (size_t n); unsigned long long pnk (size_t n, size_t k); void permute (int *a, size_t i, size_t n); void prnarray (int *a, size_t sz);  int main (void) {      int array[] = { 25, 15, 8, 20 };     size_t sz = sizeof array/sizeof *array;      /* calculate number of permutations */     unsigned long long p = pnk (sz , sz);     printf ("\n total permutations : %llu\n\n", p);      /* permute array of numbers */     printf (" permutations:\n\n");     permute (array, 0, sz);     putchar ('\n');      return 0; }  /* function swap values @ 2 pointers */ void swap (int *x, int *y) {   int temp;     temp = *x;     *x = *y;     *y = temp; }  /* calculate n factorial */ unsigned long long nfact (size_t n) {   if (n <= 0) return 1;     unsigned long long s = n;      while (--n) s *= n;      return s; }  /* calculate possible permutations */ unsigned long long pnk (size_t n, size_t k) {   size_t d = (k < n ) ? n - k : 1;     return nfact (n) / nfact (d); }  /* permute integer array elements 'i' through 'n' */ void permute (int *a, size_t i, size_t n) {   size_t j;     if (i == n)         prnarray (a, n);     else         (j = i; j < n; j++) {             swap ((a+i), (a+j));             permute (a, i+1, n);             swap ((a+i), (a+j));  // backtrack         } }  void prnarray (int *a, size_t sz) {   size_t i;     (i = 0; < sz; i++) printf (" %2d", a[i]);     putchar ('\n'); } 

use/output

$ ./bin/permute4int   total permutations : 24   permutations:   25 15  8 20  25 15 20  8  25  8 15 20  25  8 20 15  25 20  8 15  25 20 15  8  15 25  8 20  15 25 20  8  15  8 25 20  15  8 20 25  15 20  8 25  15 20 25  8   8 15 25 20   8 15 20 25   8 25 15 20   8 25 20 15   8 20 25 15   8 20 15 25  20 15  8 25  20 15 25  8  20  8 15 25  20  8 25 15  20 25  8 15  20 25 15  8 

note: strings, approach works fine, not result in lexical sorting of possible permutations.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -