Make a program able to count permutations and combinations. Follow these steps:
ID: 3574920 • Letter: M
Question
Make a program able to count permutations and combinations. Follow these steps: Read and make sure you understand the content presented in https://www.mathsisfun com/combinatorics/combinations-permutations.html Read from file input prog1.txt a phrase that describes the counting to be performed. The phrase should have the following format: Words in bold are static which means that the user must type these exact words. Words in italic are the variables of the particular count. This is an example of the input: Validate that the phrase is in the proper format. Store the four variables (n, r, order is or is not important, and with or without repetition). Output to console the type of count that the program will do: permutation or combination, with or without repetition. Output to console the formula to compute the count and the result. Here, the output for the previous example:Explanation / Answer
Your Program :
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
int main ()
{
string line,s1,s2;
ifstream infile;
infile.open("C:\Users\Kanubhai\Desktop\input.txt");
while(!(infile.EOF)) // To get you all the lines.
{
int r,n;
std::istringstream iss(line);
if(!(iss >>r >> n)){break;} //error
else{
int a,b;
a=r;b=n;
if((iss >> s1 >> s2) && (s1=="with") && (s2 != "not"))//permutation with repetition
{
while(a>0){ //calculating n^r
b = b*b;
a--;
}
cout << "The Problem corresponds to Permutation with repetition" << endl;
cout << "General Formula is : n^r"<<endl;
cout << "Answer is :" << n << "^" << r << "=" << b <<endl;
cout << "There are " << " " << a << " " << "to choose " << r << " " << "out of " << n << " " << "elements" << endl;
}
else if((iss >> s1 >> s2) && (s1=="without") && (s2 != "not"))//permutation without repetition
{
int t=0,fact1=0,fact2=0,k,ans=0;
t=b-1;
fact1 = b;
while (t>0){ //calculating n!
fact1 = fact1 * t;
t--;
}
fact2 = n-r;
k = fact2-1;
while(k>0){ //calculating (n-r)!
fact2 = fact2 * k;
k--;
}
ans = fact1/fact2;
cout << "The Problem corresponds to Permutation without repetition" << endl;
cout << "General Formula is : n!/(n-r)!"<<endl;
cout << "Answer is :" << n << "!/" << "("<<n<<"-"<<r <<")"<<"!" <<" " << "=" << ans <<endl;
cout << "There are " << " " << ans << " " << "to choose " << r << " " << "out of " << n << " " << "elements" << endl;
}
else if((iss >> s1 >> s2) && (s1=="with") && (s2 == "not"))//Combinatin with repetition
{
int i,j,t,x,y,fact1,fact2,fact3,ans;
i = r+n-1;
j = n-1;
t=r-1;
fact1 = r;
while (t>0){ //calculating r!
fact1 = fact1 * t;
t--;
}
x=i-1;
fact2 = i;
while (x>0){ //calculating (r+n-1)!
fact2 = fact2 * x;
x--;
}
y=j-1;
fact3 = j;
while (y>0){ //calculating (n-1)!
fact1 = fact1 * y;
y--;
}
ans = x/(y*t);
cout << "The Problem corresponds to Combination with repetition" << endl;
cout << "General Formula is : (r+n-1)!/r!(n-1)!"<<endl;
cout << "Answer is :" << "(" << r << "+" << n << "-1" << ")" << "/" << r << "!" << " " << "(" << n << "-1)!" << "=" << ans <<endl;
cout << "There are " << " " << ans << " " << "to choose " << r << " " << "out of " << n << " " << "elements" << endl;
}
else if((iss >> s1 >> s2) && (s1=="without") && (s2 == "not"))//Combinatin without repetition
{
int t=0,fact1=0,fact2=0,k,ans=0,j,x;
t=b-1;
fact1 = b;
while (t>0){ //calculating n!
fact1 = fact1 * t;
t--;
}
fact2 = n-r;
k = fact2-1;
while(k>0){ //calculating (n-r)!
fact2 = fact2 * k;
k--;
}
j=r-1;
x=r;
while(j>0){ //calculating r!
x = x * j;
j--;
}
ans = fact1/(fact2*x);
cout << "The Problem corresponds to Permutation without repetition" << endl;
cout << "General Formula is : n!/r!(n-r)!"<<endl;
cout << "Answer is :" << n << "!/" << "("<<n<<"-"<<r <<")"<<"!"<<r<<"!" <<" " << "=" << ans <<endl;
cout << "There are " << " " << ans << " " << "to choose " << r << " " << "out of " << n << " " << "elements" << endl;
}
}
infile.close();
system ("pause");
return 0;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.