Really don't know how to use recursion

A

a

After several days struggling, I'm still unable to generate a string
automatically *ONE by ONE*. I don't want to create a long array memorizing
all the output strings and then traverse this array to print the content
out.

For example, for studying the frequency of 5-letter words composed of
{a,b,c,d,e} in a
string, the length of array of (aaaaa, aaaab,
...., caaaa, ..., eeeee) will then be 5*5*5*5*5.

Indeed what I want is to print aaaaa first, then aaaab, then aaaac, ...,
eeeea, eeeeb, ..., eeeee.

The problem is complicated by that I want to study n-letter words (the above
example, n=5).

So I try write something like:

char lookup(int lv, int idx) {
if (lv != 0)
lookup(lv-1, idx);

if (idx != 0)
lookup(lv, idx-1);

if (idx == 0) return 'a';
else if (idx == 1) return 'b';
else if (idx == 2) return 'c';
else if (idx == 3) return 'd';
else if (idx == 4) return 'e';
}

but indeed in this way I need to create the long array concatenating one by
one....

Please help :( I'm not asking for the whole codes but I really don't know
what direction I should go for to complete the seemingly automatic task.
 
T

Thad Smith

a said:
After several days struggling, I'm still unable to generate a string
automatically *ONE by ONE*. I don't want to create a long array memorizing
all the output strings and then traverse this array to print the content
out.

For example, for studying the frequency of 5-letter words composed of
{a,b,c,d,e} in a
string, the length of array of (aaaaa, aaaab,
..., caaaa, ..., eeeee) will then be 5*5*5*5*5.

Indeed what I want is to print aaaaa first, then aaaab, then aaaac, ...,
eeeea, eeeeb, ..., eeeee.

The problem is complicated by that I want to study n-letter words (the above
example, n=5).

So I try write something like:

char lookup(int lv, int idx) {
if (lv != 0)
lookup(lv-1, idx);

if (idx != 0)
lookup(lv, idx-1);

if (idx == 0) return 'a';
else if (idx == 1) return 'b';
else if (idx == 2) return 'c';
else if (idx == 3) return 'd';
else if (idx == 4) return 'e';
}

What is this function intended to do? Without a definition of what it
should do, there is no way to evaluate it, except to say that it is more
complicated than it needs to be to return its current result.

Regarding what you say you want, here is the definition of a function to help:

/* Function: NextPermutation
** Description: This function accepts an array containing a permutation of
** n integers 0 to m-1. It returns the next lexically higher
** permutation.
**
** Example: n = 3, m = 4, perm[] = {1,2,3,3}
** result: perm[] = {1,3,0,0}
*/
int /* 0: next permutation returned, 1: entry value was last */
NextPermutation (
unsigned char *perm, /* in: current permutation, each 0..(m-1),
** most significant element first,
** out: next permutation */
int n, /* number of elements in permutation */
int m /* number of values for each element */
);

The output can be mapped easily to lowercase letters (or anything else).
I have NOT written the function, that's your opportunity!
 
A

a

Thad Smith said:
What is this function intended to do? Without a definition of what it
should do, there is no way to evaluate it, except to say that it is more
complicated than it needs to be to return its current result.

Regarding what you say you want, here is the definition of a function to
help:

/* Function: NextPermutation
** Description: This function accepts an array containing a permutation of
** n integers 0 to m-1. It returns the next lexically higher
** permutation.
**
** Example: n = 3, m = 4, perm[] = {1,2,3,3}
** result: perm[] = {1,3,0,0}
*/
int /* 0: next permutation returned, 1: entry value was last
*/
NextPermutation (
unsigned char *perm, /* in: current permutation, each 0..(m-1),
** most significant element first,
** out: next permutation */
int n, /* number of elements in permutation */
int m /* number of values for each element */
);

The output can be mapped easily to lowercase letters (or anything else).
I have NOT written the function, that's your opportunity!

After following your guidance and read posts from Google, I somehow solve
the problem when the word size <= character set size.

In the following code, when I set WSIZE > chrssize, the program fails. Why
the for loop and recursion fails? I have set the list size large enough...

#include <stdio.h>

int WSIZE=2,chrssize=4;
char charList[20];
char chrs[]={'a','b','c','d'};

void permute(int start)
{
int i,j;
if(start==WSIZE)
{
for(i=0;i<WSIZE;i++)
printf("%c",charList);
puts("");
}
else
{
i=start;
int k=0;
for(j=0;j<chrssize;j++)
{
charList=chrs[k++];
permute(start+1);
}
}
}

main () {
permute(0);
}
 
T

Thad Smith

a said:
Thad Smith said:
What is this function intended to do? Without a definition of what it
should do, there is no way to evaluate it, except to say that it is more
complicated than it needs to be to return its current result.

Regarding what you say you want, here is the definition of a function to
help:

/* Function: NextPermutation
** Description: This function accepts an array containing a permutation of
** n integers 0 to m-1. It returns the next lexically higher
** permutation.
**
** Example: n = 3, m = 4, perm[] = {1,2,3,3}
** result: perm[] = {1,3,0,0}
*/
int /* 0: next permutation returned, 1: entry value was last
*/
NextPermutation (
unsigned char *perm, /* in: current permutation, each 0..(m-1),
** most significant element first,
** out: next permutation */
int n, /* number of elements in permutation */
int m /* number of values for each element */
);

After following your guidance and read posts from Google, I somehow solve
the problem when the word size <= character set size.

You missed the essence of my post: explicitly define what your function does.
In the following code, when I set WSIZE > chrssize, the program fails. Why
the for loop and recursion fails? I have set the list size large enough...

#include <stdio.h>

int WSIZE=2,chrssize=4;
char charList[20];
char chrs[]={'a','b','c','d'};

void permute(int start)
{
int i,j;
if(start==WSIZE)
{
for(i=0;i<WSIZE;i++)
printf("%c",charList);
puts("");
}
else
{
i=start;
int k=0;
for(j=0;j<chrssize;j++)
{
charList=chrs[k++];
permute(start+1);
}
}
}

main () {
permute(0);
}


It works for me, as long as chrssizze <= sizeof chrs and WSIZE <= sizeof
charList, after I made C90-compatible changes:
1. define main as int main(void) (also for C99)
2. place i=start; after int k=0;
3. added return 0; to the end of main.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top