473,382 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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;
}
Nov 23 '07 #1
3 1328
seforo
60
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
Nov 24 '07 #2
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}
}
Nov 24 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
When i is 0 your denominator is 0. That's gonna be a long, long divide.
Nov 24 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: zhouhaiming | last post by:
On our test machine I can compile my c source code on AIX5.1(ML4) by "- q32" option and "OBJECT_MODE=32" environment variable, and it can correctly running. My operation system environment is:...
0
by: Tom Lee | last post by:
Hi, I'm new to .NET 2003 compiler. When I tried to compile my program using DEBUG mode, I got the following errors in the C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7 \include\xdebug...
3
by: Yang Zhang | last post by:
Here is a program: ///////////////////////////////////////////////// #include <iostream> using namespace std ; class A { int a ; A(const A& aA) { a=aA.a ; cout<<"copy constructor...
8
by: Davy | last post by:
Hi all, I use VC and gcc/gdb to compile and debug C/C++ files. But I found some of the debug version of the compiled files are too large to be run in a small RAM. Can I compile C/C++ Debug...
6
by: Arthur | last post by:
I had two Visual Studio .NET C++ solutions that I combined into one. These project solutions were very similar. In fact, the two solutions were sharing many files. Now that they are one...
1
by: BLACKDOG157 | last post by:
I tried to compile an existing program (which I want to modify). The program is written in C++. So I downloaded Microsoft's Visual C++ Express edition and I tried to compile the program. It...
6
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm lost in the Java world. The script at the bottom of...
109
by: zaidalin79 | last post by:
I have a java class that goes for another week or so, and I am going to fail if I can't figure out this simple program. I can't get anything to compile to at least get a few points... Here are the...
14
by: hall.jeff | last post by:
I wrote the following simple program to loop through our help files and fix some errors (in case you can't see the subtle RE search that's happening, we're replacing spaces in bookmarks with _'s) ...
30
by: Anarki | last post by:
The following is the program i am trying to compile //restrict.c #include <stdio.h> int main() { char arr = "Qualifiers" char * restrict p = arr; int i = 0; for(; i < 10; ++i)
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.