How JustAnswer Works:

  • Ask an Expert
    Experts are full of valuable knowledge and are ready to help with any question. Credentials confirmed by a Fortune 500 verification firm.
  • Get a Professional Answer
    Via email, text message, or notification as you wait on our site.
    Ask follow up questions if you need to.
  • 100% Satisfaction Guarantee
    Rate the answer you receive.

Ask Dr. Arthur Rubin Your Own Question

Dr. Arthur Rubin
Dr. Arthur Rubin, Professional w/Adv. Degree
Category: Homework
Satisfied Customers: 1263
Experience:  Ph.D. in Mathematics, 1978, from the California Institute of Technology, over 20 published papers
13714381
Type Your Homework Question Here...
Dr. Arthur Rubin is online now
A new question is answered every 9 seconds

permutations and combinations in c++

Resolved Question:

permutations and combinations in c++
Submitted: 11 months ago.
Category: Homework
Expert:  Dr. Arthur Rubin replied 11 months ago.

Dr. Arthur Rubin :

I'm not sure what you're asking. Could you be more clear?

Dr. Arthur Rubin :

Are you asking for a C++ program to generate permutations and combinations?

Customer:

i need to write a c++ program in how many ways can something drawn (oder matters permutation)

Customer:

(does not matter) combinations

Customer:

so i do not have to ask user for inputs, i have to display only in how many ways the task can be done when order matters and not

Dr. Arthur Rubin :

I think I need more answers in order to help you.
What is the input to the program (or routine)?

Customer:

the question is (how many ways 4 employees can be drawn if the order in which they are drawn matter) the department has 20 employees

Customer:

i have to write a c++ program to display in how many ways this is (a) part

Customer:

b) part the same thing but if the order in which they are drawn does not matter

Dr. Arthur Rubin :

OK, I think I know what you need. Give me a half hour or so to write the program.

Dr. Arthur Rubin :

You just need the number of ways, not a list of the possibilities, correct?

Customer:

correct

Customer:

let me send you the file that has a question so you can understand clearly

Customer:

ok ?!

Dr. Arthur Rubin :

OK

Dr. Arthur Rubin :

Do you know how to upload a file here, or to a file-sharing site?

Customer:

the file has two questions. one for permutations and combinations and other for probability

Customer:

how i can upload it here ?!

Dr. Arthur Rubin :

Do you see a row of icons?.

Customer:

yes

Dr. Arthur Rubin :

If you type a word, such as "here", and highlight it, the icon that looks like the link of a chain should become ungreyed. On my bar, it's just before the paperclip.

Dr. Arthur Rubin :

That should give you the option to upload a file, as well as to link to a web page, but there are only certain file formats that can be uploaded. Most "Word" (.doc, .docx) formats are allowed, as is .pdf. .txt is not allowed, but .zip is.

Customer:

Full Size Image

Customer:

can you see the image clearly ?!

Customer:

Full Size Image

Dr. Arthur Rubin :

OK, I'll supply the functions for you shortly.

Customer:

in how long should i check back ?!

Dr. Arthur Rubin :

Probably 1/2 hour.

Customer:

for permutations and combinations and probability, sounds good ?!

Customer:

anyways whenever you are done, i am here..

Dr. Arthur Rubin :

OK, here it is. You'll need to retype the tabs. Note the two alternative solutions to Binomial

Dr. Arthur Rubin :

/* The algorithm uses iteration to simulate recursion
P(N, X) = N * P(N-1, X-1);
P(N, 0) = 1;
*/

long Permutations(long N, long X)
{
long output = 1;
while (X > 0) {
output *= N;
N--;
X--;
}
return output;
}
/* The algorithm uses iteration to simulate recursion
C(N, X) = N * C(N-1, X-1) / X;
C(N, 0) = 1;
Note also that, under some circumstances, the program might
overflow while the ultimate answer is still in range.
*/


long Combinations (long N, long X)
{
long output = 1;
long i;


for (i = 1; i <= X; i++) { /* loop is not executed if X = 0 */
output *= N;
output /= i;
N--;
}
return output;
}

/* If
double pow(double P, long N);
is in the language description, the following should work.
*/

double Binomial(long N, double P, long X)
{
return Combinations (N,X) * power(P, X) * power(1.0-P, N-X);
}

/* Otherwise, and allowing for the case where Combination might overflow */

