Here's my code:

#include <iostream>
using namespace std;

int Factorial(int n)
{ 
    int total = 1;
    while(n!=0)
    {
      total = total * n;
      n--;
    }
    return total;
}

int Combinations(int n, int k)
{
  int count = 1;
  for(int i=0; i<n; i++)
  { 
    for(int y=0; y<(k-i); y++)
    {
      cout << count << " ";
      count = count * Factorial(i-y)/Factorial(y+1);
    }
        cout  << endl;
    }
  return count;
}

int main()
{
  int numb;
  cout << "Enter a number: ";
  cin >> numb;
  cout << " " << Combinations(numb, numb);
}

*My problem is that everytime I run my program I get this:
Enter a number: (for example) 3
1 1 0
0 0
0
0

When I thought I had coded it to look like this:
1
1 1
1 2 1
1 3 3 1

- I don't understand why it's compiling this way...And my deadline is 11pm tonight...*pulling hair*

Edited 6 Years Ago by Nick Evan: Added code-tags

First, get the number of cout statements per line correct. You have a nested loop. Let's look at the first iteration of the outer loop:

int count = 1;
for(int i=0; i<n; i++)
{
for(int y=0; y<(k-i); y++)
{
cout << count << " ";
count = count * Factorial(i-y)/Factorial(y+1);
}
cout << endl;
}

n and k are both 3. Since we're looking at the first iteration, i = 0. So k - i = 3 - 0 = 3. So here's your first inner loop:

for(int y=0; y<3; y++)
{
// display something
// calculate something
}

So you are displaying three things on the first line. Your first line should display a single number : 1.

So right away you have problems with your loop control, completely irrespective of your calculation functions, so ignore them. figure out how many lines you need and how many things need to be displayed on each line and make sure your loop control handles that. Then tackle any calculation problems.

This article has been dead for over six months. Start a new discussion instead.