(A) Problem Statement :
Write a program to generate all possible
permutations with out duplicates of given set.
(B) Input and Output :
This problem will take n no of numbers as set and gives
all possible permutations all n numbers with out duplicates.
(i) enter no of digits: 3
Enter digit: 1
Enter digit: 2
Enter digit: 3
Permutations are :
1 2 3
1 3 2
2 3 1
2 1 3
3 1 2
3 2 1
(ii) enter no of digits: 3
Enter digit: 1
Enter digit: 2
Enter digit: 1
Permutations are :
1 2 1
1 1 2
2 1 1
(C) Examples:
(i) if the set contains 4,5,6 elements the permutations are
different arrangements of set elements.
4 5 6
4 6 5
5 4 6
5 6 4
6 4 5
6 5 4
(ii) if the set contains 4,5,4 elements then permutations are
4 5 4
4 4 5
5 4 4
(D)Algorithm :
Step 1: Start
Step 2: Read the no of elements of set into n and
the elements into array a. initialize k=p=0.
Step 3: repeat the following steps until p value is 2
power n.
Step 4:if k=n then check for duplicates and if there
is no duplicate then print array.
Step 5:otherwise for I value k to n repeat steps 6to8
Step 6:exchange the array element I and k.
Step 7: goto step 3 with k value increased by 1.
Step 8: interchange array elements at k and I .
Step 9: Stop
(E) Program:
#include
#include
int p=0;
void perm(int a[],int k,int n) //finding permutations
{
int i,t,j,c;
int x[25][15];
if(k==n) // output the permutations
{
for(i=0;i
{
c=0;
for(j=1;j<=n;j++) //duplicates checking
{
if(x[i][j]==a[j])
{
c++;
}
}
if(c==n) break;
}
if(c!=n)
{
printf("\n");
for(i=1;i<=n;i++)
{ //Storing permutations in x
x[p][i]=a[i];
printf("\t%d",a[i]);
}
p=p+1;
}
}
else //a[k:n] has more than one permutation.
{ //Generate these recursively.
for(i=k;i<=n;i++)
{
t=a[k];
a[k]=a[i];
a[i]=t;
perm(a,k+1,n); //all permutations of a[k+1:n]
t=a[k];
a[k]=a[i];
a[i]=t;
}
}
}
void main()
{
int a[5],i,n;
clrscr(); //read the no of elements in set
printf("\n enter no of elements:");
scanf("%d",&n);
for(i=1;i<=n;i++) //read the elements of set
{
printf("\n enter the element:");
scanf("%d",&a[i]);
}
perm(a,1,n); // call the prem function
getch();
}
(F) Testing:
S.NO Aspect to be Tested Expected Result
1 Set of elements ={1,2}
1 2
2 1
Successful
2 Set of elements = {1,1} 1 1
(Duplicates are eliminated)
3 Set of elements = {1,2,3,4,5} Abnormal termination
(due to out of range)
(G) Limitations :
(i) this program can take maximum of 4 numbers only.
(ii) this program can give permutations of 4 numbers only
since the array size is 25 rows.
No comments:
Post a Comment