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.

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; }

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; }

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")

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 and other Homework Specialists are ready to help you