4 Replies - 830 Views - Last Post: 15 November 2009 - 04:33 PM Rate Topic: -----

#1 qi0n   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 22-October 09

functions with factorials

Posted 15 November 2009 - 02:58 PM

i have the given problem solved for use of no functions.. i am just confused on how i would do the functions to give back the factorials of the results. (functions that dont output the result right in it confuse me as how to do it)


The expression C(n,r) denotes the number of r-element subsets of an n-element set.
The value of C(n,r) is given by the formula
c(n,r) = n! / ( r! * (n-r)! )
program that computes C(n,r) using the following component functions.
( a ) main: prompts the user for two numbers and accents them into n and r, respectively.
( b ) check: compares r and n. If r > n, check invokes the function err_msg,
which prints an appropriate error message.
( c ) comb: computes C(n,r).
( d ) fact: computes factorial.


#include <iostream>
#include <cmath>
#include <iomanip>

	   
using namespace std;	   
int main ()
{	
int n,r,z;  
double ntotal = 1, rtotal = 1,ztotal = 1, integer, c;

	  
cout << "Enter n in C(n,r) : ";
cin >> n;	 
cout << "Enter r in C(n,r) : ";
cin >> r;	
	  
//if n is less than r error message
if ( n < r)
cout << "n must be greater than or equal to r in C(n,r)" << endl;

else {
	  
//n factorial
for ( int integer = n; integer >= 1; integer-- )
{

ntotal = ntotal * integer;
  
}
 
	  
	  
// r factorial
for ( int integer = r; integer >= 1; integer-- )
{

rtotal = rtotal * integer;
  
}


//n-r factorial
z= n-r;
for ( int integer = z; integer >= 1; integer-- )
{

ztotal = ztotal * integer;
  
}



//final calculations of the final answer
c = (ntotal / (rtotal * ztotal));


//Display of Results

cout << c << endl;

}
   
system ("pause");
return 0;
}	  


This post has been edited by qi0n: 15 November 2009 - 03:20 PM


Is This A Good Question/Topic? 0
  • +

Replies To: functions with factorials

#2 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3213
  • View blog
  • Posts: 19,241
  • Joined: 14-September 07

Re: functions with factorials

Posted 15 November 2009 - 03:34 PM

Throw the logic into a function, example:

int nFactorial(int n){
	 int ntotal = 0;
	 //n factorial
	 for ( int integer = n; integer >= 1; integer-- ){
		  ntotal = ntotal * integer;
	 }
	 return ntotal;
}



You'd then call the function in main, assigning the return value to some variable that you'll use later. i.e

int main(){
	 int n;
	 cin >> n;
	 int nTotal = nFactorial(n);
	 //do something with nTotal;
	 return 0;
}


Was This Post Helpful? 0
  • +
  • -

#3 qi0n   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 22-October 09

Re: functions with factorials

Posted 15 November 2009 - 03:58 PM

im still a little stuck on it.. heres how i thought it would go (adding in a cout for ntotal,rtotal,ztotal just to check the values) but it still dosnt work


#include<iostream>

using namespace std;

int nFactorial(int n){
	 int ntotal = 1;
	 //n factorial
	 for ( int integer = n; integer >= 1; integer-- ){
		  ntotal = ntotal * integer;
	 }
	 return ntotal;
}



int rFactorial(int r){
	 int rtotal = 1;
	 //r factorial
	 for ( int integer = r; integer >= 1; integer-- ){
		  rtotal = rtotal * integer;
	 }
	 return rtotal;
}


int zFactorial(int z){
	 int ztotal = 1;
	 //z factorial
	 for ( int integer = z; integer >= 1; integer-- ){
		  ztotal = ztotal * integer;
	 }
	 return ztotal;
}

int main(){
int n,r,z;  
double c;

cout << "Enter n in C(n,r) : ";
cin >> n;	

cout << "Enter r in C(n,r) : ";
cin >> r;  

if ( n >= r)
{
	 
cout << c << endl;
z = n - r;

int nTotal = nFactorial(n);
int rTotal = rFactorial(n);
int zTotal = zFactorial(n);

c = (nTotal / (rTotal * zTotal));

cout << nTotal << "\n"<< rTotal << "\n"<< zTotal << "\n";  
}



	 else 
	 cout << "n must be greater than or equal to r in C(n,r)" << endl;
  
system ("pause");
  return 0;

}


Was This Post Helpful? 0
  • +
  • -

#4 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3213
  • View blog
  • Posts: 19,241
  • Joined: 14-September 07

Re: functions with factorials

Posted 15 November 2009 - 04:00 PM

You can't output 'c' until you assign it some meaningful data.


also, each factorial part is supposed to take its named equivalent variable right? nFactorial should get 'n', rFactorial should get 'r', etc...
Was This Post Helpful? 1
  • +
  • -

#5 qi0n   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 22-October 09

Re: functions with factorials

Posted 15 November 2009 - 04:33 PM

figured it out

This post has been edited by qi0n: 15 November 2009 - 04:37 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1