Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Having trouble with my code, please help!

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Having trouble with my code, please help!

    Part of my program requires writing a function that determines the number of subsets of k items that can be chosen from a set of n distinct objects ("n choose k" or n!/((n-k)! n!)). The only errors I am getting are from the first line of the function:

    // n choose k: distinct subsets of k items chosen from n items
         public static int choose (int n, int k) {
          if (k <= 0)
            return 1;
          else
            return choose(n--, k--) * (n/k);
        }

    Any help that I can get to find out what is wrong with this would be extremely helpful. Thanks!


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Having trouble with my code, please help!

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    What do you mean, "find out what is wrong with it?" What does the error say? Post the entire error message if you don't understand it and perhaps we can help you with understanding it. Otherwise, you haven't given us enough information to help you. Post something we can work with.

  3. #3
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Having trouble with my code, please help!

    By the way, I would advice you not to use the "--" operator the way you do. It might easily confuse you since its not too obvious to a beginner what it actually does.
    Your line:
    return choose(n--, k--) * (n/k);
    is equal to:
    return choose(n, k) * ((n - 1) / (k - 1));
    and I doubt this is what you want because it will end in infinite recursion.

Similar Threads

  1. Having trouble with code for my game
    By atilla in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 30th, 2014, 04:45 AM
  2. having trouble with my code
    By JavaNewbie12 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 29th, 2014, 02:34 AM
  3. Trouble Porting my Java File Reading Code to Android Code
    By Gravity Games in forum Android Development
    Replies: 0
    Last Post: December 6th, 2012, 04:38 PM
  4. Having Trouble Modify the code!!
    By jehov86 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 2nd, 2012, 09:42 AM
  5. Trouble writing some code...help?
    By bChEos in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: February 7th, 2010, 08:54 PM