Hello there!
First off, I'm just in school, 10th grade (second year of computer science), so I am not very advanced.
I was wondering...How can I write a simple program that does combinations, in a certain way?
Let's say I have 5 buttons. I want all combinations of pressed and unpressed buttons. X can be a pressed one, and 0 an non-presssed one. I want to make those combinations:
X0000
XX000
X0X00
X00X0
X000X
XXX00
XX0X0
XX00X
X0XX0
X0X0X
and so forth, making all the possible combinations with 1 X, then with 2 X'es, then with 3 and so forth.I can make 5 different programs for this (if the maximum is 5 Xes), using lots of "for", but I don't know how to make it all in one program, because no teacher will teach us stuff like this, because it isn't very usefull for the contests in my country.

With a bit of independent studiing, I think it should be backtracking or recursivity? I don't really know, I'm still a newbie :)
Sorry for my bad English, and thank you kindly if you read this.

PS: I've searched google before posting this, and I only found simple explanations for programs that can only solve this problem for a certain number of "X",and this I can allready do, and I want one that can do it for any number of "buttons", using from 1 X to maximum X's

Please show us what you have, then we may try to direct you towards your goal.

I think this is not homework. I tried to think of an algorithm for a while, but I suck at it (plus I am only 14), so I decided to write the hard, computation power, brute-force function to do that. But it too, takes a while. So when I am done, I will share it with you :)

> I think this is not homework.
It doesn't matter.
> plus I am only 14
At 14 it could be fun. At 16 it's a routine.
Besides, OP's problems may stem from any possible direction, and I broke my last crystal ball yesterday.
Доброй ночи.

OP here.
This is not a homework, school has not even started here :). + we don't do stuff like this in my school.
I'm sorry for taking so much to respond. Here is a type of program I can do, but it only works for the example I gave (With maximum 5 buttons). Instead of X and O my program will only give the positions of the pressed buttons.

``````#include<stdio.h>
int a,b,c,d;
int main(){
freopen("example.out","w",stdout);
for(a=1;a<=5;++a) printf("%d\n", a); //with one button pressed
for(a=1;a<=4;++a)
for(b=a+1;b<=5;b++)
printf("%d %d\n", a, b); //2 buttons.
for(a=1;a<=3;++a)
for(b=a+1;b<=4;++b)
for(c=b+1;c<=5;++c)
printf("%d %d %d\n", a, b,c); //3 buttons.
for(a=1;a<=2;++a)
for(b=a+1;b<=3;++b)
for(c=b+1;c<=4;++c)
for(d=c+1;d<=5;++d)
printf("%d %d %d %d\n", a,b,c,d); //4 buttons
for(a=1;a<=5;++a) printf("%d ", a); //all buttons..kinda useless if done with the same method, 5 fors, but useless to do anyway.
printf("\n");
return 0;
}``````

This is brute force, and only works on the example I gave, with 5 buttons. I'll try today to find something more about backtracking, and make a program that works for all examples. Some tips will still be appreciated :). If you can only tell me a little hint about what should I go and learn, that would be enough for me :)

Damn we only learn boring stuff in school..not interesting ones like this

So you have 5 places, 2 states each. Sounds like a set of 5-digit binary numbers. 0 to 31.

I think i have a little ideea..it will imply a for (that repreats for the number of buttons), and a lot of recursivity, which i'm not very good at. It will take me a few hours, but it's worth it. If you have a better ideea, or some tips, feel free to help. Thanks.

EDIT: I've done it without recursivity. Thanks to all that offered to help. Here is the code if you want to see it :)

``````#include<stdio.h>
int n,i,j,k,x[100],nr,m;
int main(){
freopen("comb.in","r",stdin);
freopen("comb.out","w",stdout);
scanf("%d", &m); //cate butoane avem.
//for(i=1;i<=nr;++i) x[i]=i;
for(n=2;n<=m;++n){
for(i=1;i<=m;++i) x[i]=0;
k=1;
do{
while(x[k]<m-n+k){
x[k]=1+x[k];
if(k==n){
for(j=1;j<=n;++j)
printf("%d ", x[j]);
printf("\n");
}
else{
k++;
x[k]=x[k-1];
}
}
k--;
}while(k>0);
}
}``````
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.