Can't get this program to compile

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • thatwhiteguy
    New Member
    • Nov 2007
    • 10

    #1

    Can't get this program to compile

    I am trying to get this program to compile and it is not working. I am new to c ++ and frustrated beyond belief. Please help.

    #include "stdafx.h"
    #include <iostream>



    using namespace std ;

    int binomial(int n, int k) ; // function prototype

    int main(int argc, char**argp)

    {

    int n, k ; // parameters for the binomial number
    int result ;

    cout << endl ;

    // read in n & k

    cout << "Enter n (positive integer) : " ;
    cin >> n ;

    cout << "Enter k (positive integer) : " ;
    cin >> k ;

    result = binomial (n,k) ;

    cout << "Binomial number " << n << "C" << k
    << " = " << result << endl ;

    system("PAUSE") ;
    return (0) ;
    }

    // *************** *************** *************** ***********

    binomial(int n, int k)

    /* Computes the binomial coefficient nCk */
    /*?????????????? ??????????????? ????????? */
    /* Inputs:???????? ??????????????? ??????? */
    /*??? n, k (integers)????? ???????????? ??*/
    /*?????????????? ??????????????? ????????? */
    /* Output:???????? ??????????????? ??????? */
    /*??? binomial coefficient nCk (integer) */

    {
    int numerator , denominator ;
    int i ; // needed to compute numerator & denominator

    if (n < k)
    {
    return("Error") ;
    }
    else
    {
    denominator = 1 ; // initial value

    for (i = 0 ; i <= k+1 ; i = i+1 )
    denominator = (i * denominator) ;

    numerator = (((n - k) + i) * numerator) ;

    return (numerator/denominator) ;
    } // else

    return 0;
    }
  • seforo
    New Member
    • Nov 2006
    • 60

    #2
    Obvious error:
    Firstly you forgot to put the return type of binomial; you should do this

    int binomial(int n, int k)
    {
    //your function goes here
    }

    Secondly, the return type of binomial is integer so this statement
    if (n < k)
    return("Error") ;
    }
    is wrong. you should return a number not "Error"

    Others then should be logical errors, but your function should now compile

    Comment

    • thatwhiteguy
      New Member
      • Nov 2007
      • 10

      #3
      I appreciate the help. It did work, and my program can now compile. I have got the program to where it returns a value when n is less than k. However if it is not I get an error from microsoft and the program shuts down. I have been going over it for hours and just can't seem to find the problem. Thanks

      int binomial (int n, int k)

      /* Computes the binomial coefficient nCk */
      /*?????????????? ??????????????? ????????? */
      /* Inputs:???????? ??????????????? ??????? */
      /*??? n, k (integers)????? ???????????? ??*/
      /*?????????????? ??????????????? ????????? */
      /* Output:???????? ??????????????? ??????? */
      /*??? binomial coefficient nCk (integer) */

      {
      int numerator , denominator ;
      int i ; // needed to compute numerator & denominator

      if (n < k)
      {
      return(0) ;
      }
      else
      {
      denominator = 1 ; // initial value
      numerator = 1 ;
      for (i = 0 ; i <= k+1 ; i = i+1 )
      {
      denominator = (i * denominator) ;

      numerator = (((n - k) + i) * numerator) ;
      }

      return (numerator/denominator) ;
      }// else}
      }

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        When i is 0 your denominator is 0. That's gonna be a long, long divide.

        Comment

        Working...