double Binomial(long N, double P, long X)
{
double output = 1.;
int i;
for (i = 1; i <= X; i++) { /* loop is not executed if X = 0 */
output *= N;
output /= i;
N--;
}
for (i=0; i<X; i++) {
output *= P;
}
for (i=X; i<N; i++) {
output *= 1.-P;
}
return output;
}


Dr. Arthur Rubin :

I've got to go....personal business. I'll be back within 90 minutes.

Customer:

ok, i will go to check it out, thank you :))

Customer:

i check it out, it throws an error that binomial function already has a body. i need your help to fix it when you are back

Dr. Arthur Rubin :

I'm back, for the moment, although my wife may come by and ask me to do things which probably shouldn't be done.

Dr. Arthur Rubin :

I think you need to select which Binomial you want to use. As I made a mistake in the first one, even if your version of C has the correct function, you probably should use the second; perhaps modified as follows:

Dr. Arthur Rubin :

double Binomial(long N, double P, long X)
{
double output = 1.;
double Q = 1.-P;
int i;
for (i = 1; i <= X; i++) { /* loop is not executed if X = 0 */
output *= N;
output /= i;
N--;
}
for (i=0; i<X; i++) {
output *= P;
}
for (i=X; i<N; i++) {
output *= Q;
}
return output;
}

Customer:

would you please copy the code and past it in cpp file so you can double check it compiles, because it still throws errors

Dr. Arthur Rubin :

OK, one moment. Try this corrected version, omitting the duplicate Binomial. I haven't done a compile-check yet, but perhaps it will work.

/* The algorithm uses iteration to simulate recursion
P(N, X) = N * P(N-1, X-1);
P(N, 0) = 1;
*/

long Permutations(long N, long X)
{
long output = 1;
while (X > 0) {
output *= N;
N--;
X--;
}
return output;
}
/* The algorithm uses iteration to simulate recursion
C(N, X) = N * C(N-1, X-1) / X;
C(N, 0) = 1;
Note also that, under some circumstances, the program might
overflow while the ultimate answer is still in range.
*/


long Combinations (long N, long X)
{
long output = 1;
long i;


for (i = 1; i <= X; i++) { /* loop is not executed if X = 0 */
output *= N;
output /= i;
N--;
}
return output;
}

//* B(N, P, X) = C(N,X) * P**X * (1.-P) ** (N-X) */

double Binomial(long N, double P, long X)
{
double output = 1.;
double Q = 1.-P;
int i;
for (i = 1; i <= X; i++) { /* loop is not executed if X = 0 */
output *= N;
output /= i;
N--;
}
for (i=0; i<X; i++) {
output *= P;
}
for (i=X; i<N; i++) {
output *= Q;
}
return output;
}



Customer:

