permutation

Aug 16, 2019 at 3:31pm
why the count result of permutation is wrong?

#include<iostream>
#include<string.h>

using namespace std;

void swap (char *x, char *y)
{
char temp;
temp = *x;
*x = *y;
*y = temp;
}


void Permute(char *a, int i, int n)
{
int j;

if (i == n-1)
cout<<"\n"<<a;
else
{
for (j = i; j < n; j++)
{

swap(a[i], a[j]);
Permute(a, i + 1, n);
swap(a[i], a[j]);
}
}
}

int main()
{
char str[21];
int len, count = 1;
int i, N;

cout << " PERMUTATION \n";
cout<<"\nEnter a string: ";
cin>>str;
len = strlen(str);

for (i = 0; i < N; i++)
{
count *= (i+1);
}


cout<<"\nThe number of permutations possible is: "<<count<<i;


Permute(str, 0, len);

return 0;
}

Last edited on Aug 16, 2019 at 3:32pm
Aug 16, 2019 at 3:49pm
for (i = 0; i < N; i++)
should be
for (i = 0; i < len; i++)


and


cout<<"\nThe number of permutations possible is: "<<count<<i;
should be
cout<<"\nThe number of permutations possible is: "<<count<<'\n';


Please use code tags.
Aug 16, 2019 at 3:51pm
why the count result of permutation is wrong?

stupidity?
Aug 16, 2019 at 3:56pm
PLEASE learn to use code tags, it makes reading and commenting on your code MUCH easier.

You can edit your post and add code tags:
http://www.cplusplus.com/articles/jEywvCM9/

Your are using an uninitialized variable (N) in main().

Visual Studio 2019 vomits up an error and halts compilation. Did you mean to assign the length of your C-string to N or use the variable len in your for loop?

cout<<"\nThe number of permutations possible is: "<<count<<i;

This line's output ^^^ will be deceptive, it concatenates two different variables so it looks like one bigger number.

Using "hello" as the input string you get
The number of permutations possible is: 1205

It should be something like?:
The number of permutations possible is: 120 5
Topic archived. No new replies allowed.