In: Computer Science
i need this program to also print out the number of combinations
#include <stdio.h>
#include <string.h>
#define N 10
void generate(char *array, int n)
{
if (n==0)
{
printf("%s\n",array);
return;
}
for (int i = 0; i < n; ++i)
{
// generate all of the permutations
that end with the last element
generate(array, n-1);
// swap the element
char temp = array[n-1];
array[n-1] = array[(n%2)*i];
array[(n%2)*i] = temp;
}
}
int main(int argc, char const *argv[])
{
char array[N];
printf("Enter word: ");
scanf("%s",array);
generate(array, strlen(array));
return 0;
}
#include <stdio.h> #include <string.h> #define N 10 void generate(char *array, int n, int* count) { int i; if (n==0) { printf("%s\n",array); *count = *count + 1; return; } for (i = 0; i < n; ++i) { // generate all of the permutations that end with the last element generate(array, n-1, count); // swap the element char temp = array[n-1]; array[n-1] = array[(n%2)*i]; array[(n%2)*i] = temp; } } int main(int argc, char const *argv[]) { char array[N]; int count = 0; printf("Enter word: "); scanf("%s",array); generate(array, strlen(array), &count); printf("Total number of combinations = %d\n",count); return 0; }