I can't figure out how to get my program to calculate binomial coefficients correctly. I can do it with a simple expansion such as (x+y)^5, but when coefficients are added to x and y, the binomial coefficients are not calculated correctly. Can anyone add in what I'm missing?
int main()
{
int a, b;
int x = 1;
int p;
cout << "Enter the coefficients of the expansion: ";
cin >> a; cin >> b;
cout << "Enter the power: ";
cin >> p;
cout << "Expansion is (" << a << "x + " << b << "t)^" << p;
cout << endl << endl;
int y = p;
printf("(%dx + %dy)^%d = %.fx^%d", a, b, p, pow(a, p), p);
for (y = p; --y; printf(" + %dx^%dy^%d", x = x*(y + 1) / (p - y), y, p - y)); //the first numerical identifier is the coefficient that needs to be calculated
printf(" + y^%d\n\n", p);
return 0;
}
Why do you have that printf() within the for() loop, instead of in the body of the loop? I also recommend you move all those calculations outside of the printf() statements. Assign values to variables if you need to print the intermediate results. Also consider using double for your variable types instead of the int.