-6

why is = n! / ((n-k)!*k!) not printing?

Also will this code solve the problem below?

stuck.

"The number of combinations of n things taken k at a time as an integer"

A little more clarification: "For example, the combinations of four items a,b,c,d taken three at a time are abc, abd, acd, and bcd. In other words, there are a total of four different combinations of four things "taken three at a time"."

#include <stdio.h>
#include <math.h>   

int main (void)
{
    int z = 0, n_in, k_in, k = 0, n = 0, result, nfr = 0, kfr = 0;

    do
    {
        printf("Enter the number of items in the list (n):");
        scanf("%d*c", &n_in);

        if (n_in>1 && n_in<11)
        {

            printf("Enter the number of items to choose (k)");
            scanf("%d*c", &k_in);

            if (k_in>0 && k_in<5)
            {

                if (k_in <= n_in)
                {

                    k_in = k;
                    n_in = n;


                    result = n! / ((n-k)!*k!);


                    z = 1;

                }

                else
                    printf("?Please Try again k must be less than n \n");
            }

            else
                printf("?Invalid input: Number must be between 1 and 4 \n");

        }

        else
            printf("?Invalid input: Number must be between 1 and 10 \n");


    } while (z == 0);

    result = (nfr / (nfr * kfr));
    printf("k value = %d n value = %d the result is %d", nfr, kfr, result);


    return 0;
}   
RandomNumberFun
  • 638
  • 2
  • 8
  • 25
  • do you know what a ! does in C ? – Bartlomiej Lewandowski Feb 09 '13 at 22:51
  • 5
    `!` doesn't do what you think it does. Make your own factorial function. – Blender Feb 09 '13 at 22:51
  • 3
    Why don't you learn the language before trying to do stuff? –  Feb 09 '13 at 22:53
  • 1
    That will not compile. Start at the beginning. No need to worry about printing before the current code can even be executed. –  Feb 09 '13 at 22:54
  • Doesn't 'result' get thrown away, and overwritten with another (division by zero!) calculation? – JasonD Feb 09 '13 at 22:58
  • Note that there are better algorithms for computing the number of combinations than the mathematically succint `n!/((n-k)!k!)`, as computing factorials will cause integer overflow very quickly. – chepner Feb 09 '13 at 23:05
  • 2
    This is your third question on the general topic, the others being [How could I count a sub-range of 1-4 in permutations of a 1-10 digit range](http://stackoverflow.com/questions/14784602/) and [A simple expression for counting combinations in a range of numbers](http://stackoverflow.com/questions/14783910/). This question is worrying; you're posting non-compiling code instead of an SSCCE ([Short, Self-Contained, Correct Example](http://sscce.org/)) which you can be helped with. You should note that your code does not compile; 'not working' normally denotes code that compiles but fails to run. – Jonathan Leffler Feb 09 '13 at 23:38

2 Answers2

2

This line:

result = n! / ((n-k)!*k!);

...is not valid C code. ! in C means "not".

You will need to provide a factorial function so that you can call:

  result = factorial(n) / (factorial(n-k) * factorial(k));
Community
  • 1
  • 1
johnsyweb
  • 136,902
  • 23
  • 188
  • 247
0

! is not the NOT operator in C. Use this factorial function instead.

int factorial(int n)
{
  return (n == 1 || n == 0) ? 1 : factorial(n - 1) * n;
}

So your calculation would be:

result = factorial(n) / (factorial(n-k)*factorial(k));

There are probably faster ways to do it, but this is readable.

Also, this line

result = (nfr / (nfr * kfr));

Does not make any sense to me, since both nfr and kfr are zero, but I guess you wanted to get the code to compile, before completing the logic.

EDIT: complete code should look like this:

#include <stdio.h>
#include <math.h>

int factorial(int n)
{
    return (n == 1 || n == 0) ? 1 : factorial(n - 1) * n;
}

int main (void)
{
    int z = 0, n_in, k_in, k = 0, n = 0, result, nfr = 0, kfr = 0;
    do
    {
        printf("Enter the number of items in the list (n):");
        scanf("%d*c", &n_in);

        if (n_in>1 && n_in<11)
        {
            printf("Enter the number of items to choose (k)");
            scanf("%d*c", &k_in);
            if (k_in>0 && k_in<5)
            {
                if (k_in <= n_in)
                {
                    k_in = k;
                    n_in = n;
                    result = factorial(n) / (factorial(n-k)*factorial(k));
                    //result = n! / ((n-k)!*k!);
                    z = 1;
                }
                else
                    printf("?Please Try again k must be less than n \n");
            }
            else
                printf("?Invalid input: Number must be between 1 and 4 \n");
        }
        else
            printf("?Invalid input: Number must be between 1 and 10 \n");
    } while (z == 0);
    //result = (nfr / (nfr * kfr));
    printf("k value = %d n value = %d the result is %d\n", nfr, kfr, result);
    return 0;
}

Output:

~/so$ gcc test.cc
~/so$ ./a.out 
Enter the number of items in the list (n):3
Enter the number of items to choose (k)2
k value = 0 n value = 0 the result is 1
~/so$
user000001
  • 32,226
  • 12
  • 81
  • 108
  • I am getting an error ` Use of out-of-scope declaration of 'factorial'` when I add the result = factorial(n) / (factorial(n-k) * factorial(k)); in place of my result. – RandomNumberFun Feb 09 '13 at 23:09
  • 1
    @user1787331 Updated answer. Maybe you placed the function in the wrong place? – user000001 Feb 09 '13 at 23:13
  • @user1787331: You really need to learn [the C programming language](http://www.amazon.co.uk/exec/obidos/ASIN/0131103628/johnsydotorg-21) in order to progress from here. StackOverflow cannot teach you to program. – johnsyweb Feb 09 '13 at 23:34
  • @user000001 Thank you so much! This helped me solve my problem! – RandomNumberFun Feb 12 '13 at 07:29