Help in combination program

Hi I just started learning C++ program last week and I was trying to figure out the C++ program code for a combination program. I did this and my logic seem to be right to me but I think I cant work this way:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

#include <iostream>
using namespace std;

int main (int argc, char * const argv[]) {
    // insert code here...
    long n,r,a,b,c,d,e;
	cout << "Please Enter the number (N) for combination C(N,r): " ;
	cin >> n;
	cout << " Please Enter your r";
	cin >> r;
	for (int i=1; i<=n; i++) {
		a=a*i;
	}
	cout << " Your N! value is " << a <<endl ;
	b= n-r;
	for (int j = 1; j <=b; j++) {
		c=c*j;
	}
	cout << " Your (N-r)! valuse is " << c << endl;
	for (int k =1; k <=r; k++) {
		d=d*k;
	}
	cout<< "Your value or r! is " << d<< endl;
	e= a/(d*c);
	cout<<" The required way on how combinations can be done is" << e<< endl;
    return 0;
}

Last edited on
Remember that after declaring variables like this:

long n,r,a,b,c,d,e;

they have "garbage" values - ie, if you were to print them to cout immediately, they would contain (effectively) random values.

(hint: have a look at lines 13, 18 and line 22 - what is the initial value of a, c and d?)
Last edited on
mmay is right.

Global Variables, if not initialized, automatically are given the value of zero. Local variables that are not initialized are filled with garbage, random numbers that are highly ineffectual.

You might want to initialize each of your variables by giving them values.

Here's an example

int r = 0;

The rest is pretty straight forward. I gave you a huge hint, right there. Good Luck!

-Code Assassin
Last edited on
Thank you so much guys!!
Topic archived. No new replies allowed.