3

I am trying to make a C++ program for the following formula:

enter image description here

I made the choose part of the function:

#include <iostream>
#include <fstream>
using namespace std;


int choose();
void binomialdistribution();

int main(){
  choose();
  binomialdistribution();
}

int choose() {
  double n = 3;
  double k = 0;
  double i;
  double b;
  double value;
  while (k <= n){
    if (0 == k || n == k) {
      return 1;
    }
    if (k > n) {
      return 0;
    }
    if (k > (n - k)) {
      k = n - k;
    }
    if (1 == k) {
      return n;
    }
    b = 1;
    for (i = 1; i <= k; ++i) {
      b *= (n - (k - i));
      if (b < 0){
          return -1;
      }
      b /= i;
    }
    return b;
    cout << k;
    k++;
    binomialdistribution();
  }
}

void binomialdistribution(){
  cout << choose();
}

My output for k is blank and my choose() output is 0000000000000000000000

Any help will be appreciated

GSPdibbler
  • 405
  • 1
  • 9
  • 22
hockeynl
  • 151
  • 2
  • 9
  • It looks like you `return b` in your while loop before printing k, incrementing it, and calling `binomialdistribution()` again. – AndyG Apr 02 '14 at 21:16
  • `choose()` should return a double. – Zeta Apr 02 '14 at 21:16
  • FYI http://codereview.stackexchange.com/ is a good resource where you can get more general feedback. – TooTone Apr 02 '14 at 21:47
  • 1
    @TooTone You should also note that they generally expect the code in questions to be working when recommending that site. Although, since the question didn't ask about general improvements to the code (you can't really argue that "any help" classifies as that - that's just what people say), I don't think recommending it is particularly appropriate, if for no other reason than unsolicited recommendations are, by definition, spam, even if you're recommending another [se] site. – Bernhard Barker Apr 02 '14 at 22:19
  • 1
    @Dukeling thanks for your point about correctness; I think spam generally has the connotation that the post is for some personal gain, which mine was not. I'll consider what you say, but I'm not yet sure I'm going to stop posting links or making comments that I think are helpful (I believe others have sometimes benefited from comments I've made, and I know that I've benefited from others' comments). – TooTone Apr 02 '14 at 22:26
  • how large is your n can be? – shole Apr 03 '14 at 04:43

3 Answers3

0

From your program 'choose' is called from main and the return value is not given.Plus your 'k' would always be equal to zero because from the while loop.It first checks if k is equal to zero and returns 1.(Notice there is no variable to put the return value in.)then calls 'choose' from binomial distribution.(Notice you have not changed the value of k.It will always be ZERO!.).(Notice you called choose twice in the program)

kehindesalaam
  • 45
  • 1
  • 8
0

Try this

#include <cmath>
#include <stdio.h>

int main(){
    double p; 
    int k;
    int n;

    scanf("%d%d", &n, &k);
    scanf("%lf", &p);

    if (k > n) return 1;
    if (p > 1 || p < 0) return 1;

    double w = 1;   //neutral element of multiplication

    // n choose k part
    for (int i = n - k + 1; i <= n; ++i) w = w * i;
    for (int i = 1; i <= k; ++i) w = w / i;

    // p^k * (1-p)^(n-k) part
    w = w * pow(p, k) * pow(1.0 - p, n - k);

    printf("%lf\n", w);
    return 0;
}

ask if something is not clear.

Also remember to compile with -lm flag

GSPdibbler
  • 405
  • 1
  • 9
  • 22
  • 1
    What is printf, I never seen that before, or scanf for that matter – hockeynl Apr 02 '14 at 21:56
  • `printf` and `scanf` are C functions for printing and scanning data. You can use `cin >> n >> k >> p;` and `cout << w << endl;` as well, these are called streams and require including . – GSPdibbler Apr 02 '14 at 22:14
0

You can use temmplate programming that will allow you to write less boiler code

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>    // std::sort
using namespace std;


template <unsigned int N, unsigned int K>
struct Binomial
{
    enum
    {
        value=Binomial<N-1,K-1>::value + Binomial<N-1,K>::value
    };
};

template <unsigned int N>
struct Binomial<N,0>
{
    enum
    {
        value=1
    };
};

template <unsigned int N>
struct Binomial<N,N>
{
    enum
    {
        value=1
    };
};


int main(void)
{
    std::cout<<" Binomial<10,1> = ";
    std::cout<< Binomial<10,1>::value;
    std::cout<< std::endl;

    std::cout<<" Binomial<8,3> = ";
    std::cout<< Binomial<8,3>::value;
    std::cout<< std::endl;
}

Output:

Binomial<10,1> = 10

Binomial<8,3> = 56

Gabriel
  • 3,564
  • 1
  • 27
  • 49