Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Learn recursion

Reply
Thread Tools

Learn recursion

 
 
red_hax0r@yahoo.com
Guest
Posts: n/a
 
      06-25-2006
I'm trying to learn how to make a scrambling algorithm in C that will
turn out all possible permutations of a word without repeating (but
repeating can easily be fixed). I found out a few algorithms in
comp.lang.c++, but none of them work.

I'm using gcc.

 
Reply With Quote
 
 
 
 
Kenny McCormack
Guest
Posts: n/a
 
      06-25-2006
In article <(E-Mail Removed). com>,
http://www.velocityreviews.com/forums/(E-Mail Removed) <(E-Mail Removed)> wrote:
>I'm trying to learn how to make a scrambling algorithm in C that will
>turn out all possible permutations of a word without repeating (but
>repeating can easily be fixed). I found out a few algorithms in
>comp.lang.c++, but none of them work.


In order to understand recursion, you must first understand recursion.
 
Reply With Quote
 
 
 
 
Morris Dovey
Guest
Posts: n/a
 
      06-25-2006
[OP hasn't shown up on news.qwest.net yet]:

| In article <(E-Mail Removed). com>,
| (E-Mail Removed) <(E-Mail Removed)> wrote:
|| I'm trying to learn how to make a scrambling algorithm in C that
|| will turn out all possible permutations of a word without
|| repeating (but repeating can easily be fixed). I found out a few
|| algorithms in comp.lang.c++, but none of them work.

Recursion is fun. I came up with the following; but you'll need to
clean it up a bit because I didn't check the return value from
malloc() and left a debug printf() in place so you can better see
what's going on during execution.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void permute(char *fixed,char *remain)
{ unsigned i,len = strlen(fixed);
char *newrem;
unsigned char c;

if (strlen(remain))
{ newrem = malloc(1+strlen(remain));
fixed[len+1] = '\0';
for (i=0; i<strlen(remain); i++)
{ fixed[len] = remain[i];
strcpy(newrem,remain);
strcpy(newrem+i,remain+i+1);
printf("permute(\"%s\",\"%s\")\n",fixed,newrem);
permute(fixed,newrem);
}
fixed[len] = '\0';
}
else puts(fixed);
}
int main(void)
{ char left[16]="\0",right[16]="ABCDE";
permute(left,right);
return 0;
}

Hope this helps to get you started (I'm sure there's a better way.)

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto


 
Reply With Quote
 
Frederick Gotham
Guest
Posts: n/a
 
      06-25-2006
(E-Mail Removed) posted:

> I'm trying to learn how to make a scrambling algorithm in C that will
> turn out all possible permutations of a word without repeating (but
> repeating can easily be fixed). I found out a few algorithms in
> comp.lang.c++, but none of them work.
>
> I'm using gcc.



If you're using C++, check this out:


http://en.wikipedia.org/wiki/Template_metaprogramming



--

Frederick Gotham
 
Reply With Quote
 
red_hax0r@yahoo.com
Guest
Posts: n/a
 
      06-26-2006


>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
>
> void permute(char *fixed,char *remain)
> { unsigned i,len = strlen(fixed);
> char *newrem;
> unsigned char c;
>
> if (strlen(remain))
> { newrem = malloc(1+strlen(remain));
> fixed[len+1] = '\0';
> for (i=0; i<strlen(remain); i++)
> { fixed[len] = remain[i];
> strcpy(newrem,remain);
> strcpy(newrem+i,remain+i+1); <<<<


Yeah but what does this line do? Why shoudl it be necessary? I know
it is, because it spit out garbage without it. I tried changing it to
address notation and it works. Any tutorials you could suggest would
be helpful.

> printf("permute(\"%s\",\"%s\")\n",fixed,newrem);
> permute(fixed,newrem);
> }
> fixed[len] = '\0';
> }
> else puts(fixed);
> }
> int main(void)
> { char left[16]="\0",right[16]="ABCDE";
> permute(left,right);
> return 0;
> }
>


 
Reply With Quote
 
Charles Richmond
Guest
Posts: n/a
 
      06-26-2006
Kenny McCormack wrote:
>
> In article <(E-Mail Removed). com>,
> (E-Mail Removed) <(E-Mail Removed)> wrote:
> >I'm trying to learn how to make a scrambling algorithm in C that will
> >turn out all possible permutations of a word without repeating (but
> >repeating can easily be fixed). I found out a few algorithms in
> >comp.lang.c++, but none of them work.

>
> In order to understand recursion, you must first understand recursion.
>

Ah-hhmmm. In order to understand recursion, you must first
understand a *simpler* form the of the recursion.

--
+----------------------------------------------------------------+
| Charles and Francis Richmond richmond at plano dot net |
+----------------------------------------------------------------+
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
va_arg... recursion: changing arguments and the using recursion jononanon@googlemail.com C Programming 8 04-26-2012 08:37 PM
Some information for the one who decided to learn C++, and now wantsto learn at least a bit of C? Alexander C Programming 20 09-11-2010 01:04 AM
Learn the language before you learn the GUI. CoreyWhite C++ 1 03-31-2007 08:56 PM
newbie question: should I learn TKinter or skip it and learn more advanced toolkit? Porky Pig Jr Python 3 05-12-2004 08:58 AM
Output buffering problems during recursion Tim Mohler Perl 1 09-16-2003 12:35 AM



Advertisments