473,406 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

permutation code.

this is my version of permutation and combinations.
is this piece of code correct.
#include<stdio.h>
int a[]={1,2,3,4};

permute(int * b,int n)
{
int i;
if(n==1)
{
for(i=0;i<4;i++)
{
printf("%d",a[i]);
}
printf("\n");
return;
}

int temp;
for(i=0;i<n;i++)
{
temp=b[0];
b[0]=b[i];
b[i]=temp;

printf("\n");
for(i=0;i<4;i++)
{
printf("%d",a[i]);
}
permute(a+1,n-1);

temp=b[0];
b[0]=b[i];
b[i]=temp;

printf("\n");
for(i=0;i<4;i++)
{
printf("%d",a[i]);
}
}
}

main()
{
permute(a,4);
}

Jul 9 '06 #1
4 8618
RIFF.D
Jul 9 '06 #2
int a[]={1,2,3,4};
permute(int * b,int n)
{
int i;
if(n==1)
{
for(i=0;i<4;i++)
{
printf("%d",a[i]);
}
printf("\n");
return;
}
int temp;
for(i=0;i<n;i++)
{
temp=b[0];
b[0]=b[i];
b[i]=temp;
printf("\n");
for(i=0;i<4;i++)
{
printf("%d",a[i]);
}
permute(a+1,n-1);
temp=b[0];
b[0]=b[i];
b[i]=temp;
printf("\n");
for(i=0;i<4;i++)
{
printf("%d",a[i]);
}
}

}
main()
{
permute(a,4);
}

Jul 9 '06 #3
rr********@gmail.com wrote:
this is my version of permutation and combinations.
is this piece of code correct.
#include<stdio.h>
<snip>

It is no more correct than when you posted it on the 7th, it also has
not improved since I commented on it also on the 7th. Any C compiler in
conforming mode (conforming to any version of the C standard) is
required to issue diagnostics for the code. If I ignore the diagnostics
and run it anyway it does not produce the correct result.

Go back and read the comments I made on it in the thread
http://groups.google.co.uk/group/com...da5bd8bdca72db
Once you have addressed those points and produced some code that is
actually different you might get a few more comments.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
Jul 9 '06 #4
<rr********@gmail.comwrote:
this is my version of permutation and combinations.
is this piece of code correct.
#include<stdio.h>
int a[]={1,2,3,4};

permute(int * b,int n)
{
int i;
if(n==1)
{
for(i=0;i<4;i++)
{
printf("%d",a[i]);
}
printf("\n");
return;
}

int temp;
for(i=0;i<n;i++)
{
temp=b[0];
b[0]=b[i];
b[i]=temp;

printf("\n");
for(i=0;i<4;i++)
{
printf("%d",a[i]);
}
permute(a+1,n-1);
You have to make up your mind. Is the array a global variable, as you use
it here? Or is it a parameter passed to functions, as used in main? It is
pretty much essential when using recursion, that a function called via.
recursion receives its knowledge via parameters. So start by putting the
array in main and doing whatever fixup that implies. I am new to this thread
so I have no idea what your real underlying problem is. But this problem is
a biggie and must be fixed before you can proceed. .

Also, don't post code that the compiler complains about. You don't declare a
return type for permute().. This attracts criticism like a misplaced
decimal point at an accountant's convention. It may have nothing to do with
your problem but it causes all kinds of hostile - and perhaps not germane to
your problem - responses
>
temp=b[0];
b[0]=b[i];
b[i]=temp;

printf("\n");
for(i=0;i<4;i++)
{
printf("%d",a[i]);
}
}
}

main()
{
permute(a,4);
}

Jul 9 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Talin | last post by:
I'm sure I am not the first person to do this, but I wanted to share this: a generator which returns all permutations of a list: def permute( lst ): if len( lst ) == 1: yield lst else: head =...
3
by: Jack Middleton | last post by:
Hi! I'm lookin for a faster permutation algorithm for matrices. I know that it can be done with multiplying a matrix with a permutation matrix. It just seems a waste to iterate through all those...
1
by: user | last post by:
Hello I have Array of 50 ints. I want to receive random permutation, so in each int will be different number from 0-49. Is there any class for permutation ? Thanx Michal
6
by: Rajesh | last post by:
Hello Everybody, Can anybody help me in writing a C program to generate and print all possible combinations of n numbers. For eg. for 3 numbers(1,2,3) there turn out 3! combinations. (1,2,3),...
27
by: onkar | last post by:
How to generate different permutations of n char array? ex : for n= 3, and basic string = abc bca cab bac cab ..... ... ..
3
by: weidongtom | last post by:
Hi, I have been working at this problem, and I think I need a permutation algorithm that does the following: Given a list of elements that are either a character or a character follows by a...
6
by: badcrusher10 | last post by:
Hello. I'm having trouble figuring out what to do and how to do.. could someone explain to me what I need to do in order to work? THIS IS WHAT I NEED TO DO: Professor Snoop wants a program...
7
by: xirowei | last post by:
Let's say i create a String array that store 4 Alphabets {"A","B","C","D"} How can i get the result if i need permutation of 4P3 and 4P2? I had refer to many examples from the internet, but...
0
by: 249740 | last post by:
Write a program that reads N phrases and determines if one phrase is a permutation of the other. For example: Phrase 1 is: “One World One Dream” Phrase 2 is: “World One One Dream”. Then the output...
13
by: sillyhat | last post by:
Hello, can someone please help. I found the following code at http://code.activestate.com/recipes/252178/ def all_perms(str): if len(str) <=1: yield str else: for perm in all_perms(str):...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.