Permutations are concerned with order. There are many different orders that we c
ID: 3685495 • Letter: P
Question
Permutations are concerned with order. There are many different orders that we can select to line up the objects in a row. Suppose we have n different objects that can be combined and we select k of the objects. Then there are n!/(n-k)! possible orderings of k objects!
A more efficient way to compute permutations is to use the formula
P(n,k) = n(n-1)(n-2).....(n-k+2)(n-k+1).
This means the product of the k integers from n down to n-k+1. Use this formula to write a function perm( ) and a test driver for the function and confirm that your program works. The test driver should ask the user to input a positive number and should use a break command if the user enters a negative number.
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <cmath>
void perm ( char*a , int l , int y )
{
int i ;
if (l== y)
printf("%",a);
else
{
for(i= l ; i<=y ; i+l)
{
std::swap ((a+l) , (a+i));
perm (a, l+1,y);
std::swap ((a+l), (a+i)) ;
}
}
}
int main ()
{
char string [] ="hello" ;
int n = strlen (string);
perm ( s+0 , 0 , n-1);
return 0;
}
this is what i have so far,for some reason there are a few bugs in the program.
Explanation / Answer
#include <stdio>
#include <conio>
#include <string>
#include <algorithm>
#include <iostream>
#include <stdlib>
#include <cmath>
using namespace std;
void perm ( char*a , int l , int y )
{
int i ;
if (l== y)
printf("%s",a);
else
{
for(i= l ; i<=y ; i+l)
{
std::swap ((a+l) , (a+i));
perm (a, l+1,y);
std::swap ((a+l), (a+i)) ;
}
}
}
int main ()
{
char string [] ="hello" ;
int n = strlen (string);
perm ( string , 0 , n-1);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.