Combinations

This is a discussion on Combinations within the C Programming forums, part of the General Programming Boards category; Hi , I have to write a program that calculates the number of possible combinations k of n objects. It ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    46

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

  2. #2
    not-a-geek
    Join Date
    Apr 2004
    Posts
    210
    I'm pretty sure this is not what you want:

    Code:
      	if(n > k > 0){
    This evaluates to ...

    Code:
      if ((n > k) > 0) {
    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)) {
    main() { int O[!0<<~-!0]; (!0<<!0)[O]+= ~0 +~(!0|!0<<!0); printf("a function calling "); }

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    46
    Thanks , I made that change to my code, but it still returns a zero everytime

  4. #4
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    169
    You're assigning k and n to 0 in the if statement try:

    if ((k == 0) || (n == k)){

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    46
    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

  6. #6
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    56 is correct
    :wq

  7. #7
    ---
    Join Date
    May 2004
    Posts
    1,379
    >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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating combinations of array values - recursion??
    By johndirect in forum C Programming
    Replies: 2
    Last Post: 11-20-2008, 12:49 PM
  2. computing the number of combinations
    By clover in forum C Programming
    Replies: 34
    Last Post: 06-06-2004, 02:12 PM
  3. Grabbing Ctrl and Alt key combinations
    By scorpio_IITR in forum Linux Programming
    Replies: 0
    Last Post: 04-12-2004, 04:01 AM
  4. Combinations, lotto, 6/49
    By Robert in forum C++ Programming
    Replies: 8
    Last Post: 12-06-2002, 07:27 PM
  5. Combinations
    By GaPe in forum C Programming
    Replies: 16
    Last Post: 01-09-2002, 05:38 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21