Click here to Skip to main content
15,885,941 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Hi,

I am doing a small project and I am new to programming.

I need to do a list where I can perm any 1 from 12 from 4 sections of 12 and I do not have a clue where to start.

Can anyone assist please.

kind regards
jim
Posted
Updated 16-Dec-10 22:29pm
v3
Comments
Indivara 7-Dec-10 22:13pm    
Maybe it's just me, but what you want to do is not comprehensible. What do you mean by "perm any 1 from 12 from 4 sections of 12"?
jimleslie 8-Dec-10 6:25am    
hi, thank you for your reply maybe i never explained this right so i will try another way.
take 4 horseraces i want to pick any 1 horse from each race to perm them so i finish up with
(1,6,8,12) (3,5,6,11) and on and on.
i am trying to draw up rinks = x4 per team so i can have 2 or 3 playing in the same team but
never the same 4 i hope this explains what i am looking for.
kind regards and thank you for your time.

jim
Dalek Dave 17-Dec-10 4:29am    
Edited for Grammar and Readability.
Abhinav S 19-Dec-10 10:45am    
Have you tried anything so far?

You should use STL containers and algorithms, but you need to learn it...
Your horses should be in an std::list< int >[^], then use one of algorithm function, may be generate[^] with your lambda function as third parameter.
 
Share this answer
 
It sounds like you want combinations and not permutations. There is no need to use an STL container. You can try something like this, which I utilized to confirm a k-th to default computation I made with Newton-Girard.

Basically, create a numbering system and iterate over the ordered sets with strictly increasing #'s.

C#
namespace {
    void print(int *s, int const & k) {
        for (int j=0; j<k; j++)
            printf("%i ", s[j]+1);
        printf("\n");
    }
    int diff_sum(int *s, int const & k) {
        int sum=0;
        for (int j=1; j<k; j++)
            sum+=s[j]-s[j-1];
        return sum;
    }
};

void combination_list(int const & N, int const & k) {
    int selection[k], reset[k-1];
    selection[0]=N-k;
    int i;
    for (i=1; i<k; i++)
        selection[i]=reset[i-1]=N-k+i;
    int count=1;
    print(selection, k);
    int j;
    while (selection[0]+diff_sum(selection, k)>=k) {
        for (j=0; j<k; j++) {
            if ( diff_sum(&selection[j], k-j)==k-1-j) {
                selection[j]-=1;
                for (i=j+1; i<k; i++)
                    selection[i]=reset[i-1];
                count++;
                print(selection, k);
            }
        }
    }
    printf("count = %i\n", count);
}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900