math is breaking i think...
Feb 27, 2011 at 5:51pm UTC
i am in a discrete struct class and a program i had to make works but also seem to get negative numbers and dont know why. i think the fractorial function is breaking. here is the code and any help would be great thanks....
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int m,n,answer;
int factorial(int );
cout << "Enter number of Martians: " ;
cin >> m;
cout << "Enter number of Jovians: " ;
cin >> n;
if (n<(m+2)) // easily checks for doubleing issues--m=2 and n=4 eg. _m_m_ 3 spots but 4 n's
{ // will cause doubleing events in every case!
// answer= m!*((m+1)!/(m-n+1)!)
answer = ((factorial(m))*((factorial(m+1))/(factorial(m-n+1))));
cout << answer << endl;
}else {
cout << "No combinations possible." <<endl;
}
system("pause" );
return 0;
}
int factorial(int i)
{
for (int j=(i-1);j!=0;j--)
i = i*j;
return i;
}
PS-- i am using Visual C++
Feb 27, 2011 at 6:09pm UTC
works fine for me..
Feb 27, 2011 at 6:15pm UTC
i closed and reopened it and now i am getting right answers... stupid programming :)
Topic archived. No new replies allowed.