Helplife2.net: Permutations in C

15357

Companion Cube
Joined
Jan 11, 2005
Messages
15,209
Reaction score
23
So, I've many, many assignments for my classes, easy enough, except this.

I'm supposed to come up with 2 programs and a respective algorithm table for each one.

First one was way easy; you just have to use loops to get

*
**
***
****

and so on.

But the second one has stumped me:

Using only "for(x;x;x)" structures and "printf" thingys, I'm supposed to make a sort of 'permutation' (not sure, babelfish translation) for C.

Output should be this:

Code:
123
132
213
231
312
321

So far, the closest I could get was this:

Code:
#include <stdio.h>

void main(){
	int a = 0, b = 0, c = 0;

	for (a = 1; a <= 3; a++){
		for (b = 1; b <= 3; b++){
			for (c = 1; c <= 3; c++){
				printf("%d%d%d\n" , a, b, c);
			}
		}
	}

}

With an output of:
Code:
111
112
123
211
212
213
...
and so on

So, somebody? I suck at C. I need someone useful. :p

Thanks in advance.
 
This is probably the worst/least efficient way of doing it, but you can write a single if line inside the innermost for loop, basically saying, if b not equal a, and c not equal a, and c not equal b, then printf (thereby excluding 111, 112, 121, etc. from the output).

I'm sure there is a MUCH better way, but this should work.

[edit]
Or you could split the if line, as in

for a... {
for b... {
if b not = a then
for c... {
if c not = a and c not = b then
printf...
}
}
}

I think this is slightly better as it reduces evaluation of some c values? I dunno. I'm not a programmer. I can't even remember what the not symbol is :eek:.
 
This is probably the worst/least efficient way of doing it, but you can write a single if line inside the innermost for loop, basically saying, if b not equal a, and c not equal a, and c not equal b, then printf (thereby excluding 111, 112, 121, etc. from the output).

I'm sure there is a MUCH better way, but this should work.

[edit]
Or you could split the if line, as in

for a... {
for b... {
if b not = a then
for c... {
if c not = a and c not = b then
printf...
}
}
}

I think this is slightly better as it reduces evaluation of some c values? I dunno. I'm not a programmer. I can't even remember what the not symbol is :eek:.

Eh, well it says that I can't use anything other than for and printfs.
 
If you have 3 groups and 3 places then the maximum number of permutations you can have is 3*2*1 = 6 . A program which lists these permutations would be:-
Code:
#include <stdio.h>

int main()
{
 int i;
 int num[3] = {1,2,3};
 for(i=0;i<3;i++)
 {
   printf("%d%d%d\n",num[i],num[(i+1)%3],num[(i+2)%3]); 
   printf("%d%d%d\n",num[i],num[(i+2)%3],num[(i+1)%3]); 
 }

 return 0;
}

edit: so you can't use arrays ? . The problem is very similar to counting in base 3 ... if that helps.
 
Oh wow, I totally missed the "only for" part. Sorry!
 
If you have 3 groups and 3 places then the maximum number of permutations you can have is 3*2*1 = 6 . A program which lists these permutations would be:-
Code:
#include <stdio.h>

int main()
{
 int i;
 int num[3] = {1,2,3};
 for(i=0;i<3;i++)
 {
   printf("%d%d%d\n",num[i],num[(i+1)%3],num[(i+2)%3]); 
   printf("%d%d%d\n",num[i],num[(i+2)%3],num[(i+1)%3]); 
 }

 return 0;
}

edit: so you can't use arrays ? . The problem is very similar to counting in base 3 ... if that helps.

Hey, thanks. :D

Arrays? Yeah, that might be OK. But I'm not sure if I can use it or not. If there is a way without using them, it'd be appreciated.

Oh wow, I totally missed the "only for" part. Sorry!

Not a problem. :p
 
Hey, thanks. :D

Arrays? Yeah, that might be OK. But I'm not sure if I can use it or not. If there is a way without using them, it'd be appreciated.
The problem with that solution is that it is specific to the 3 number case. For example, it can't permute 1234.

My first instinct is to hack an if condition out of the for construct.

Code:
#include <stdio.h>

void main(){
	int a = 0, b = 0, c = 0;

	for (a = 1; a <= 3; a++){
		for (b = 1; b <= 3; b++){
			for (c = 1; c <= 3; c++)
			{
				for(;a != b && b!=c && a!= c;)
				{
					printf("%d%d%d\n" , a, b, c);
					break;
				}
			}
		}
	}

}
Obviously, this will not be a clean solution, but it meets your criterion. :thumbs:

EDIT: I just realised that this solution is 3-specific too. But once you have an if construct, you can use more complex algorithms.
 
Following on mindless_moder's use of modulus, I think a similar idea can be implemented without arrays.

