i have to make a program that reads m,n from the keyboard.
i have :
Code:
char letters[]={'A','B','C','D','E','F','G','H','I','J','K'}
the program is going to make commutations of those letters.
if n=2 and m=4 the output will look something like this :
AB,AC,BA,BC ... etc
so it will take the 4 starting letters and make groups of 2.
this is the program that i done so far...
but at the fuction Valid() i am missing something
i didn't manage to figure what. All help is welcomed.
Sorry for the bad english.
Code:
#include <stdio.h>
int m,n;
int st[11];
char litere[]={'A','B','C','D','E','F','G','H','I','J','K'};
void Init(int st[],int k)
{
st[k]=0;
}
int Am_Succesor(int st[],int k)
{
if(st[k]<n)
{
st[k]++;
return 1;
}
else return 0;
}
int Valid(int st[],int k)
{
int i;
for(i=1;i<k;i++)
if(st[k]==st[i]) return 0;
return 1;
}
int Solutie(int k)
{
if(k==n) return 1;
else return 0;
}
void Tipar()
{
int i;
for(i=1;i<=n;i++)
printf("%c",litere[st[i]]);
printf("\n");
}
void back()
{
int k;
int As,Ev;
k=1;
Init(st,k);
while(k>0)
{
do
{
As=Am_Succesor(st,k);
if(As==1) Ev=Valid(st,k);
}while((As==1)&&(Ev==0));
if(As)
if(Solutie(k)) Tipar();
else
{
k++;
Init(st,k);
}
else k--;
}
}
int main(void)
{
printf("m="); scanf("%d",&m);
printf("n="); scanf("%d",&n);
back();
return 0;
}
in the program i done so far if i put m=4 and n=2 i get : BC CB only .