Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 24-04-2008, 12:27 PM   #1 (permalink)
Q6600 Underclocked
 
sourishzzz1234's Avatar
 
Join Date: Dec 2006
Location: Howrah>>Kolkata
Posts: 118
Exclamation Permutation Program...


Please give me a working code to print the permutation of a given no..for example if i enter the no. 1234 it will print
1234
1243
1342
1324
1423
1432
and so on... till
it will print 24 possible permutations...
like this i can enter any number of any digit(max 6)... and the output will be it's all possible permutations...
Thanks in advance...
Please help me as this may come in my exams...
__________________
Core 2 Quad Q6600 2.4Ghz (OC->2.7Ghz)
Cooler Master Hyper 212 Plus
aBIT IP-35 E
1*2GB DDR2 800MHz Zion RAM
320GB Seagate Baraccuda
Sparkle nVdia GeForce 9400GT 512MB

Last edited by sourishzzz1234; 24-04-2008 at 12:40 PM.
sourishzzz1234 is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 24-04-2008, 12:38 PM   #2 (permalink)
Google Bot
 
Pathik's Avatar
 
Join Date: Aug 2005
Posts: 9,772
Default Re: Permutation Program...

In case of 4 digit nos, it ll show 24 permutations. Not 16.
__________________
My new blog: www.pathikshah.com
Pathik is offline  
Old 24-04-2008, 12:40 PM   #3 (permalink)
Q6600 Underclocked
 
sourishzzz1234's Avatar
 
Join Date: Dec 2006
Location: Howrah>>Kolkata
Posts: 118
Default Re: Permutation Program...

Quote:
Originally Posted by Pathik View Post
In case of 4 digit nos, it ll show 24 permutations. Not 16.
Ya sorry......i corrected it....but please give me the code.....
__________________
Core 2 Quad Q6600 2.4Ghz (OC->2.7Ghz)
Cooler Master Hyper 212 Plus
aBIT IP-35 E
1*2GB DDR2 800MHz Zion RAM
320GB Seagate Baraccuda
Sparkle nVdia GeForce 9400GT 512MB
sourishzzz1234 is offline  
Old 24-04-2008, 02:19 PM   #4 (permalink)
I see right through you.
 
Sykora's Avatar
 
Join Date: Sep 2005
Location: Chennai
Posts: 597
Default Re: Permutation Program...

A search for "generating permutations" gives you more than you'll ever need.
__________________
I didn't make the world, I only try to live in it.
http://lucentbeing.com
-- Sykora --
Sykora is offline  
Old 24-04-2008, 05:24 PM   #5 (permalink)
Legen-wait for it-dary!
 
dheeraj_kumar's Avatar
 
Join Date: Dec 2004
Location: Chennai
Posts: 2,471
Default Re: Permutation Program...

Its all loops... for a simple program, no. of digits to generate = no. of loops. Hope you get it.
__________________
If the Start Windows Restart when Windows starts check box is checked Windows Restart will start automatically every time Windows is started. - Actual excerpt from a windows program help file
dheeraj_kumar is offline  
Old 24-04-2008, 07:25 PM   #6 (permalink)
Just another linux lover.
 
subratabera's Avatar
 
Join Date: Jun 2006
Location: Bangalore, KA
Posts: 562
Default Re: Permutation Program...

Try this...
Quote:
#include<stdio.h>
#define ARRSIZE 3

