Ask your own question, for FREE!
Computer Science 17 Online
OpenStudy (anonymous):

I keep getting error on this code, i know it's the comb(k-1,r) giving me the error, but i don't know why. The code is suppose to generate next_Combination.

OpenStudy (anonymous):

#include <iostream> #include <algorithm> using namespace std; int arr[10]={0,1,2,3,4,5,6,7,8,9}; void switc(int a, int b){ int temp = arr[a]; arr[a]=arr[b]; arr[b]=temp; return; } void print(int size){ for (int i=0;i<size;i++) cout << arr[i] << " "; cout << endl; return; } long Factorial(long val) { long Result = 1; for(long i = 1; i <= val; i++) { Result *= i; } return Result; } long Combination(long N, long R) { return (Factorial(N)) / ((Factorial(N-R)) * Factorial(R)); } void comb(int n, int r){ int i; int k= Combination(n, r); if (k==0) print(r); else{ for (i=k-1;i>=0;i--){ switc(i,k-1); comb(k-1,r); switc(i,k-1); } } return; } int main(){ while (1){ int n, n1; cout<<"Enter a number: "; cin>>n>>n1; comb(n, n1); cout<<"The Factoral for the combination is "<<Combination(n,n1)<<endl; return 0; }}

OpenStudy (anonymous):

@e.mccormick

OpenStudy (e.mccormick):

So the problem is in the recursion?

OpenStudy (e.mccormick):

long Combination(long N, long R) { return (Factorial(N)) / ((Factorial(N-R)) * Factorial(R)); } That is returning a long and you are passing it to a function that takes an int.

OpenStudy (anonymous):

would it make a difference if i change long in to int?

OpenStudy (e.mccormick):

Might. At that point it would be called properly. But you have to watch your casting.

OpenStudy (e.mccormick):

Since they are the same on most systems, you might be able to just use ints all the way through.

OpenStudy (anonymous):

i changed it, but it didn't make a difference, it still gives me an error

OpenStudy (e.mccormick):

Or longs.

OpenStudy (e.mccormick):

What exact error is it giving?

OpenStudy (anonymous):

it wouldn't load the compiler.. it stops it before compiling and says that the program has stopped working

OpenStudy (e.mccormick):

So it is some sort of logic error? Hmm... At that point, you may need to toss in some print statements or break points and find what things are doing right before the program breaks and see what values are being passed around. Also, how deep is your recursion going? How many calls?

OpenStudy (e.mccormick):

If you have some insane depth you could hit a limit. http://stackoverflow.com/questions/2630054/does-c-limit-recursion-depth But that would be really deep to hit that unless something is causing it to chew up a ton of ram.

OpenStudy (anonymous):

idk how deep the recursion is, it does factorial, so it could get a little deep wen im calling it several times with in a loop

OpenStudy (anonymous):

What is sample input and output?

OpenStudy (anonymous):

the input number i used were 5 and 3, but it wouldn't compile it, but it stops it

OpenStudy (e.mccormick):

Yah, with those it should not get too deep. Hmm....

OpenStudy (anonymous):

it doesn't compile? or it doesn't run?

OpenStudy (anonymous):

it runs it up to Enter a number, then when i input two numbers, the program stops working

OpenStudy (anonymous):

what is the code supposed to do?

OpenStudy (anonymous):

permutation, combination, and arrangement.. the above code is suppose to be for combination..

OpenStudy (anonymous):

``` if (k==0) print(r); else{ for (i=k-1;i>=0;i--){ switc(i,k-1); comb(k-1,r); switc(i,k-1); } } ``` What is this supposed to do?

OpenStudy (anonymous):

it is suppose to calculate and print out the next_combination ex. 012 013 032 312 etc.. it amount of numbers depends on what the user inputs

OpenStudy (anonymous):

``` 120 / 12 362880 / 4320 0 / 0 ```

OpenStudy (anonymous):

It's wrong, that part of it.

OpenStudy (e.mccormick):

Hmmm.... yah... how large is that value getting! I wonder if it is exceeding 2147483647

OpenStudy (anonymous):

Why are you swapping around the array anyway, that?

OpenStudy (anonymous):

What should 5 3 as input provide?

OpenStudy (anonymous):

so the numbers would have different arrangements,

OpenStudy (anonymous):

5 & 3 is suppose to have 10 combination, each combination with 3 numbers like 123 231 312 etc

OpenStudy (anonymous):

What exactly are you doing though? Are you just doing permutations?

OpenStudy (anonymous):

i did the permutation already, and the permutation only requires one number since the formula is n! , but for the combination it is slightly different, it uses n!/r!(n!-r!), that's why it's getting a bit complicated for me

OpenStudy (anonymous):

this is the original question if it helps Write a C++ program to generate the followings: - Next/Previous permutation of n objects and calculate the total number of permutations of n objects. - Generate all the permutations of n digits. - Generate and calculate the number of combinations of n k by k digits. - Generate and calculate the number of arrangements of n k by k digits

OpenStudy (anonymous):

what is nest/prev permutation mean?

OpenStudy (anonymous):

something like this Enter a number: 3 0 1 2 1 0 2 0 2 1 2 0 1 2 1 0 1 2 0 Press any key to continue . . . The numbers that are generated are the next/prev permutation for 3

OpenStudy (anonymous):

Okay, so why are you even counting combinations then?

OpenStudy (anonymous):

Why aren't you printing the array?

OpenStudy (anonymous):

the combination works similarly as the permutation and i use the recursion to calculate the combination, it worked for the calculation, but when i try to print the next/prev combination with the same method i used for permutation, it didn't work

OpenStudy (anonymous):

You're not printing out an array.

OpenStudy (anonymous):

You're only printing out the number of combinations

OpenStudy (anonymous):

i thought it would print the combination too since it was able to do for the permutation. any suggestion what i should do

OpenStudy (anonymous):

First have it print out the actual array

OpenStudy (anonymous):

btw, this is the method i used for permutation, and it worked fine, but wen it comes to comb, not so much <code> void perm(int k,int size){ int i; if (k==0) print(size); else{ for (i=k-1;i>=0;i--){ switc(i,k-1); perm(k-1,size); switc(i,k-1); } } return; } <code/>

OpenStudy (anonymous):

okay can you give me sample output then, for combinations?

OpenStudy (anonymous):

Enter a number: 5 2 Press any key to continue . . .

OpenStudy (anonymous):

now when i put numbers less than 5 it works for example Enter a number: 4 3 0 1 2 0 1 3 0 3 2 3 1 2 The Factorial for the combination is 4 Press any key to continue . . .

OpenStudy (anonymous):

so does it have to do with the limit of the recursion.

OpenStudy (e.mccormick):

It would have to get insanely deep to do that, and I don’t think this is. But what about putting a few couts in there. Find out each step of the way what it is calling and returning. Use a pause so it only loops or recurses when you press a key. Then you can watch the values to see if they are doing something you did not expect.

OpenStudy (anonymous):

Thanks guys, i'll come back to it tomorrow

OpenStudy (anonymous):

The problem is you end up with 0/0

OpenStudy (anonymous):

it isn't recursion

OpenStudy (e.mccormick):

LOL! You even posted that before and I missed it. Was a bit distracted.... yah, 0/0 is a big deal in computers.

OpenStudy (anonymous):

you did put that earlier, i didn't notice it.

OpenStudy (anonymous):

so i need to have something like this ? long Combination(long N, long R) { if (((Factorial(N-R)) * Factorial(R)) != 0) return (Factorial(N)) / ((Factorial(N-R)) * Factorial(R)); else{ cout<<"Error! can't divide by zero. "; } }

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!