• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 282
  • Last Modified:

All permutations

Hi,
   I have not programming experience in any languages, therefore I need some help with a program that I either have to find and download one that some one has already wrote, or write one just for this case.

I need a program that will be able to output all the permutations of a certain numbers of letters. That means I can input, say, 10 letters, which may or may not contain same letters, and  the program will output all the permutations. E.g., I input ABC and the program will ouptput ABC, ACB, BAC, BCA, CAB, CBA. So, all the combinations in which the order matters.

It needs to be able to handle large number of permutations such as with letters S S U U A C E P K T, which has 907200 different permutations.

Thanks,
Mark.
0
markma060100
Asked:
markma060100
  • 4
  • 3
  • 3
  • +1
1 Solution
 
CJ_SCommented:
Is this homework?

Regards,
CJ
0
 
markma060100Author Commented:
No, it's not.
i'm doing it  for some word unscrambling games.
0
 
griesshCommented:
markma

this is a quick and dirty implementation, but it should compile on most platforms (I used VC++). let me know if you have problems.

======
Werner


========================================================
#include <stdio.h>
char buf[20];
char done[20], rest[20];
int nc =0;

void perm (char* done, char* rest)
{
     char d[20], r[20];
     int lend=0, lenr=0, i, j;
     char* p;
     for (i=0; i<20; i++) { d[i]=done[i]; r[i]=rest[i];}

     p=r;
     while (*p != 0) {p++;lenr++;}
     
     p=d;
     while (*p != 0) {p++;lend++;}

     if (lenr == 2){
          printf ("%s%c%c\n", done, rest[0], rest[1]);
          printf ("%s%c%c\n", done, rest[1], rest[0]);
     }
     else {
          for (i=0; i<lenr; i++) {
               d[lend]=rest[i];
               for (j=0; j<20; j++) r[j]=rest[j];
               for (j=i; j<19; j++) r[j]=rest[j+1];
               perm (d, r);
          }
     }
}
         

void main()
{
 char *ch = buf;
 int i;

 for (i=0; i<20; i++) {done[i]=0; rest[i]=0;}

 while ( (*ch= getchar()) != (char) 10)
 {
      ch++;
      nc++;
 }
 *ch = 0;
 
 printf ("%d : %s -----------\n", nc, buf);

 for (i=0; i<nc; i++) rest[i]=buf[i];

 perm (done, rest);

}

0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
markma060100Author Commented:
griessh,
Thankyou for your help.
However, I do have a problem with compiling the program.
Here is what it said:

Saving all files...
Creating OLE types...
Updating 0 COM classes
--------------- Win32 - Debug ---------------
Compressing form files...
---> Executing nmake /nologo /f makefile.mak cfg="Win32 - Debug" <---
Compiling...
Can't start nmake /nologo /f makefile.mak cfg="Win32 - Debug"
Finish.
Process exit code is 4 - can not start DEBUG\PERMUTATIONS.EXE
0
 
griesshCommented:
Strange ...
Here is what to do from the menue:
New Project->Win32 Console App
create an Empty App
New File -> C++ Source
name it perm.c
Copy&Paste the code into the file and compile. Should work ... if not I will send you the binary ...

Let me know

======
Werner
0
 
jklmnCommented:
Hi markma,

This is not so easy and I believe this is not a home work :-) Try following VB code:

Option Explicit
Private total As Integer, wholeS As String

Private Sub Command1_Click()
Dim items() As String, i As Integer, j As Integer, n As Long
Dim s As String, s1 As String, s2 As String

s = "aabb"      'the letters
total = Len(s)
ReDim items(total)
For i = 1 To total
items(i) = Mid(s, i, 1)
Next i
ListAll items(), 1
Print wholeS
        'delete repeated cases
s2 = wholeS
s = ""
Do
n = InStr(s2, " ")
s1 = Left(s2, n)
s = s & s1
s2 = Mid(s2, n + 1)
s2 = Replace(s2, s1, "")
Loop Until s2 = ""
Print s

End Sub

Private Sub ListAll(items() As String, n As Integer)
Dim i As Integer, j As Integer, items2() As String, s As String
ReDim items2(total)

If n > total Then
For i = 1 To total
s = s & items(i)
Next i
wholeS = wholeS & s & " "
Exit Sub
End If
For i = n To total
For j = 1 To n - 1
items2(j) = items(j)
Next j
items2(n) = items(i)
For j = n + 1 To i
items2(j) = items(j - 1)
Next j
For j = i + 1 To total
items2(j) = items(j)
Next j
ListAll items2(), n + 1
Next i

End Sub
0
 
jklmnCommented:
Hi markma,

Have you got chance to test my code?
0
 
markma060100Author Commented:
Sorry,
I have been very busy this past week.
I think I'd better work with griessh's possible solution first.

Thanks,
Mark
0
 
markma060100Author Commented:
griessh,
Thanks for your continued help.
I think that we are in completely different places here...I have no experience in C++, all I know is a BIT of Pascal.
I just downloaded something called Octopod for C++ 3.2.
Here's what I did, File>New
then Selected C++ Builder and gave it a folder name,
then it went to some kind of wizard, so I selected console application.
Next>, gave it a file name
then went to File>New
C++ File, gave it a name
and copy&pasted the your source code.
Then went to Project>Run.
and near the bottom of the screen it gave me the error message.
Now what?

Thanks,
Mark
0
 
jklmnCommented:
Hi Mark,

Thank you for your comment and good luck on the work with griessh.
If you get chance to try my code, you need Visual Basic 6.

Regards.

jklmn
0
 
griesshCommented:
Mark

I think since you are not really interested in compiling the program yourself and I have no idea how Octopod works, I suggest you go to my website and download the source/binary for win32.
I think for EE the source is OK, if anybody has compiler problems, we will find a way out.

http://people.ne.mediaone.net/wgriesshammer/download/index.html

will give you links to these two files. Let me know if there are problems.

======
Werner
0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 4
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now