C++ help: I need help with program 2: Count how many different possibilities are
ID: 3576738 • Letter: C
Question
C++ help: I need help with program 2:
Count how many different possibilities are there for choosing r out of n ele- ments with without repetition. Order is is not important. 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 Count how many different possibilities are there for choosing 2 out of 3 elements without repetition. Order is important 3. Validate that the phrase is in the proper format. 4. Store the four variables. (nr, order is or is not important, and with or without repetition 5. Output to console the type of count that the program will do: permutation or combination, with or without repetition. 6. Output to console the formula to compute the count and the result. Here, the output for the previous example: 1 This problem corresponds to a permutation without repetition 2 The general formula is n (n-r) 3 (3-2) 6 The answer is 4 There are 6 ways to choose 2 out of three elements.Explanation / Answer
#include<bits/stdc++.h>
using namespace std;
int fact(int n)
{int re=1;
for (int i = 1; i <=n; ++i)
{
re=re*i;
}
return re;
}
int main(int argc, char const *argv[])
{
cout<<"This problem corresponds to permutation without repetition ";
cout<<"The general formula is :n!/(n-r)! ";
cout<<"The answer is:";
int n,c,r,result;
n=3;
r=2;
result=fact(n)/fact(n-r);
cout<<n<<"!/("<<n<<"-"<<(r)<<")!="<<result;
cout<<"There are "<<result<<" ways to choose "<<r<<" out of "<<n<<" elements ";
cout<<"List of possiblities: ";
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(i!=j)
cout<<i+1<<" "<<j+1<<endl;
}
}
cout<<"Number of possiblities:"<<result<<endl;
cout<<"This count corresponds to the value computed using the formula ";
return 0;
}
==============================================================================
Output:
akshay@akshay-Inspiron-3537:~/Chegg$ g++ prob.cpp
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
This problem corresponds to permutation without repetition
The general formula is :n!/(n-r)!
The answer is:3!/(3-2)!=6There are 6 ways to choose 2 out of 3 elements
List of possiblities:
1 2
1 3
2 1
2 3
3 1
3 2
Number of possiblities:6
This count corresponds to the value computed using the formula
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.