7 Replies - 553 Views - Last Post: 29 March 2009 - 03:40 PM Rate Topic: -----

#1 Defiant81  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 Defiant81  Icon User is offline

  • New D.I.C Head

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

Re: How do I FIX this function formula!

Posted 29 March 2009 - 02:13 PM

View PostSassleFrassle, 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?
Was This Post Helpful? 0
  • +
  • -

#4 IrishCereal  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • 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;
}


Was This Post Helpful? 1
  • +
  • -

#5 Defiant81  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#6 SassleFrassle  Icon User is offline

  • New D.I.C Head

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

Re: How do I FIX this function formula!

Posted 29 March 2009 - 03:26 PM

View PostDefiant81, on 29 Mar, 2009 - 01:34 PM, said:

View PostIrishCereal, 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. :(
Was This Post Helpful? 0
  • +
  • -

#7 luckyman76  Icon User is offline

  • New D.I.C Head

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

Re: How do I FIX this function formula!

Posted 29 March 2009 - 03:30 PM

When you implement this part of your code(in your combination function):
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?




View PostDefiant81, on 29 Mar, 2009 - 01:34 PM, said:

View PostIrishCereal, 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).

Was This Post Helpful? 0
  • +
  • -

#8 Defiant81  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1