How do I FIX this function formula!

Page 1 of 1

7 Replies - 888 Views - Last Post: 29 March 2009 - 03:40 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=96040&amp;s=6510f95a31ec6cd9cf3a0d45a53b461e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 Defiant81

Reputation: 0
• Posts: 29
• Joined: 26-February 09

How do I FIX this function formula!

Posted 29 March 2009 - 11:28 AM

Can't seem to figure out why my combination functions keeps returning a 0 value.. and how do I make this program run only 5 times (count-controlled loop).

This post has been edited by Defiant81: 29 March 2009 - 07:21 PM

Is This A Good Question/Topic? 0

Replies To: How do I FIX this function formula!

#2 SassleFrassle

Reputation: -3
• Posts: 21
• Joined: 21-March 09

Re: How do I FIX this function formula!

Posted 29 March 2009 - 02:10 PM

I don't see anything that says for it to loop only 5 times. Why not try something like this?

```while (int count = 1; count <= 5; count++)
{
Whatever
}
```

Hope this helps.

#3 Defiant81

Reputation: 0
• Posts: 29
• Joined: 26-February 09

Re: How do I FIX this function formula!

Posted 29 March 2009 - 02:13 PM

SassleFrassle, on 29 Mar, 2009 - 01:10 PM, said:

I don't see anything that says for it to loop only 5 times. Why not try something like this?

```while (int count = 1; count <= 5; count++)
{
Whatever
}
```

Hope this helps.

does that go at the beginning of the program?

#4 IrishCereal

Reputation: 4
• Posts: 81
• Joined: 11-December 08

Re: How do I FIX this function formula!

Posted 29 March 2009 - 02:24 PM

I thought while loops only worked like this:
```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 Defiant81

Reputation: 0
• Posts: 29
• Joined: 26-February 09

Re: How do I FIX this function formula!

Posted 29 March 2009 - 02:34 PM

ok

This post has been edited by Defiant81: 29 March 2009 - 07:22 PM

#6 SassleFrassle

Reputation: -3
• Posts: 21
• Joined: 21-March 09

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:

I thought while loops only worked like this:
```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 luckyman76

Reputation: 1
• Posts: 32
• Joined: 05-February 09

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:

I thought while loops only worked like this:
```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 Defiant81

Reputation: 0
• Posts: 29
• Joined: 26-February 09

Re: How do I FIX this function formula!

Posted 29 March 2009 - 03:40 PM

ok

This post has been edited by Defiant81: 29 March 2009 - 07:22 PM