Generate all configuration of given array

If I have an array: 0 1 2 3, and the output is:
000
001
002
003
011
012
013
022
023
033
111
112
113
122
123
133
222
223
232
233
333
How the code?
this does not seem logical , you have
013
022
023
033


instead of
013
021
022
023
031
032
033

?
Was that really the output request ?

Perhaps you want to use a recursive permutation function if you want to generate all permutations , but your output seems to be in ascending order...

Please give the exact and complete question , if there was no output tell us then.
> this does not seem logical
it is, that you can't see the pattern is another thing.

> instead of
> 013
> 021
¿why dont 020? that does not seem logical.


> if you want to generate all permutations
3-combination with repetition of 4 elements
@OP
Think about it as if it were your car odometer. Each kilometer you travel causes the odometer to move to the next number:

    027344
    027345

Each next number flips the one's place. When it is time to flip back to zero, you also need to flip the ten's place as well:

    027349
    027350

Every digit place can affect the next place -- the trick is to watch the zeros:

    027999
         0
        0
       0
      8
    028000

The digits don't have to be the numbers 0-9. They can be anything. In your case, they are the digits 0-3.

You'll need to have a function that takes a digit place and adds a '1' to it:

1
2
3
4
5
6
bool add_one_to( char digits[], int place_index )
{
  ...

  return (are we done?)
}
Call it as:
1
2
3
4
5
6
7
8
9
int main()
{ //               012 : place index
  char number[] = "000";

  cout << number << "\n";  // prints "000"

  add_one_to( number, 2 );
  cout << number << "\n";  // prints "001"

There are several things to consider:
- what happens when adding wraps around? How do you know when it should wrap around?
- what happens when adding does not wrap around?
- what should happen when it is time to wrap around the digit at index 0? How does this help you know when to terminate the loop that calls add_one_to() and cout << number?

Hope this helps.
Last edited on
closed account (E3h7X9L8)
this looks like a combinatorics problem , the easiest way to do it its with recursion, here is a example for your array , you can adapt the code for your needs this is just pure informative

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
using std::endl;
using std::cout;
using std::cin;

int a[4] = {0, 1, 2, 3};
int b[4] = {0, 0, 0, 0};

void perm(int, int);

int main()
{

	perm(0, 4);
	return 0;
}

void perm(int var, int size)
{
	if (var == size - 1) // if var == with size - 1 print permutation
	{
		for (int i = 0; i < size - 1; i++)
		{
			cout << ::b[i] << " ";
		}
		cout << endl;
	}
	else
	for (int i = 0; i < size; i++)
	{
		::b[var] = ::a[i];   
		perm(var + 1, size); //recursion 
	}
}
Woot! Way to do the OP's homework for him, in the least-understandable way possible. You've even got global variables with foo names in there, an array that is inexplicably sized 4 instead of 3, and multiple indices.

I'm not trying to be a jerk, and I'm glad you can figure it out too, but it does get tiresome to take the time to give a good answer only to have someone come in right after me and give one that both relieves the OP of having to actually think and confuses the OP if he does actually want to think.

I know I'm a little touchy about this, but it really stokes my ire when people blithely throw away my efforts to help others. This happens about every time I post here.


And, no, really, the easiest way to do it is exactly as I suggested. It's certainly much easier than trying to figure out what you were thinking with some of the stuff you did in that code.

/me stomps off
> Woot! Way to do the OP's homework for him
¿why people don't test anymore?

> an array that is inexplicably sized 4 instead of 3
The OP stated clearly «If I have an array: 0 1 2 3»
The output seems to be formed by selecting elements from that array.

> but it does get tiresome to take the time to give a good answer
your answer is for another problem.
- you miss to select elements from the array (missing a translation step)
- the sequence generated is not the one that the OP want.

> And, no, really, the easiest way to do it is exactly as I suggested.
no, you could simply use a couple of nested loops.
Last edited on
ne555 wrote:
nonsense
What's your problem!?

The OP stated clearly «If I have an array: 0 1 2 3»
The output seems to be formed by selecting elements from that array.
Way to go Sherlock.

OP also clearly stated that he wanted to see the following sequence:
000
001
002
003
...

Had you been less interested in being a jerk than actually analyzing leryss's code, you'd find this:
6
7
int a[4] = {0, 1, 2, 3};
int b[4] = {0, 0, 0, 0};

Hmm, I do wonder which one of those four-element arrays you think you're trying to win an argument with here...?

Oh, I know. I'm clearly just too stupid to know which of those four element arrays I'm talking about when I complained. Thanks for bringing it to my attention.

your answer is for another problem.
- you miss to select elements from the array (missing a translation step)
- the sequence generated is not the one that the OP want.
Ooooh, you've got me! You obviously understand me better than myself! Woo!

Funny how I've managed to generate the exact sequence the OP wants a number of times now.

But since I'm so dumb, I'll just go away.

Good luck!
> You obviously understand me better than myself!
It is not yourself the one that needs to understand your explication

> When it is time to flip back to zero
it does not flip back to zero.
Note that except for one case the sequence of each digit is non-decreasing.

Also, ¿would you redefine `sum' if the array were {2,3,5,7}?
LOL. Okay, I'll bite back.

> You obviously understand me better than myself!
It is not yourself the one that needs to understand your explication
Again, you've misunderstood clear language. I wasn't making a statement about my understanding. (Though, unless I'm overrating you, I think you are well-aware of this.)

> When it is time to flip back to zero
it does not flip back to zero.
Note that except for one case the sequence of each digit is non-decreasing.
You seem to want to treat the array of symbols as if they have some sort of intrinsic relationship.

The only relationship they have is their order in the sequence, which I have recommended the user consider as circular. Odometers and other 'flip' devices work by turning or rotating or, gasp, flipping to the next symbol, eventually flipping back to the first. This is all very obvious if you know anything at all about how an odometer works -- unless, of course, you're trying to be a jerk.

You'll also recall I said the digits can be anything. The array could be {2,*,q,☻}. OP is using {0,1,2,3}.

Clearly, you aren't adding one to the displayed symbol. You're adding one to the index into the displayed symbols. This should have been fairly obvious. But since it wasn't, thanks for giving me an opportunity to state the minutiae.

You go out of your way to misunderstand me, on a regular basis. Why is that, I wonder?


On a related note, I also like how the 'report' button has become something of a censorship button. Being upset, or posting a critical remark, or simply disagreeing with someone is too much to handle, apparently. Rather than let opinions with which we disagree stand, we can 'report' them in an effort to make them disappear! Very, very mature.

So go ahead. Report me again! Do it! Do it!

I'm beginning to develop regular regret for trying to do something as simple as help some poor soul online to better understand his homework. I mean, it's sooo easy to ignore the meaning of words, and just pretend they mean something else. I don't know how any computer scientist can handle living with that.

And since we're doing OP's homework for him, show me your 'simple couple of nested loops' and I'll show you mine, and we'll let others decide which is easier to understand.
> eventually flipping back to the first.
no, not the first.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

int main(){
	const int n = 4;
	int foo[n] = {0, 1, 2, 3};
	for(int K=0; K<n; ++K)
		for(int L=K; L<n; ++L)
			for(int M=L; M<n; ++M)
				std::cout << foo[K] << foo[L] << foo[M] << '\n';
}
000
001
002
003
011
012
013
022
023
033
111
112
113
122
123
133
222
223
233
333
note that this output is incorrect becouse after 223 comes 233 but it was 232 in the OP.
Last edited on
Topic archived. No new replies allowed.