Need help in every combination program

Hi
I have a every combination program, it's working fine but i've got one problem.
I want to have program which gives me every combination of let's say 10 letters in 7 letters word ( that's 120 combinations)
But my program is giving me combinations of let's say 10 letters = every combination in 10 letter word
So i want this program to do combination of X letters to Y number of letters word. Code of this program is below:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  

#include <iostream>
#include <algorithm>

int main(int argc, char* argv[]) {
	const unsigned short size = 5; //you can change it and then add more letters in char numbers
	char numbers[size] = { 'a', 'b', 'c', 'd', 'e'};

	std::sort(numbers, numbers + size);

	do {
		for(unsigned short i=0; i<size; ++i) {
			std::cout << numbers[i] << (i+1!=size?" ":"\n");
		}
	}while(std::next_permutation(numbers, numbers + size));

	std::cin.get();


	return 0;
}
Last edited on
Clarify what you want @haabee3.

You say that you want combinations (nCr), but your program is producing permutations (nPr). There's a world of a difference. There is only 1 combination of 5 things from 5, but there are 5!=120 permutations of 5 things from 5.

In COMBINATIONS order doesn't matter, just the content of a set. In PERMUTATIONS then the order is crucial.

If you do want combinations then you can create them recursively.
alright, i will try to search about this, thanks for replying!
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
29
30
31
32
#include <iostream>
#include <string>
using namespace std;

const string alphabet = "abcdefghijklmnopqrstuvwxyz";
int counter = 0;


void writeCombinations( string current, int firstLetter, int lastLetter, int remainingLength )
{
   if ( remainingLength == 0 )
   {
      counter++;
      cout << counter << ": " << current << endl;
   }
   else
   {
      for ( int i = firstLetter; i - 1 + remainingLength <= lastLetter; i++ )
      {
         writeCombinations( current + alphabet[i], i + 1, lastLetter, remainingLength - 1 );
      }
   }
}


int main()
{
   int numChoices = 10;
   int wordLength = 7;

   writeCombinations( "", 0, numChoices - 1, wordLength );
}

Last edited on
Topic archived. No new replies allowed.