I have the following code but it is not working correctly:
#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <string>
#define MAXN 100
const unsigned n=4;
const unsigned k=2;
int taken[MAXN];
void print(unsigned i)
{
unsigned l;
printf(" ( ");
for (l=0; l<=i-1; l++) printf("%u ", taken[l] + 1);
printf(")\n");
}
void variate(unsigned i)
{
unsigned j;
if (i>=k)
{
print(i);
return;
}
for (j=0;j<n;j++)
{
taken[i]=j;
variate(i+1);
}
}
void condition(unsigned number)
{
unsigned j;
if (number>=k)
{
print(number);
return;
}
for (j=0;j<n;j++)
{
taken[number]=j;
variate(number+1);
}
}
int main(void)
{
variate(0);
int number;
printf("Enter number from 1 to 4: \n", number);
scanf("%d", &number);
printf("All varians of the combinations with your number are: \n");
condition(0);
system ("pause");
return 0;
}
The program is printing all possible combinations of the numbers 1, 2, 3 and 4 and it works correctly.
But the void condition
is not working fine. After printing all possible combinations of the four numbers the user have to enter a number between 1 and 4 and all combinations with user's number have to appear and none of the rest of the combinations.