Thread: Every possible combination of a set of numbers

  1. #1
    Registered User
    Join Date
    Sep 2013
    Location
    Jamaica
    Posts
    134

    Every possible combination of a set of numbers

    Well on my lil project and I'm at the most complicated part, haven't been stuck long but I need help. This part of the code i suppose to get generate all possible combination of number and numbers and combination can't repeat. There 6 set of numbers the first 5 can't repeat, here is what I have so far. Its not efficient and is time consuming for my PC. probrably will take upto 10 minutes to complete.

    Code:
    void Gen2(int limit){
    	FILE *fPtr;
    	
    	struct Ticket set;
    	struct Ticket nSet;
    	struct Ticket checkSet;
    	
    	int pass;
    	int x;
    	int check;
    	int count = 0;
    	int loop;
    	
    	if((fPtr = fopen("possCom.bin", "wb")) == NULL)
    	{
    		printf("Failed to get all possible combination.\n");
    	}
    	else
    	{
    		printf("Generation starting......\n");
    		for(pass = 1; pass <= limit; pass++)
    		{
    			numGen(&set);
    			printf("%d %d %d %d %d %d\n", set.num1, set.num2, set.num3, set.num4, set.num5, set.num6);
    			fwrite(&set, sizeof(struct Ticket), 1, fPtr);
    			if((pass % 10) == 0)
    			{
    				fseek(fPtr, 0 * sizeof(struct Ticket), SEEK_SET);
    				loop = pass;
    				for(x = count; x < loop; x++)
    				{
    					fread(&checkSet, sizeof(struct Ticket), 1, fPtr);
    					while(!feof(fPtr))
    					{
    						if(repeatCheck(&checkSet, &set) == 1)
    						{
    							printf("repetition was found\n");
    							nSet.num1 = set.num1;
    							nSet.num2 = set.num2;
    							nSet.num3 = set.num3;
    							nSet.num4 = set.num4;
    							nSet.num5 = set.num5;
    							nSet.num6 = set.num6;
    							while(repeatCheck(&checkSet, &nSet) == 1)
    							{
    								numGen(&gSet);
    							}
    							set.num1 = nSet.num1;
    							set.num2 = nSet.num2;
    							set.num3 = nSet.num3;
    							set.num4 = nSet.num4;
    							set.num5 = nSet.num5;
    							set.num6 = nSet.num6;
    						}
    						printf("repeatCheck complete\n");
    						getch();
    						fread(&checkSet, sizeof(struct Ticket), 1, fPtr);
    					}
    					count++;
    				}
    			}
    			printf("%d\n", pass);
    		}
    	}
    	fclose(fPtr);
    	
    	menu();
    }
    The repeatCheck function returns the correct value but is just repeats constantly. If you need source code just ask.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Hmmmm..... for a relatively simple problem, your code is remarkable in having undue complexity.

    There are relatively simple algorithms to do exactly what you're trying, in a fraction of the time.

    Do you know anything about arrays and recursive functions?

    The total number of combinations of six values is 720 (6!) [and if any values are duplicated, the number of combinations is less]. Even if 10 combinations are produced per second, that's a maximum of 72 seconds. And most modern computers will work significantly faster than that, assuming you're not doing excessive I/O. Assuming you pick a sane algorithm, naturally.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Sep 2013
    Location
    Jamaica
    Posts
    134
    ok I edited the code and got it working but its not getting all possible combinations still.

    Grumpy I like to use original coding, I love being helped here and there but I wont copy and edit some's code or example ill look at it and see the method then adjust mine in ways to complete the task. BTW those shorter algorithms doesn't work for what I want to do.

    In my program order of numbers aren't important so its a total of 324642 possible combination of 35 numbers, so it can 34 33 29 5 2 1 or 1 3 2 20 32 10, it doesn't matter once all possible combinations are found thats my objective here

  4. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by BIGDENIRO View Post
    There 6 set of numbers the first 5 can't repeat
    Quote Originally Posted by BIGDENIRO View Post
    In my program order of numbers aren't important so its a total of 324642 possible combination of 35 numbers
    The 6th number is allowed to be any of the 35 numbers, so the number of combinations is 35 things taken five at a time, multiplied by the number of combinations of 35 things taken 1 at a time = 324632 x 35 = 11362120 combinations.

    For generic code, you could use some variation of a next_permutation() function (you can search for these on the internet).

    If this isn't generic code and specifically for 5 numbers plus any number, then you could use 6 nested loops, for example this program will calculate a sum of 11362120:

    Code:
    #include<stdio.h>
    
    int main(void)
    {
    int i1, i2, i3, i4, i5, i6;
    int sum = 0;
    
        for(i1 =    1; i1 <= 31; i1++)
            for(i2 = i1+1; i2 <= 32; i2++)
                for(i3 = i2+1; i3 <= 33; i3++)
                     for(i4 = i3+1; i4 <= 34; i4++)
                         for(i5 = i4+1; i5 <= 35; i5++)
                             for(i6 =    1; i6 <= 35; i6++)
                             {
                                 sum += 1;
                             }
        printf("%d\n", sum);
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 11-14-2012, 10:44 PM
  2. Combination calculator
    By 843 in forum C Programming
    Replies: 3
    Last Post: 10-15-2010, 03:43 AM
  3. sum combination
    By the_contractor in forum C Programming
    Replies: 5
    Last Post: 02-17-2010, 01:35 PM
  4. binary combination.
    By Moony in forum C Programming
    Replies: 1
    Last Post: 02-24-2008, 12:55 AM
  5. Every possible combination/best combination
    By ldb88 in forum C++ Programming
    Replies: 12
    Last Post: 07-19-2007, 02:48 AM