You are here:

C++/Binomial Numbers Programming Problem

Advertisement


Question
Hello,
You helped someone with the exact same problem last year and it was a great help to me as well, so I say thank you.  However, I'm still getting 1 error and I can't seem to figure this out and was hoping you could help me.

: error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

This is the only error in the program @ line 64.

// Module4ProgrammingExercise.cpp : Defines the entry point for the console application.
//
/******************************************************/
/* File: Module4ProgrammingExercise.cpp    */
/* Created by: Kimberly */
/* Date: November 23,2010      */
/*                          */
/* Program to compute binomial coefficien   */
/*                          */
/* Inputs: (ke             */
/*   Two positive integers (n & k)       */
/*                             */
/* Output:        */
/*   Corresponding binomial coefficient (nCk)  */
/*          */
/* Algorithm: see attached description  */
/************************************************/

#include "stdafx.h"

#include <iostream>

using namespace std ;

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


int main()
{
  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 ;

  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 (0) ;
  }
  else
  {
     denominator = 1 ; // initial value

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

     numerator = 1 ;

     for (i=1 ; 1<=k ; i=i+1)
        numerator = numerator * (n-k+1) ;

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


Answer
Change these two lines

// *****************************************************
{
binomial(int n, int k)

to

// *****************************************************
int binomial(int n, int k)
{


a. The function binomial must have a return type (int)
b. The open brace { must come after, not before the function name and parameters.

Do this and the code will compile without errors.

There is at least one other error (a typo) in your program, at this line.

for (i=1 ; 1<=k ; i=i+1) // **** here
  numerator = numerator * (n-k+1) ;

You need to fix that too.  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++09. i would not answer questions about gui and web programming.

Experience

over 15 years

Education/Credentials
post graduate engineer

©2012 About.com, a part of The New York Times Company. All rights reserved.