Generating all possible combinations

Hi guys. I'm sure this is pretty simple, but I'm stumped for a way to do this. Essentially if I have an array with P collumns and V^P rows, how can I fill in all the combinations, that is, essentially, all possible numbers in base V of P digits. For example, for P=3 and V=2

000
001
010
011
100
101
110
111

Keep in mind that this is an 2 dimensional array, not an array of ints.

For P=4 and V=3.

0000
0001
0002
0010
0011
0012
....

Having this array generated, the rest of work for what I'm trying to devolop is trivial. So having some code/tips on how to do this would be greatly appreciated. Thanks.
You need three loops.

The outer loop increases the level from 0 to P
The inner loop increases the number form 0 to V
The most inner loop repeats the number from the inner loop level * V + 1 and sets the value according to the level in the array

better use std::vector for this: http://www.cplusplus.com/reference/stl/vector/
:D It's the second time today I have been recommended to use std::vector. Unfortunately, this assignement is due in few hours, and I don't really have much time to look into it. Thanks though.
Sorry, but I can't seem to get your loop to work. It doesn't output as expected. Sorry for the newbness, but I'm now to C++ and I'm kinda confused.

Here's the code I wrote:
1
2
3
4
5
6
7
for(i=0;i<P;i++) {
        for(j=0;j<V;j++) {
            for(k=j;k<(V+1);k++) {
                a[i][j]=k;
            }           
        }
    }


But this returns 1, and says Run Failed.
How did you define a?

Honestly, it seems easier to write a wrapper class for numbers in a certain base, such as:
1
2
3
4
struct number {
    static unsigned base;
    std::vector<unsigned short> bnum;
}

Then just keep an array (or vector) of number Objects.
a is defined as short a[3][(3^2)]

I don't know how to work with std::vector. All I needed was to fill in the array with the pattern in the OP. If that cannot be achieved without vectors I think I will have to find another way to do it.
Last edited on
this assignement is due in few hours
I don't know why you guys always come at this time?

Your loops don't have much to do with muy suggestion...
level * V + 1


You don't need to use vectors as long as your array is large enough

1
2
3
4
5
6
7
8
9
for(i=0;i<P;i++) {
        for(j=0;j<V;j++) {
int repeat = i * V + 1;
int offs = j * repeat;
            for(k=0;k<repeat;k++) {
                a[offs + k][P - i - 1]=j;
            }           
        }
    }
It is not tested! Test it thoroughly. Especially that the index is at the right place!
Topic archived. No new replies allowed.