-3

Okay, so I have to create a nCr program.

I need to allow the user to input the amount of nCr values they wish to calculate ( between 2 and 12 inclusive). Then input the n and r values accordingly, making sure n is greater than r (n > r) Afterwards i need to calculate and store the nCr value in an array and output the values

I wish to output the values in the format of a 2d array in the form of:

n | c | nCr

(without the grids)

So far my code is as follows:

    public class nCr {
     public static void main (String[] args) {
            int nCrValues;

            System.out.println("Enter amount of nCr values you wish to calculate: ");
            nCrValues = input();

            while ((nCrValues < 2) || (nCrValues > 10)){
              System.out.println("Must be between 2 and 12 inclusive");
              nCrValues = input();
            }//END while
           values(nCrValues);
              }//END main

         public static void values(int nCrValues){

             //I know I need a loop to nCrValues to input 'n' and 'r' as an array, not sure how

             int n, r;

             System.out.println("Enter n value: ");
             n = input();
             System.out.println("Enter r value: ");
             r = input();

             calculatenCr(n,r);
    }//END values

     public static int calculatenCr(int n, int r){
      int nCr;

      nCr = fact(n) / (fact(n-r) * fact(t));
      //store nCr values in array
    }//END calculatenCr

   public static int fact(int num){
      int count;
      int factor = 1;

      for (count = 1; count <= num; count++){
       factor *= count;
      }//END for
     return factor;
    }//END fact

     public static int input(){
      int input;

      Scanner sc = new Scanner(System.in);
      input = sc.nextInt();

       while (input < 0){
        System.out.println("Must be a positive number.")'
        input = sc.nextInt();
       }//END while
      return input;
     }//END input
}//END CLASS

I need a method that outputs the arrays int the form

n | c | nCr |

how do i go by that and is all my coding correct so far?

Thank you very much

kaqqao
  • 12,984
  • 10
  • 64
  • 118
SHX
  • 1
  • 1
  • 1
    Questions on SO are never urgent. You can't force us to hurry up. – node_modules May 01 '17 at 10:02
  • but how do i input x amount of 'n' and 'r' as well as checking that n > r ? – SHX May 01 '17 at 10:07
  • @GhostCat I think you jumped the gun on marking as duplicate (like mods here often do). The core of OP's question is something else, it seems. – kaqqao May 01 '17 at 10:08
  • @kaqqao correct, i want to input x amount of nCr values to calculate. From there input 'n' and 'r' according to what my 'x' was (eg, I want 4 nCr values, i have to input 'n' and 'r' 4 times) From there Ill check if n > r and then calculate the actual nCr value. I store the nCr value and print 'n', 'r' and 'nCr' all in an array – SHX May 01 '17 at 10:11
  • @kaqqao Just for the record: A) I am not a moderator ... you "just" need a gold badge to close to dups for the tag you got the badge B) the question is low quality and would actually deserve more downvotes and close requests ... it basically other people to do the rest of the homework; and "nice" to see that you just went forward to do that. – GhostCat May 01 '17 at 12:10
  • @GhostCat "mod" was a shorthand for "anyone with permissions to...". And agreed on everything, I just think this user showed genuine effort in writing 99% of the code (correctly) and deserved to be thrown a bone with the remaining 1% (I literally added maybe 3 lines) even if the question was poor. I'm not a fan of draconic treatment of newbies that gives SO the terrible reputation it has. – kaqqao May 01 '17 at 12:20

1 Answers1

0

Not fully understanding if you just want to print the number of combinations,not actual combinations themselves, but that's what your code indicates, so that's the way I completed it:

public class Ncr {
    public static void main (String[] args) {
        int nCrValues;

        System.out.println("Enter amount of nCr values you wish to calculate: ");
        nCrValues = input();

        while ((nCrValues < 2) || (nCrValues > 10)){
            System.out.println("Must be between 2 and 12 inclusive");
            nCrValues = input();
        }//END while
        for (int[] res : values(nCrValues)) {
            System.out.println(String.format("%d | %d | %d", res[0], res[1], res[2]));
        }
    }//END main

    public static int[][] values(int nCrValues){

        //I know I need a loop to nCrValues to input 'n' and 'r' as an array, not sure how
        int[][] res = new int[nCrValues][3];
        for (int i = 0; i < nCrValues; i++) {
            int n, r;

            System.out.println("Enter n value: ");
            n = input();
            System.out.println("Enter r value: ");
            r = input();

            res[i] = new int[]{n, r, calculatenCr(n, r)};
        }
        return res;
    }//END values

    public static int calculatenCr(int n, int r){
        return fact(n) / (fact(n-r) * fact(r));
    }//END calculatenCr

    public static int fact(int num){
        int count;
        int factor = 1;

        for (count = 1; count <= num; count++){
            factor *= count;
        }//END for
        return factor;
    }//END fact

    public static int input(){
        int input;

        Scanner sc = new Scanner(System.in);
        input = sc.nextInt();

        while (input < 0){
            System.out.println("Must be a positive number.");
            input = sc.nextInt();
        }//END while
        return input;
    }//END input
}//END CLASS
kaqqao
  • 12,984
  • 10
  • 64
  • 118