Solved

All permutations

Posted on 2001-07-24
11
273 Views
Last Modified: 2010-04-17
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
Comment
Question by:markma060100
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 22

Expert Comment

by:CJ_S
ID: 6316227
Is this homework?

Regards,
CJ
0
 

Author Comment

by:markma060100
ID: 6319279
No, it's not.
i'm doing it  for some word unscrambling games.
0
 
LVL 11

Expert Comment

by:griessh
ID: 6319457
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
 

Author Comment

by:markma060100
ID: 6320435
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
 
LVL 11

Expert Comment

by:griessh
ID: 6321711
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 5

Expert Comment

by:jklmn
ID: 6332681
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
 
LVL 5

Expert Comment

by:jklmn
ID: 6344511
Hi markma,

Have you got chance to test my code?
0
 

Author Comment

by:markma060100
ID: 6346013
Sorry,
I have been very busy this past week.
I think I'd better work with griessh's possible solution first.

Thanks,
Mark
0
 

Author Comment

by:markma060100
ID: 6346084
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
 
LVL 5

Expert Comment

by:jklmn
ID: 6346112
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
 
LVL 11

Accepted Solution

by:
griessh earned 200 total points
ID: 6346510
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This is an explanation of a simple data model to help parse a JSON feed
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now