(All I've used for the past five years is Matlab, so I may have some funky syntax errors here)

Code:
void main(){
	int a = 0, b = 0, c = 0;
	for (a = 0; a <= 2; a++){
		for (n = 1; n <= 2; n++){
			b = a+n;
			c = b+n;
			a2 = a%3 + 1;
			b2 = b%3 + 1;
			c2 = c%3 + 1;
			printf("%d%d%d\n" , a2, b2, c2);
		}
	}
}
 
You guys suck at coding.
5 lines, 2 variables:

Code:
void permutation ()
{
	int i,ii;
	for (i=1;i<4;i++)
		for (ii=i%3+1;ii!=i;ii=ii%3+1)
			printf ("%i%i%i\n",i,ii,6-i-ii);
	return ;
}
 
Shouldn't the solution be general, not specific to generating that output? Some of these algorithms seem to be totally missing the point.
 
He asked for a specific code to generate a specific output...
 
If you were coding something that created permutations of variable input, then yes. But this is only for the permutations of 1-3 in a 3-number sequence.
 
dont do it and say a comunist stole your homework
 
Here is a pretty darn simple method.
Only 3 lines of code and 1 variable but a bit more math:
Code:
void permutation ()
{
	for (int i=0;i<6;i++)
		printf ("%d%d%d\n",i%3+1,(i+i/3+1)%3+1,(i+(i/3)*2+2)%3+1);
	return ;
}
 
Sorry guys :eek: I guess I should have been more clear; it's supposed to use a loop-within-loops structure to do this. One of the criteria said "Do not use any other functions" which I didn't understand.

vikram's solution, I guess might do.


Man I love you guys. Thanks y'all.
 
Sorry guys :eek: I guess I should have been more clear; it's supposed to use a loop-within-loops structure to do this. One of the criteria said "Do not use any other functions" which I didn't understand.

vikram's solution, I guess might do.


Man I love you guys. Thanks y'all.

Well if you are going to code it lame like that, you might as well just be lame and write short concise code.
Code:
void permutation ()
{
        for (;0;){}
	printf ("123\n132\n213\n231\n312\n321");
	return ;
}
 
Dear god, I don't know what this is, but I never want to endure it.
 
Well if you are going to code it lame like that, you might as well just be lame and write short concise code.
Code:
void permutation ()
{
        for (;0;){}
	printf ("123\n132\n213\n231\n312\n321");
	return ;
}

A couple of my friends did something like that before on one of their homeworks... maybe numerical methods or reactor design. They knew what the answer should be but couldn't get their code to work, so they left in what they had and then just wrote a line to print the answer. But the TA actually read through their code. Bet he got a good laugh out of that.
 
Well if you are going to code it lame like that, you might as well just be lame and write short concise code.
Code:
void permutation ()
{
        for (;0;){}
	printf ("123\n132\n213\n231\n312\n321");
	return ;
}

Lol.

I can imagine the face on the professor as he reads through my algorithm map:
Code:
[START]
   |
<0?>----
   |____|
   |
[printf.......]
   |
 [END]

I love programming, but I just seem to not be able to do this correctly. I mean, I understand why the professor is giving us this question - he wants us to be able to fully understand the concept of the for loop - but I just can't do it. :/
 
I took courses in programming, forgot them all, how pointless -_-

So next time I take the next step of the programming courses, I am completely ass ****ed
 
Lol.

I can imagine the face on the professor as he reads through my algorithm map:
Code:
[START]
   |
<0?>----
   |____|
   |
[printf.......]
   |
 [END]

I love programming, but I just seem to not be able to do this correctly. I mean, I understand why the professor is giving us this question - he wants us to be able to fully understand the concept of the for loop - but I just can't do it. :/

The point of the for loop is not to hack together an IF statement out of the middle.
 
The point of the for loop is not to hack together an IF statement out of the middle.

I know. :p

That's why I included several solutions, including 2 of yours.



Everything can be solved by massed attack - I'll overwhelm the professor by giving him 4 solutions instead of 1. :p
 
Sounds like a good way to demonstrate that other people did your homework. If you haven't learned what modulus means, then using % might be a giveaway.
 
Sounds like a good way to demonstrate that other people did your homework. If you haven't learned what modulus means, then using % might be a giveaway.

Oh.

Damnit, I know what % does. Heck, I already know most of the course material. :/
 
I will try something

void porn

give me porn
give me porn
give me porn
wait change that for->blowjob

give me blowjob
give me blowjob
give me blowjob
give me blowjob
weird characters to make this look cool%&$Ç*"#*][
give me blowjob
give me blowjob
give me blowjob
repeat forever------

I am doing it right?
 
Ohhhh, you're doing something right at the very least.
 
Dan's solution can be done recursively to solve that.
Umm... almost any of these solutions work once you let recursion into the mix. One way to bypass this is to maintain your own stack, but that's just nasty.

In case you still want a general solution without recursion, here's the code:

Code:
#define N 3
#define LIST {1,2,3}

struct stack_type
{
  int val[20];
  int idx[20];
  int top;
} stk;

int main(void)
{
  int digs[N] = LIST;
  int i, j, max = N;
  stk.top = 0;

  for(i = 0; i <= max; i++)
  {
    for( ; i == max ; )                 // if current list has ended
    {
       for( ; max == 0 ; )             // if list is empty
       {
           printf("\n");
           for(j = 0; j < stk.top; j++)         // print the stack
              printf("%d",stk.val[j]);
       }
       max++;
       stk.top--;                     // POP from stack
       i = stk.idx[stk.top];
       for(j = max; j > i; j++)         // this loop turns {1,3} back into {1,2,3} using the stack
         digs[j] = digs[j-1];
       digs[i] = stk.val[top];
       break;
    }
    for( ; i != max ; )                 // else (current list has elements left)
    {
       stk.val[stk.top] = digs[i];      // PUSH onto stack
       stk.idx[stk.top] = i;
       stk.top++;
       for(j = i; j < max - 1; j++)     // this loop turns {1,2,3} into {1,3} for i = 1
         digs[j] = digs[j+1];
       max--;
       i = 0;
       break;
    }
  }
}
 
Back
Top