Stuck in this question .I cant spot where i am doing the mistake. Can somebody help me please. thanks.

this is the question:
-----------------------------------------------
The number of combinations (C) from M items taken N at a time is given by
C= fac(M) / [fac(N) * fac (M – N)]
where fac(X) = X * (X – 1) * (X - 2) * … * 2 *1
Write a function to evaluate fac(X) and incorporate it in a progam to request input
values M and N and calculate C.
===============================================

Below is my code. I have done 3 recursive functions that i want to recall and place in the formula. i also made X represent M-N. finally i wanted to print the result of c. But as I am still learning i am obviously doing lots of mistakes.Sorry about that. please have a look at the code for where i am wrong. thanks.Or maybe the whole code is wrong. please let me know. Thanks.

#include <iostream> 
using namespace std; 
 
int M(int m); 
int N(int n);
int X(int x);
 
int main() 
{ 
  int m,n,x,c;
  cout << "Please enter value of M: "<<M(m)<<endl;
  cin>>m;
  cout << "Please enter value of N: "<<N(n)<<endl;
  cin>>n;
  x=(m-n);
  c=(M(m)/(N(n)*X(x));
  cout << "Calculated value of C is :"<<c<<endl;
 
  return 0; 
} 
 
int M(int m)  
{ 
  int answerm; 
 
  if(m==1)  
      return(1); 
  
  answerm = M(m-1)*m; 
  return(answerm); 
}

int N(int n)

{
 int answern;
  if(n==1)
    return(1);
    answern = N(n-1)*n;
    return(answern);
}


   int X(int x)

{
 int answerx;
  if(x==1)
    return(1);
    answerx = X(x-1)*x;
    return(answerx);
}

as a newbie myself take my advice with a pinch of salt

why have three functions that all do the same thing why not have just one and call it three times?

secondly i find recursion a nightmare to get my head round so where a simple while loop would suffice i would use it until my grasp of the language and the idea of recursion was better.

also be wary of user input as factorial numbers get very big very very quick

Below, your main with corrections and remarks. Personnally I would
avoid having the function M called recursively by itself. The called
functions don't return a value until m=1 is reached. This could lead
to many threads running simultaneously, I think. There are several
solutions. A for loop could be used:
int answerm=1;
for(int i=2; i<=m; i++)
answerm *=i;
Another remark: if n<m, you get in serious troubles!

#include <iostream> 
using namespace std; 
 
int M(int m); 
int N(int n);
int X(int x);
 
int main() 
{ 
  int m,n,x,c;
  //cout << "Please enter value of M: "<<M(m)<<endl;	As M(m) has
  //not yet been calculated, it cannot be printed
  cout << "Please enter value of M: "<<endl;
  cin>>m;
  cout << "Please enter value of N: "<<endl;
  cin>>n;
  x=(m-n);
  //c=(M(m)/(N(n)*X(x));		there is an extra (
  c=M(m)/(N(n)*X(x));
  cout << "Calculated value of C is :"<<c<<endl;
 //why 3 times the same function? M, N, and X are absolutely identical.
  //You could simply write
  c=M(m)/(M(n)*M(x));
  cout << c << endl;
  return 0;

Sorry, I meant m<n give troubles!

You defintely have a point frogboy.Thanks r.evrard. well i called the function three times because i had no idea how to change the values in the functions for each parameter including M-N. I understand function a bit that is why i had done it the long way. I am looking at your code now. So how would u incorporate the loop with varying the parameters M, N and M-N?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.