it does not compile correctly again :((

Dr. Arthur Rubin :

OOPS, typo. Try this one.

Dr. Arthur Rubin :

/* The algorithm uses iteration to simulate recursion
P(N, X) = N * P(N-1, X-1);
P(N, 0) = 1;
*/

long Permutations(long N, long X)
{
long output = 1;
while (X > 0) {
output *= N;
N--;
X--;
}
return output;
}
/* The algorithm uses iteration to simulate recursion
C(N, X) = N * C(N-1, X-1) / X;
C(N, 0) = 1;
Note also that, under some circumstances, the program might
overflow while the ultimate answer is still in range.
*/


long Combinations (long N, long X)
{
long output = 1;
long i;


for (i = 1; i <= X; i++) { /* loop is not executed if X = 0 */
output *= N;
output /= i;
N--;
}
return output;
}

/* B(N, P, X) = C(N,X) * P**X * (1.-P) ** (N-X) */

double Binomial(long N, double P, long X)
{
double output = 1.;
double Q = 1.-P;
int i;
for (i = 1; i <= X; i++) { /* loop is not executed if X = 0 */
output *= N;
output /= i;
N--;
}
for (i=0; i<X; i++) {
output *= P;
}
for (i=X; i<N; i++) {
output *= Q;
}
return output;
}

The previous version had a syntax error, that I should have caught myself. Please report any error message (other than "syntax error") in detail.

Dr. Arthur Rubin :

One more error. In "Binomial", "int i;" should be "long i;", even though the odds of the program completing without overflow are small if X exceeds MAX_INT.

Customer:

the error keeps throw fatal error LNK 1120: unresolved externals

Customer:

please try it in cpp file with your compile

Dr. Arthur Rubin :

Ah, that might help. Does your compiler/linker require a "main" in order to work properly. It certainly won't run without a main, but some combinations produce a link error.

Customer:

i think so

Dr. Arthur Rubin :

Try adding a dummy main program, then, and see if you can get the linker to produce the name of (an) unresolved external.

Customer:

i added int main() {

Customer:

return 0; } it compile successfully but it did not display any result

Customer:

hello are you here

Dr. Arthur Rubin :

It shouldn't display any result; you have to use an output statement in the main program.

Dr. Arthur Rubin :

Something like:

Dr. Arthur Rubin :

#include <stdio.h>
int main(int argc, char * * argv)
{
printf("Number of permutations of %d members taken %d at a time is %ld\n",20,4,Permutations(20,4));
printf("Number of combinations of %d members taken %d at a time is %ld\n",20,4,Combinations(20,4));
return 0;
}

Customer:

i does not compile again, it throws an error that main function cannot be overloaded

Dr. Arthur Rubin :

Replace your existing main with that, then. I would need to know your particular compiler to determine what "main" is supposed to be. What I wrote is the "C Standard" main.

Customer:

it compiles successfully, just one more thing how i can display the probability as well

Dr. Arthur Rubin :

Problem 2 would be

Dr. Arthur Rubin :

printf("Probably of at most one defective bomb is %lg\n", Binomial(50,.06,0) + Binomial(50,.06,1))

Dr. Arthur Rubin :

(If the drawing is done with replacement. If it's done without replacement, then you don't have the appropriate functions, yet.)

Dr. Arthur Rubin :

Problem 3 would be:

Dr. Arthur Rubin :

printf("Probably of at most two defective grenades is %lg\n",
Binomial(15,.075,0) + Binomial(15,.075,1) + Binomial(15,.075,2))

Dr. Arthur Rubin :

Sorry, that should be "%g" or "%f" rather than "%lg"; I forgot that "float" is never passed as a generic argument.

Dr. Arthur Rubin :

Sorry again; I misread problem 2. It should be Binomial(10,.06,0) + Binomial(10,.06,1) if done with replacement.

Dr. Arthur Rubin :

If done without replacement, it would be (after promoting enough argument to "real")

Dr. Arthur Rubin :

(Combinations(47,10)*Combinations(3,0) + Combinations(47,9)*Combinations(3,1))/(double)Combinations(50,6)

Dr. Arthur Rubin :

OOPS, that wasn't correct, either it should be (Combinations(47,10)*Combinations(3,0) + Combinations(47,9)*Combinations(3,1))/(double)Combinations(50,10)

Customer:

that mess up my head would you please clarify what was not correct

Dr. Arthur Rubin :

The number for problem 2 is Binomial(10,.06,0) + Binomial(10,.06,1) if the sampling is done with replacement (returning each sample after testing)

Dr. Arthur Rubin :

If the sampling is done without replacement, it should be:
(Combinations(47,10)*Combinations(3,0) + Combinations(47,9)*Combinations(3,1))/(double)Combinations(50,10)

Dr. Arthur Rubin :

Or, if you prefer,
(Combinations(40,3)*Combinations(10,0) + Combinations(40,2)*Combinations(10,1))/(double)Combinations(50,3)

Dr. Arthur Rubin :

These have to do with the hypergeometric distribution, rather than the binomial distribution. But, unless you're in a probability course, you shouldn't know that yet.

Customer:

yah our professor mentioned that today, i mean hypergeometric distribution and he said that we do not have to worry about

Customer:

only binomial distribution

Customer:

so should i keep the code as it is ?!

Dr. Arthur Rubin :

Probably, although the answer is technically not correct. Problem 2 told you that 3 of the 50 (or 6%) were defective; if you are to use the "6%", you must use the binomial distribution, even if inappropriate.

Customer:

can i ask you to check a function i wrote for another program ?! i am sorry if i should not but only i need someone to check it out

Dr. Arthur Rubin :

You probably should ask as a separate question. You can mark it as "for Arthur Rubin" or "for arubin9" (my internal tag) if you want to reserve it for me.

Customer:

anyways, i will try to figure it out. thank you so much for your awesome work :)

Dr. Arthur Rubin, Professional w/Adv. Degree
Category: Homework
Satisfied Customers: 1263
Experience: Ph.D. in Mathematics, 1978, from the California Institute of Technology, over 20 published papers
Dr. Arthur Rubin and 6 other Homework Specialists are ready to help you

JustAnswer in the News:

 
 
 
Ask-a-doc Web sites: If you've got a quick question, you can try to get an answer from sites that say they have various specialists on hand to give quick answers... Justanswer.com.
JustAnswer.com...has seen a spike since October in legal questions from readers about layoffs, unemployment and severance.
Web sites like justanswer.com/legal
...leave nothing to chance.
Traffic on JustAnswer rose 14 percent...and had nearly 400,000 page views in 30 days...inquiries related to stress, high blood pressure, drinking and heart pain jumped 33 percent.
Tory Johnson, GMA Workplace Contributor, discusses work-from-home jobs, such as JustAnswer in which verified Experts answer people’s questions.
I will tell you that...the things you have to go through to be an Expert are quite rigorous.
 
 
 

What Customers are Saying:

 
 
 
  • Wonderful service, prompt, efficient, and accurate. Couldn't have asked for more. I cannot thank you enough for your help. Mary C. Freshfield, Liverpool, UK
< Last | Next >
  • Wonderful service, prompt, efficient, and accurate. Couldn't have asked for more. I cannot thank you enough for your help. Mary C. Freshfield, Liverpool, UK
  • This expert is wonderful. They truly know what they are talking about, and they actually care about you. They really helped put my nerves at ease. Thank you so much!!!! Alex Los Angeles, CA
  • Thank you for all your help. It is nice to know that this service is here for people like myself, who need answers fast and are not sure who to consult. GP Hesperia, CA
  • I couldn't be more satisfied! This is the site I will always come to when I need a second opinion. Justin Kernersville, NC
  • Just let me say that this encounter has been entirely professional and most helpful. I liked that I could ask additional questions and get answered in a very short turn around. Esther Woodstock, NY
  • Thank you so much for taking your time and knowledge to support my concerns. Not only did you answer my questions, you even took it a step further with replying with more pertinent information I needed to know. Robin Elkton, Maryland
  • He answered my question promptly and gave me accurate, detailed information. If all of your experts are half as good, you have a great thing going here. Diane Dallas, TX
 
 
 

Meet The Experts:

 
 
 
  • Manal Elkhoshkhany

    Tutor

    Satisfied Customers:

    4520
    More than 5000 online tutoring sessions.
< Last | Next >
  • http://ww2.justanswer.com/uploads/BU/BusinessTutor/2012-2-2_115741_Kouki2.64x64.jpg Manal Elkhoshkhany's Avatar

    Manal Elkhoshkhany

    Tutor

    Satisfied Customers:

    4520
    More than 5000 online tutoring sessions.
  • http://ww2.justanswer.com/uploads/LI/lindaus/2012-6-10_04811_IMG20120609164157.64x64.jpg Linda_us's Avatar

    Linda_us

    Finance, Accounts & Homework Tutor

    Satisfied Customers:

    3121
    Post Graduate Diploma in Management (MBA)
  • http://ww2.justanswer.com/uploads/ComputersGuru/2010-02-13_051118_Photo41.JPG LogicPro's Avatar

    LogicPro

    Engineer

    Satisfied Customers:

    3035
    Expert in Java C++ C C# VB Javascript Design SQL HTML
  • http://ww2.justanswer.com/uploads/lanis/2009-4-1_233717_phput9xef_c1pm.jpg Lani S.'s Avatar

    Lani S.

    Tutor

    Satisfied Customers:

    2457
    Registered Nurse, Internet Researcher, Private Tutor
  • http://ww2.justanswer.com/uploads/chooser77/2009-08-18_162025_Chris.jpg Chris M.'s Avatar

    Chris M.

    M.S.W. Social Work

    Satisfied Customers:

    2341
    Master's Degree, strong math and writing skills, experience in one-on-one tutoring (college English)
  • http://ww2.justanswer.com/uploads/JawaadAhmed/2009-6-27_12137_SIs_SHadi.jpg F. Naz's Avatar

    F. Naz

    Chartered Accountant

    Satisfied Customers:

    1975
    Experience with chartered accountancy
  • http://ww2.justanswer.com/uploads/JK/jkcpa/2011-1-16_182614_jkcpa.64x64.jpg Bizhelp's Avatar

    Bizhelp

    CPA

    Satisfied Customers:

    1873
    Bachelors Degree and CPA with Accounting work experience