This post has been edited by Defiant81: 29 March 2009  07:21 PM
How do I FIX this function formula!
Page 1 of 17 Replies  627 Views  Last Post: 29 March 2009  03:40 PM
#1
How do I FIX this function formula!
Posted 29 March 2009  11:28 AM
Replies To: How do I FIX this function formula!
#2
Re: How do I FIX this function formula!
Posted 29 March 2009  02:10 PM
while (int count = 1; count <= 5; count++) { Whatever }
Hope this helps.
#3
Re: How do I FIX this function formula!
Posted 29 March 2009  02:13 PM
#4
Re: How do I FIX this function formula!
Posted 29 March 2009  02:24 PM
while(count < 5) { stuff ++count; }
I thought you had to use a for loop for something like this:
for(int ii = 0; ii < 5; ++ii) { stuff; }
#5
Re: How do I FIX this function formula!
Posted 29 March 2009  02:34 PM
This post has been edited by Defiant81: 29 March 2009  07:22 PM
#6
Re: How do I FIX this function formula!
Posted 29 March 2009  03:26 PM
Defiant81, on 29 Mar, 2009  01:34 PM, said:
IrishCereal, on 29 Mar, 2009  01:24 PM, said:
while(count < 5) { stuff ++count; }
I thought you had to use a for loop for something like this:
for(int ii = 0; ii < 5; ++ii) { stuff; }
awesome thanks for your help..one last problem.. if you take a look at my code.. the combination function... I can't get it to return anything but 0..any ideas?
#include <iostream> #include <iomanip> using namespace std; int Factorial(int); int Combinations(int,int); int main(void) { for(int ii = 0; ii < 5; ++ii) { int numberOfSets = 0, numberOfObjects = 0; cout << "Enter the number of objects to pick (k).\n"; cin >> numberOfObjects; if (numberOfObjects < 1) { cout << "That number is invalid because it is less than 1.\n"; return 0; } else numberOfObjects = numberOfObjects; cout << "Now enter the number of sets (n):\n"; cin >> numberOfSets; if (numberOfSets < 1) { cout << "That number is invalid because it is less than 1.\n"; return 0; } else numberOfSets = numberOfSets; int factorialResult = Factorial(numberOfSets); int combinationsResult = Combinations(numberOfObjects, numberOfSets); cout << "Using " << numberOfSets << " sets, the factorial is " << factorialResult << endl; cout << "Using " << numberOfObjects << " objects, the combination result is " << combinationsResult << endl; } return 0; } int Factorial(int numSets) { int factorial_result = 1; while (numSets > 0) { factorial_result *= numSets; } return factorial_result; } int Combinations(int numObjects, int numSets) { int factorial_result_o = 1; while (numObjects > 0) { factorial_result_o *= numObjects; } int difference = 1; difference = (numSets  numObjects); int factorial_result_os =1; while (difference > 0) { factorial_result_os *=difference; } return ((numObjects/(factorial_result_o * factorial_result_os))); }
here is the output
Enter the number of objects to pick (k).
9
Now enter the number of sets (n):
5
Using 5 sets, the factorial is 120
Using 9 objects, the combination result is 0
Enter the number of objects to pick (k).
The very last line in the code:
return ((numObjects/(factorial_result_o * factorial_result_os))); is yielding a zero.
So by basic algebra, the "numObjects" is some how coming out with a 0. I found this error by replacing that line with return ((numObjects+(factorial_result_o * factorial_result_os))); Whether this is the actual calculation you want, I don't know. Multiplication was the same way, w/ a 0 at the beginning.
Sorry, I'm new to this stuff too, so I can't tell you how to fix it.
#7
Re: How do I FIX this function formula!
Posted 29 March 2009  03:30 PM
while (numObjects > 0) { factorial_result_o *= numObjects; }
You decrement numObjects to zero...even though it won't go through the while loop again because of your condition being greater than zero, numObjects will decrement that last time through to zero which kicks you out of the loop.
Once you get to the end of your code:
return ((numObjects/(factorial_result_o * factorial_result_os)));
you have zero on top....you think that might be the case?
Defiant81, on 29 Mar, 2009  01:34 PM, said:
IrishCereal, on 29 Mar, 2009  01:24 PM, said:
while(count < 5) { stuff ++count; }
I thought you had to use a for loop for something like this:
for(int ii = 0; ii < 5; ++ii) { stuff; }
awesome thanks for your help..one last problem.. if you take a look at my code.. the combination function... I can't get it to return anything but 0..any ideas?
#include <iostream> #include <iomanip> using namespace std; int Factorial(int); int Combinations(int,int); int main(void) { for(int ii = 0; ii < 5; ++ii) { int numberOfSets = 0, numberOfObjects = 0; cout << "Enter the number of objects to pick (k).\n"; cin >> numberOfObjects; if (numberOfObjects < 1) { cout << "That number is invalid because it is less than 1.\n"; return 0; } else numberOfObjects = numberOfObjects; cout << "Now enter the number of sets (n):\n"; cin >> numberOfSets; if (numberOfSets < 1) { cout << "That number is invalid because it is less than 1.\n"; return 0; } else numberOfSets = numberOfSets; int factorialResult = Factorial(numberOfSets); int combinationsResult = Combinations(numberOfObjects, numberOfSets); cout << "Using " << numberOfSets << " sets, the factorial is " << factorialResult << endl; cout << "Using " << numberOfObjects << " objects, the combination result is " << combinationsResult << endl; } return 0; } int Factorial(int numSets) { int factorial_result = 1; while (numSets > 0) { factorial_result *= numSets; } return factorial_result; } int Combinations(int numObjects, int numSets) { int factorial_result_o = 1; while (numObjects > 0) { factorial_result_o *= numObjects; } int difference = 1; difference = (numSets  numObjects); int factorial_result_os =1; while (difference > 0) { factorial_result_os *=difference; } return ((numObjects/(factorial_result_o * factorial_result_os))); }
here is the output
Enter the number of objects to pick (k).
9
Now enter the number of sets (n):
5
Using 5 sets, the factorial is 120
Using 9 objects, the combination result is 0
Enter the number of objects to pick (k).
#8
Re: How do I FIX this function formula!
Posted 29 March 2009  03:40 PM
This post has been edited by Defiant81: 29 March 2009  07:22 PM