int main()
{
int i,j,k;
char arr[ARRSIZE]="123";

for (i = 0; i < ARRSIZE; i++)
{
for (j = 0; j < ARRSIZE; j++)
{
for (k = 0; k < ARRSIZE; k++)
{
printf("%d %d %d\n",i,j,k);
if (i != j && i != k && j != k)
{
printf("%c%c%c",arr[i],arr[j],arr[k]);
printf("\n");
}
}
}
}
}
Change according to your requirements.
__________________
Today is a most unusual day, because we have never lived it before; we will never live it again; it is the only day we have.
(Registered Linux User #432737 - subratabera.blogspot.com)

Last edited by subratabera; 24-04-2008 at 07:34 PM.
subratabera is offline  
Old 24-04-2008, 09:25 PM   #7 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default Re: Permutation Program...

subratabera - What kind of an approach is that? Its so silly that you do so many loops just for the sake of displaying 3! = 6 numbers.

dheeraj_kumar - Nope, just one loop is more than enough. You can do it without a loop too.

Follow one of the standard algorithms, its just a 2-3 liner than this long 'thing' you've written which wont work for anything but ARRSIZE of 3.

Sykora's already given the tip to find some of the good permutation algorithms and it wasn't a "JFGI" point.
__________________
Harsh J
www.harshj.com
QwertyManiac is offline  
Old 24-04-2008, 11:08 PM   #8 (permalink)
Just another linux lover.
 
subratabera's Avatar
 
Join Date: Jun 2006
Location: Bangalore, KA
Posts: 562
Default Re: Permutation Program...

Well this is just another approach for solving a problem. I will appreciate your comment if you give me an actual example (2-3 liner). Also you can change the program easily to work with larger arrays (e.g. ARRSIZE 30).

I repeat, please support your answer with an example...
__________________
Today is a most unusual day, because we have never lived it before; we will never live it again; it is the only day we have.
(Registered Linux User #432737 - subratabera.blogspot.com)
subratabera is offline  
Old 25-04-2008, 03:57 AM   #9 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default Re: Permutation Program...

Well, all you had to do was learn at Wiki or even other places far better.

A fully running C++ program for "1234":

Code:
#include <iostream>
#include <string>

using namespace std;

int fact(int n, int f=1)
{
    while (n>0)
        f*=n--;
    return f;
}

string permutation(string s, int n)
{
    if(s.size()<2) return s;
    int quot = n/fact(s.size()-1); n = n % fact(s.size()-1);
    return s[quot] + permutation(s.substr(0,quot)+s.substr(quot+1),n);
}

int main()
{
    string s="1234";
    cout<<"Lexicographic permutation"<<endl<<endl;
    for (int i=0; i<fact(s.size()); i++)
    {
        cout<<"No. "<<i+1<<" Permutation is: "<<permutation(s,i)<<endl;
    }        
    return 0;
}

Using strings in C++ is easier, thus the preference of it over C. If you want me to prove it in C itself, hit me with it again.

P.s. Just changing ARRSIZE would work, you really think so? What about your strong check for a!=b!=c thing, it'd increase with increase in loops which would change each time with change in amount of digits as well. It works for what he's exampled above, but apart from that it doesn't satisfy a thing.
__________________
Harsh J
www.harshj.com

Last edited by QwertyManiac; 25-04-2008 at 04:04 AM.
QwertyManiac is offline  
Old 25-04-2008, 08:31 AM   #10 (permalink)
Pawned!... Beyond GODLIKE
 
fun2sh's Avatar
 
Join Date: May 2006
Location: World Of Warcraft -DOTA
Posts: 1,051
Default Re: Permutation Program...

wel there r three approaches.
1.lexicographic
2.johnson trotter
3.bottom up minimal change algorithm.

search abt these and u wil get good explanation.
i m not in mood to do the prog. but lexicographic is easiest to implement
__________________
If God has indeed created Himself in His own image, then I submit to you that God is a cockroach :mrgreen:!!!!!!!!
fun2sh is offline  
Old 25-04-2008, 09:25 AM   #11 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default Re: Permutation Program...

Non-lexicographic is the easiest really.
__________________
Harsh J
www.harshj.com
QwertyManiac is offline  
Old 25-04-2008, 09:45 AM   #12 (permalink)
I see right through you.
 
Sykora's Avatar
 
Join Date: Sep 2005
Location: Chennai
Posts: 597
Default Re: Permutation Program...

There is a next_permutation function in the C++ STL algorithms header. just start off with the lowest lexicographical permutation (all digits sorted in ascending order) and call that n! times.
__________________
I didn't make the world, I only try to live in it.
http://lucentbeing.com
-- Sykora --
Sykora is offline  
Old 25-04-2008, 01:10 PM   #13 (permalink)
Just another linux lover.
 
subratabera's Avatar
 
Join Date: Jun 2006
Location: Bangalore, KA
Posts: 562
Thumbs up Re: Permutation Program...

Quote:
Originally Posted by QwertyManiac View Post
P.s. Just changing ARRSIZE would work, you really think so? What about your strong check for a!=b!=c thing, it'd increase with increase in loops which would change each time with change in amount of digits as well. It works for what he's exampled above, but apart from that it doesn't satisfy a thing.
Anyone who modify the AARSIZE must also modify the other variables/things also. That's just a common sense. Is'nt it? By the way, I agree that your code is far better than mine. Thanks for that.
__________________
Today is a most unusual day, because we have never lived it before; we will never live it again; it is the only day we have.
(Registered Linux User #432737 - subratabera.blogspot.com)
subratabera is offline  
Old 25-04-2008, 01:20 PM   #14 (permalink)
I see right through you.
 
Sykora's Avatar
 
Join Date: Sep 2005
Location: Chennai
Posts: 597
Default Re: Permutation Program...

Quote:
Anyone who modify the AARSIZE must also modify the other variables/things also. That's just a common sense.
Changing ARRSIZE to 30 will require you to make up to 30 loops. That doesn't fit under common sense nor "modified easily" Brute force solutions are never extensible.
__________________
I didn't make the world, I only try to live in it.
http://lucentbeing.com
-- Sykora --
Sykora is offline  
Old 25-04-2008, 01:41 PM   #15 (permalink)
Just another linux lover.
 
subratabera's Avatar
 
Join Date: Jun 2006
Location: Bangalore, KA
Posts: 562
Default Re: Permutation Program...

Ya, you are right. I need to think seriously about my C skills... .

Thanks.
__________________
Today is a most unusual day, because we have never lived it before; we will never live it again; it is the only day we have.
(Registered Linux User #432737 - subratabera.blogspot.com)
subratabera is offline  
Old 25-04-2008, 05:32 PM   #16 (permalink)
Legen-wait for it-dary!
 
dheeraj_kumar's Avatar
 
Join Date: Dec 2004
Location: Chennai
Posts: 2,471
Default Re: Permutation Program...

@Querty_Maniac

Nice code there, and I agree with subratabera, I need to seriously brush up my c/c++ skills.
__________________
If the Start Windows Restart when Windows starts check box is checked Windows Restart will start automatically every time Windows is started. - Actual excerpt from a windows program help file
dheeraj_kumar is offline  
Old 25-04-2008, 09:26 PM   #17 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default Re: Permutation Program...

Quote:
Originally Posted by Sykora View Post
There is a next_permutation function in the C++ STL algorithms header. just start off with the lowest lexicographical permutation (all digits sorted in ascending order) and call that n! times.
Cool, I didn't know that! (Never used STL so much really)

Well this looks far far easy now:
Code:
#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    int a[] = {1,2,3,4};
    do
    {
        cout<<a[0]<<a[1]<<a[2]<<a[3]<<endl;
    } while (next_permutation(a,a+4));
    
    return 0;
}
__________________
Harsh J
www.harshj.com
QwertyManiac is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

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 On


Similar Threads
Thread Thread Starter Forum Replies Last Post
need c program VINSTAR Programming 3 30-01-2008 04:32 PM
program in c...... srikanth.9849671439 Programming 3 01-12-2007 01:30 PM
program? rounit Software Q&A 6 10-04-2007 10:54 PM
new program in 'close program menu' list and more....... vishakadatta QnA (read only) 2 12-04-2005 11:33 PM
RAID: Permutation & combinations??? tarun_34 QnA (read only) 6 24-02-2005 04:37 AM

 
Latest Threads
- by Sujeet
- by clmlbx
- by Sujeet
- by icebags

Advertisement




All times are GMT +5.5. The time now is 10:58 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2