-
Combinations
Hi , I have to write a program that calculates the number of possible combinations k of n objects.
It has to be a recursive program.
It compiles and runs, but while debugging while I type in some numbers it returns 0 everytime
Thanks for any help
Code:
#include <stdio.h>
int possibleCombi(int n,int k);
int main (void)
{
int n,k,combinations;
printf("type in n , number of total objects");
scanf("%d",&n);
printf("type in k , numbers drawn");
scanf("%d",&k);
combinations = possibleCombi(n,k);
printf("%d",combinations);
}
int possibleCombi(int n,int k)
{
if ((k = 0) || (n = k)){
return 1;
}
if(n > k > 0){
return (possibleCombi(n - 1,k) + possibleCombi(n - 1,k - 1));
}
}
-
I'm pretty sure this is not what you want:
This evaluates to ...
which means: if the boolean result of n > k (either "true" or "false") is > 0 do....
You probably want something like
Code:
if ((n > k) && (k > 0)) {
-
Thanks , I made that change to my code, but it still returns a zero everytime :confused:
-
You're assigning k and n to 0 in the if statement try:
if ((k == 0) || (n == k)){
-
Thanks , it works now , except that the answer isn't right I think.
Shouldn't the answer for example with n = 8 and k = 3 be 512.
Instead it returns then 56 :confused:
-
-
>Shouldn't the answer for example with n = 8 and k = 3 be 512
you must be a little confused.
that would be
8^3 = 512 (not XOR)
8*8*8 = 512