your debugged code :-
error you were making was in tacking the row at each recursive
call...and outer loop runs for 2...which should actually runs for all
words
#include<iostream>
#include<string>
#include<cstdio>
using namespace std;
string str[]={"hello", "how","a"};
#define words_len 3
void print_all(string sel, int row,int k,int n)
{
if(k == n){
static int count =1;
cout<<"---->"<<count++<<" "<<sel<<endl;
return;
}
for(int i = row; i < words_len ; i++)
{
for(int j = 0; j < str[i].length(); j++)
{
print_all(sel+str[i][j],i+1, k+1,n);
}
}
}
int main()
{
print_all("",0,0,2);
}
On 10/1/12, atul anand <[email protected]> wrote:
> my prev code was incorrect :-
>
> here is the correct code :-
>
> void combination(char str[3][100],int len,int row,int j)
> {
> static char prn[3];
> int i=0,k=0,p=0;
>
> if(j==2)
> {
> prn[j]='\0';
> printf("\n%s",prn);
> return;
> }
> for(p=row;p<len;p++)
> {
> for(k=0;k<strlen(str[p]);k++)
> {
> prn[j]=str[p][k];
> combination(str,len,p+1,j+1);
>
> }
> }
> }
> call : combination(str,len,0,0); // len = number of words
>
> for input : str[3][100]={"hello","how","a"};
>
> output :-
> hh
> ho
> hw
> ha
> eh
> eo
> ew
> ea
> lh
> lo
> lw
> la
> lh
> lo
> lw
> la
> oh
> oo
> ow
> oa
> ha
> oa
> wa
>
>
>
>
> On 10/1/12, ~*~VICKY~*~ <[email protected]> wrote:
>> Hi atul,
>>
>> Thanks for your response. I found your idea matches very much with my
>> approach which i claimed buggy. The problem in my code is that It prints
>> some extra strings in the end. For eg for the below input it prints
>> 24(5*3+3*3) comb instead of 15(5*3). I couldn't recognize how to resolve
>> it. Could any1 help me out.
>>
>> #include<iostream>
>> #include<string>
>> #include<cstdio>
>>
>> string str[]={"hello", "how","a"};
>>
>> void print_all(string sel, int k,int n)
>> {
>> if(k == n){
>> static int count =1;
>> cout<<"---->"<<count++<<" "<<sel<<endl;
>> return;
>> }
>> for(int i = k; i < n ; i++)
>> {
>> for(int j = 0; j < str[i].length(); j++)
>> {
>> print_all(sel+str[i][j], k+1,n);
>> }
>> }
>> }
>> int main()
>> {
>> print_all("",0,2);
>> }
>> //code ends
>>
>> On Mon, Oct 1, 2012 at 12:33 AM, atul anand <[email protected]>
>> wrote:
>>
>>> void combination(char str[2][100],int len,int row,int j)
>>> {
>>> static char prn[3];
>>> int i=0,k=0,p=0;
>>>
>>> if(j==2)
>>> {
>>> prn[j]='\0';
>>> printf("\n%s",prn);
>>> }
>>> for(p=row;p<len;p++)
>>> {
>>> for(k=0;k<strlen(str[row]);k++)
>>> {
>>> prn[j]=str[row][k];
>>> combination(str,len,p+1,j+1);
>>>
>>> }
>>> }
>>> }
>>>
>>> call : combination(str,len,0,0);// len = number of words in your eg call
>>> will be
>>> combination(str,2,0,0);
>>>
>>> On 9/30/12, ~*~VICKY~*~ <[email protected]> wrote:
>>> > Given 'n' arrays each of variable sizes. Write code to print all
>>> > combinations. Each combination contains one element from one array
>>> > each.
>>> > For eg:
>>> > string str[]={"hello", "how"}
>>> >
>>> > op will be
>>> > hh
>>> > ho
>>> > hw
>>> > eh
>>> > eo
>>> > ew
>>> > lh
>>> > lo
>>> > lw
>>> > lh
>>> > lo
>>> > lw
>>> > oh
>>> > oo
>>> > ow
>>> >
>>> > 15 sets would come, includes repetition.
>>> >
>>> > My approach had bugs and didn't work. Help would be appreciated.
>>> >
>>> >
>>> >
>>> >
>>> > --
>>> > Cheers,
>>> >
>>> > Vicky
>>> >
>>> > --
>>> > You received this message because you are subscribed to the Google
>>> > Groups
>>> > "Algorithm Geeks" group.
>>> > To post to this group, send email to [email protected].
>>> > To unsubscribe from this group, send email to
>>> > [email protected].
>>> > For more options, visit this group at
>>> > http://groups.google.com/group/algogeeks?hl=en.
>>> >
>>> >
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups
>>> "Algorithm Geeks" group.
>>> To post to this group, send email to [email protected].
>>> To unsubscribe from this group, send email to
>>> [email protected].
>>> For more options, visit this group at
>>> http://groups.google.com/group/algogeeks?hl=en.
>>>
>>>
>>
>>
>> --
>> Cheers,
>>
>> Vicky
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Algorithm Geeks" group.
>> To post to this group, send email to [email protected].
>> To unsubscribe from this group, send email to
>> [email protected].
>> For more options, visit this group at
>> http://groups.google.com/group/algogeeks?hl=en.
>>
>>
>
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.